Data types and objects
In programming, data are objects (entities) that can be accessed by a name (identifier). Data are allocated in the computer memory. Every data has a type. Data type is an attribute that tells the computer how to interpret a given piece of data. There are specified sets of values and operations for each data type.
Fundamental data types
>>> a = 9 #decimal representation
>>> b = 0b1100 #binary representation - we use 0b before our binary value; 1100 binary is 12 decimal
>>> print(b)
12
>>> c = 0o32 #octal representation - we use 0o before our octal value; 32 octal is 26 decimal
>>> print(c)
26
>>> d = 0xc3f #hexadecimal (hd) representation - we use 0x for our hd value; c3f hd is 3135 decimal
>>> print(d)
3135
-float – represents real numbers
>>> pi = 3.141592654
>>> e = 2.718281828
-bool – represents boolean (logical) values which are True and False; booleans are actually integers, where True has integer value 1 and False has integer value 0
>>> a = True
>>> b = a + 5
>>> print(b)
6
>>> c = False
>>> d = c - 2
>>> print(d)
-2
-complex – represents complex numbers in form a + bj, where a and b are real numbers and j is imaginary unit (in maths, we used symbol i)
>>> z = 3 + 4j
>>> text = "James Bond 007"
Type conversion
>>> x = 12.5
>>> y = int(x)
>>> print(y)
12
>>> #float to int; converted value is previous value rounded to the first lower integer value
>>> a = 34
>>> b = bool(x)
>>> print(y)
True
>>> #int/float to bool; non-zero values are converted to True, while zero is converted to False
>>> p = "12345"
>>> q = int(p)
>>> print(q)
12345
>>> #str to int; this conversion works only if the string only contains digits
>>> p = "123a4b"
>>> q = int(p)
ValueError: invalid literal for int() with base 10: '123a4b'
>>> #if there is a single character that is not a digit in the string, str to int isn't possible
Variables
Variable is a piece of data, whose value can be changed. Basically, variable is a place in the computer memory where we store our data. Variables can be accessed by its name (identifier).
Data types of variables
>>> x = 7
>>> type(x)
<class 'int'>
>>> #data type of x is int (integer)
>>> x = "programming"
>>> type(x)
<class 'str'>
>>> #we changed data type of x to str (string)
>>> x = True
>>> type(x)
<class 'bool'>
>>> #we changed data type of x to bool (logical)
Defining variables
When we first define a variable, we must give it some value, or else our program will signal an error. This is called variable declaration in other programming languages, but Python doesn’t allow it. If a variable is already defined, writing its name alone will print a value of that variable.
>>> x
NameError: name 'x' is not defined
>>> x = 13
>>> x
13
>>> y = None
>>> x = input("Read in a value: ")
Read in a value: 3
>>> print(type(x))
<class 'str'>
>>> y = x + 2 #we cannot add integers to strings
TypeError: can only concatenate str (not "int") to str
>>> y = int(x) + 2 #explicit conversion string to integer in order to perform addition
5
We can assign value None to our variable, which is NonType value (this is similar to declaration in other languages).
>>> x = None
>>> type(x)
<class 'NoneType'>
We can also define multiple variables at the same time. However, the number of variables we want to define must be equal to the number of values we are assigning:
>>> x = y = z = 69 #all variables get value 69
>>> a, b, c = 3, 7, 18 #values of a, b and c are, respectively, 3, 7 and 18
>>> p, q, r = 2.5, "Dog", 7 #values can be of different data types (respectively: float, str, int)
>>> i, j, k = "program", True
TypeError: not enough values to unpack (expected 3, got 2)
>>> #in the last example, we had more variables than values, so the program reported an error
Destroying variables
If we have no further use for the variable, we can destroy it with the function del:
>>> x = "university"
>>> print(x)
university
>>> del(x)
>>> print(x)
NameError: name 'x' is not defined
>>> #after we deleted x, it no longer exists, therefore, when we try to print x again, an error occurs
Variable types
Variable identifiers
>>> verylongidentifierforthisvariable = 3.5
2) identifier can contain uppercase letters (A-Z), lowercase letters (a-z), digits (0 to 9) and underscore character ( _ ); Python is a case-sensitive language, which means uppercase and lowercase variants of the same letter are two different characters
>>> basKeTbaLl_13 = "Spalding" #we used uppercase, lowercase, digit and underscore
3) identifier must not start with a digit (it’s recommended to start with a lowercase letter)
>>> 7abc = 11 #if identifier starts with a digit, program signals an error
SyntaxError: invalid syntax
4) identifier must not be a keyword (explained in chapter 1); however, with a single post-underscore, keywords can be used as identifiers
>>> continue = "Can a variable identifier be a keyword?"
SyntaxError: invalid syntax
>>> continue_ = "Yes, but with _ at the end!" #underscore makes keywords available for use in naming identifier
5) identifier must not contain special characters, such as ! @ # $ %
>>> w!p%er = True
SyntaxError: invalid syntax
6) identifier must be a single word/string
>>> kobe bryant = 24 #incorrect
SyntaxError: invalid syntax
>>> michael_jordan_23 = "GOAT" #ok
Table of contents
- Basic Data Structures
- Functions
- Collections
- Exceptions
- Input & Output
- RegEx & PRNG
- Classes And Objects
- Popular Libraries
- Additional Problems