8.04M

Lecture 2.1 - Data Types. Data Manipulation (1)

1.

Advanced Programming I
Lecture 2.1 – Data Types and
operations over them
Shynggys Kairatuly Alshynov
MSc in IT
[email protected]

2.

OBJECTIVES
• Identifiers and keywords
• Variables, functions, classes, modules
• Deleting variables
• Naming conventions
• Keywords list
• Data Types
• Numbers and their features https://docs.python.org/3/library/stdtypes.html#numeric-types-intfloat-complex
Integers
Floats
Complex numbers
Decimals
Binaries
Hexs/Octs
• Strings and their features https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
• Code Examples

3.

LEARNING OUTCOMES
• At the end of this lecture you will be able to:
• Use different types of variables and their features
• Access and/or change elements of variables
• Give suitable names to variables

4.

Identifiers
• Identifiers - a name used to identify a variable, function, class,
module or other object. An identifier starts with a letter A to Z or a
to z or an underscore (_) followed by zero or more letters,
underscores and digits (0 to 9)
• Python does not allow punctuation characters such as @, $, and %
within identifiers. Python is a case sensitive programming language.
• Variable is nothing but a reserved space in a memory to store some
values.
• Deleting variables:
• del var_name

5.

Identifiers naming conventions
• These are naming conventions for Python identifiers:
• Class names start with an uppercase letter. All other identifiers start with a
lowercase letter.
• Starting an identifier with a single leading underscore (_) indicates that the
identifier is private.
• Starting an identifier with two leading underscores (__) indicates a strongly
private identifier.
• If the identifier also ends with two trailing underscores (__name__), the
identifier is a language-defined special name.

6.

List of keywords
• These are reserved words and you cannot use them as constant or
variable or any other identifier names.

7.

DATA TYPES – Numbers I
• Numbers are data types that store numeric values. They are
immutable. This means, changing the value of a number data type
results in a newly allocated object.
• int (signed integers) - Integers in Python 3 are of unlimited size.
• float (floating point real values) − represented as decimal point dividing the
integer and the fractional parts. Floats may also be in scientific notation,
with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).
• complex (complex numbers) − are of the form a + bJ, where a and b are
floats and J (or j) represents the square root of -1 (which is an imaginary
number). The real part of the numbser is a, and the imaginary part is b.
Complex numbers are not used much in Python programming.

8.

DATA TYPES – Numbers II
• It is also possible to represent integers as binary, hexadecimal or octal
values:
• Hex:
• number = 0xA
• number will be equal to what?
• Oct:
• number = 0o22
• number is equal to ‘I hope you know it guys’
• Binary:
• number = 0b1010
• number is equal to ‘I hope you know it too’

9.

DATA TYPES – Numbers III
• Number type conversion:
• int(value) – converting to int
• float(value) – converting to float
• complex(value) – converting to complex number with real part equal to
value and imaginary part 0.
• complex(real, imaginary) – converting to complex number with real and
imaginary parts.

10.

DATA TYPES – Numbers IV
• Math functions for numbers (some examples):
• abs(x) – returns absolute value of a number;
• max([1,2,3,4,9,7,0]) – returns maximum value from inerrable object
• min([1,2,3,4,9,7,0]) – returns minimum value from inerrable object
• pow(x,y) – returns a value of x^y, can be written as x**y
• sqrt(x) – returns square root of x.

11.

DATA TYPES- STRINGS I
• Strings are amongst the most popular types in Python. We can
create them simply by enclosing characters in quotes. Python treats
single quotes the same as double quotes. No chars.
• String characteristics:
• Immutable
• Indexable
• Sliceable
• Iterable

12.

DATA TYPES- STRINGS II
• Indexing is accessing a character from a string.
• “Testing value”[4] will return ‘i'
• “This is A”[-1] will return ‘A’
• Slicing is getting a range of indexes:
• “this is some slice testing”[1:4]will return ‘his’
• You can iterate through a string char by char

13.

DATA TYPES- STRINGS III
• String methods:
• IDLE, YOUR TIME HAS COME
• Use dir(str) to see all the string methods in python interpreter
• Real time demo

14.

TRICKS – STRINGS
• Autoconcatenation
• print("py" "thon" " is " " fun")
• Multiplication:
• “a”*5 will return “aaaaa”
• Special character avoidance
• print(“D:\Something\name”)
• print(r“D:\Something\name”)
• print(“Is there another way to use ‘quotation marks?’)
• print("Yes, there are. \“You could use it\" before")

15.

TRICKS – NUMBERS
• Negative Floor (integer) division:
• -22//10 = -3
• Number is rounded down toward the more negative value, because of floor division.
• Negative Floating-Point division (modulo operation)
• -22%10 = 8
• Mathematical reasons
• -22 = 10*i + r, if 0<=r<10, i = -3
• Divisor*i + reminder = Number; condition 0<= reminder < Divisor
• More at
https://docs.python.org/3.6/faq/programming.html#numbers-andstrings

16.

PEP8 – Style Guide For Python
• https://www.python.org/dev/peps/pep-0008/
• Examples:

17.

SUMMARY
• You obtained(not for sure):
• General knowledge about variables
• Understandings about types like strings, numbers and in python
• Skill to use features of the data types and variables
Congratulations!

18.

Thank you for your attention!
Python is magical!
English     Русский Rules