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

Most fundamental data types in Python are:
 
-int – represents integers; integer values can be defined in decimal, binary, octal or hexadecimal system
>>> 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
-str – represents strings (group of characters); string values are written inside of quotation marks (“” or ”); in this chapter, we will only define
string data type for use in further examples, but in future lessons we will learn more about it
>>> text = "James Bond 007"

Type conversion

Type conversion is transformation of one data type into another. Some conversions are, however, impossible to perform. Data type conversion can be explicit or implicit.
 
Explicit conversion means we have to point out the type we’re converting data to. Implicit conversion will be explained in next chapter (3. Operators).
Some of the possible conversions are float to int, int to float, int/float to bool, bool to int. Here are a few examples:
>>> 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

Data type of a variable can be redefined, hence there is no need to explicitly state the type when defining one. For this reason, Python is also called a loosely-typed language.
If we are not sure what is the data type of our variable we can use the function type, which requires the variable’s name as an argument and returns the name of its data type:
>>> 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
We can define variables by assigning values to it or reading values via standard input.
When we assign value, we use assign operator ( = ). We will learn more about operators in the next chapter.
We can also assign value by reading it from the standard input, using function input. This function has optional argument that represent a message written in quotation marks and appears before reading in a value. When a value is read, its type is string and must be explicitly converted in order to use it for various operations.
>>> 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

From the aspect of “visibility” of a variable in program, there are two types of variables: global and local.
Global variables are visible and accessible in the whole program. Once defined, they can be used anytime, anywhere in our program, unless we delete them with the function del.
Local variables are visible only in some parts of our program. We will learn more about them in some of the next lessons.

Variable identifiers

When naming a variable, we must follow these rules:
1) identifier can be of any length
>>> 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

Leave a Reply