python-course.eu

6. Data Types and Variables

By Bernd Klein. Last modified: 12 Feb 2022.

Introduction

Have you programmed low-level languages like C, C++ or other similar programming languages? If so, you might think you know already enough about data types and variables. You know a lot, that's right, but not enough for Python. So it's worth to go on reading this chapter on data types and variables in Python. There are dodgy differences in the way Python and C deal with variables. There are integers, floating point numbers, strings, and many more, but things are not the same as in C or C++.

If you want to use lists or associative arrays in C e.g., you will have to construe the data type list or associative arrays from scratch, i.e., design memory structure and the allocation management. You will have to implement the necessary search and access methods as well. Python provides power data types like lists and associative arrays called "dict", as a genuine part of the language.

So, this chapter is worth reading both for beginners and for advanced programmers in other programming languages.

Containers

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See our Python training courses

See our Machine Learning with Python training courses

Variables

General Concept of Variables

This subchapter is especially intended for C, C++ and Java programmers, because the way these programming languages treat basic data types is different from the way Python does. Those who start learning Python as their first programming language may skip this and read the next subchapter.

So, what's a variable? As the name implies, a variable is something which can change. A variable is a way of referring to a memory location used by a computer program. In many programming languages a variable is a symbolic name for this physical location. This memory location contains values, like numbers, text or more complicated types. We can use this variable to tell the computer to save some data in this location or to retrieve some data from this location.

Box X and Y

A variable can be seen as a container (or some say a pigeonhole) to store certain values. While the program is running, variables are accessed and sometimes changed, i.e., a new value will be assigned to a variable.

What we have said so far about variables best fits the way variables are implemented in C, C++ or Java. Variable names have to be declared in these languages before they can be used.

int x;
int y;

Such declarations make sure that the program reserves memory for two variables with the names x and y. The variable names stand for the memory location. It's like the two empty shoeboxes, which you can see in the picture above. These shoeboxes are labeled with x and y. Like the two shoeboxes, the memory is empty as well.

Putting values into the variables can be realized with assignments. The way you assign values to variables is nearly the same in all programming languages. In most cases, the equal "=" sign is used. The value on the right side will be saved in the variable name on the left side.

We will assign the value 42 to both variables and we can see that two numbers are physically saved in the memory, which correspond to the two shoeboxes in the following picture.

Box X and Y

x = 42;
y = 42;

We think that it is easy to understand what happens. If we assign a new value to one of the variables, let's say the value 78 to y:

y = 78;

Assigning a value to a variable in C or Java

We have exchanged the content of the memory location of y.

We have seen now that in languages like C, C++ or Java every variable has and must have a unique data type. E.g., if a variable is of type integer, solely integers can be saved in the variable for the duration of the program. In those programming languages every variable has to be declared before it can be used. Declaring a variable means binding it to a data type.

Variables in Python

There is no declaration of variables required in Python, which makes it quite easy. It's not even possible to declare the variables. If there is need for a variable, you should think of a name and start using it as a variable.

Another remarkable aspect of Python: Not only the value of a variable may change during program execution, but the type as well. You can assign an integer value to a variable, use it as an integer for a while and then assign a string to the same variable. In the following line of code, we assign the value 42 to a variable:

i = 42

The equal "=" sign in the assignment shouldn't be seen as "is equal to". It should be "read" or interpreted as "is set to", meaning in our example "the variable i is set to 42". Now we will increase the value of this variable by 1:

i = i + 1
print(i)

OUTPUT:

43

As we have said above, the type of a variable can change during the execution of a script. Or, to be precise, a new object, which can be of any type, will be assigned to it. We illustrate this in our following example:

i = 42          # data type is implicitly set to integer
i = 42 + 0.11       # data type is changed to float
i = "forty"     # and now it will be a string 

When Python executes an assignment like "i = 42", it evaluates the right side of the assignment and recognizes that it corresponds to the integer number 42. It creates an object of the integer class to save this data. If you want to have a better understanding of objects and classes, you can but you don't have to start with our chapter on Object-Oriented Programming.

In other words, Python automatically takes care of the physical representation for the different data types.

Object References

We want to take a closer look at variables now. Python variables are references to objects, but the actual data is contained in the objects:

Python Variable

As variables are pointing to objects and objects can be of arbitrary data types, variables cannot have types associated with them. This is a huge difference from C, C++ or Java, where a variable is associated with a fixed data type. This association can't be changed as long as the program is running.

Therefore it is possible to write code like the following in Python:

x = 42
print(x)

OUTPUT:

42
x = "Now x references a string"
print(x)

OUTPUT:

Now x references a string

We want to demonstrate something else now. Let's look at the following code:

x = 42
y = x

We created an integer object 42 and assigned it to the variable x. After this we assigned x to the variable y. This means that both variables reference the same object. The following picture illustrates this:

Two Python Variables referencing the same object

What will happen when we execute

y = 78

after the previous code?

Python will create a new integer object with the content 78 and then the variable y will reference this newly created object, as we can see in the following picture:

Assigning a new object to an existing Python Variable

Most probably, we will see further changes to the variables in the flow of our program. There might be, for example, a string assignment to the variable x. The previously integer object "42" will be orphaned after this assignment. It will be removed by Python, because no other variable is referencing it.

Python Variable referencing string object

You may ask yourself, how can we see or prove that x and y really reference the same object after the assignment y = x of our previous example?

The identity function id() can be used for this purpose. Every instance (object or variable) has an identity, i.e., an integer which is unique within the script or program, i.e., other objects have different identities. So, let's have a look at our previous example and how the identities will change:

x = 42
id(x)

OUTPUT:

140709828470448
y = x
id(x), id(y)

OUTPUT:

(140709828470448, 140709828470448)
y = 78
id(x), id(y)

OUTPUT:

(140709828470448, 140709828471600)

Valid Variable Names

The naming of variables follows the more general concept of an identifier. A Python identifier is a name used to identify a variable, function, class, module or other object.

A variable name and an identifier can consist of the uppercase letters "A" through "Z", the lowercase letters "a" through "z", the underscore _ and, except for the first character, the digits 0 through 9. Python 3.x is based on Unicode. That is, variable names and identifier names can additionally contain Unicode characters as well.

Identifiers are unlimited in length. Case is significant. The fact that identifier names are case-sensitive can cause problems to some Windows users, where file names are case-insensitive, for example.

Exceptions from the rules above are the special Python keywords, as they are described in the following paragraph.

The following variable definitions are all valid:

height = 10
maximum_height = 100
υψος = 10
μεγιστη_υψος = 100
MinimumHeight = 1

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

Upcoming online Courses

Python Course for Beginners

19 May to 23 May 2025
02 Jun to 06 Jun 2025
30 Jun to 04 Jul 2025
11 Aug to 15 Aug 2025
22 Sep to 26 Sep 2025
03 Nov to 07 Nov 2025

Python Intensive Course

23 Jun to 27 Jun 2025
28 Jul to 01 Aug 2025
08 Sep to 12 Sep 2025
20 Oct to 24 Oct 2025

Efficient Data Analysis with Pandas

02 Jun to 03 Jun 2025
23 Jun to 24 Jun 2025
28 Jul to 29 Jul 2025
08 Sep to 09 Sep 2025
20 Oct to 21 Oct 2025

Machine Learning from Data Preparation to Deep Learning

02 Jun to 06 Jun 2025
28 Jul to 01 Aug 2025
08 Sep to 12 Sep 2025
20 Oct to 24 Oct 2025

See our Python training courses

See our Machine Learning with Python training courses

Python Keywords

No identifier can have the same name as one of the Python keywords, although they are obeying the above naming conventions:

and, as, assert, break, class, continue, def, del, elif, else,
except, False, finally, for, from, global, if, import, in, is, 
lambda, None, nonlocal, not, or, pass, raise, return, True, try, 
while, with, yield 

There is no need to learn them by heart. You can get the list of Python keywords in the interactive shell by using help. You type help() in the interactive, but please don't forget the parenthesis:

help()
Welcome to Python 3.4's help utility!
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.4/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
help> 

What you see now is the help prompt, which allows you to query help on lots of things, especially on "keywords" as well:

help> keywords
Here is a list of the Python keywords.  Enter any keyword to get more help.
False               def                 if                  raise
None                del                 import              return
True                elif                in                  try
and                 else                is                  while
as                  except              lambda              with
assert              finally             nonlocal            yield
break               for                 not                 
class               from                or                  
continue            global              pass    
help> 

Naming Conventions

We saw in our chapter on "Valid Variable Names" that we sometimes need names which consist of more than one word. We used, for example, the name "maximum_height". The underscore functioned as a word separator, because blanks are not allowed in variable names. Some people prefer to write variable names in the so-called CamelCase notation. We defined the variable MinimumHeight in this naming style.

There is a permanent "war" going on between the camel case followers and the underscore lovers. Personally, I definitely prefer "the_natural_way_of_naming_things" to "TheNaturalWayOfNamingThings". I think that the first one is more readable and looks more natural language like. In other words: CamelCase words are harder to read than their underscore counterparts, EspeciallyIfTheyAreVeryLong. This is my personal opinion shared by many other programmers but definitely not everybody. The Style Guide for Python Code recommends underscore notation for variable names as well as function names.

Certain names should be avoided for variable names: Never use the characters 'l' (lowercase letter "L"), 'O' ("O" like in "Ontario"), or 'I' (like in "Indiana") as single character variable names. They should be avoided, because these characters are indistinguishable from the numerals one and zero in some fonts. When tempted to use 'l', use 'L' instead, if you cannot think of a better name anyway. The Style Guide has to say the following about the naming of identifiers in standard modules: "All identifiers in the Python standard library MUST use ASCII-only identifiers, and SHOULD use English words wherever feasible (in many cases, abbreviations and technical terms are used which aren't English). In addition, string literals and comments must also be in ASCII. The only exceptions are (a) test cases testing the non-ASCII features, and (b) names of authors. Authors whose names are not based on the latin alphabet MUST provide a latin transliteration of their names."

Companies, institutes, organizations or open source projects aiming at an international audience should adopt a similar notation convention!

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See our Python training courses

See our Machine Learning with Python training courses

Changing Data Types and Storage Locations

Programming means data processing. Data in a Python program is represented by objects. These objects can be

•built-in, i.e., objects provided by Python, 
•objects from extension libraries,
•created in the application by the programmer.

So we have different "kinds" of objects for different data types. We will have a look at the different built-in data types in Python.

Numbers

Python's built-in core data types are in some cases also called object types. There are four built-in data types for numbers:

•Integer
    ∙Normal integers
    e.g., 4321
    ∙Octal literals (base 8)
    A number prefixed by 0o (zero and a lowercase "o" or uppercase "O") will be interpreted as an octal number
    example:
a = 0o10
print(a)        

OUTPUT:

8
    ∙Hexadecimal literals (base 16)
    Hexadecimal literals have to be prefixed either by "0x" or "0X".
    example:      
hex_number = 0xA0F
print(hex_number)        

OUTPUT:

2575
    ∙Binary literals (base 2)
    Binary literals can easily be written as well. They have to be prefixed by a leading "0", followed by a "b" or "B":
x = 0b101010
x      

OUTPUT:

42

The functions hex, bin, oct can be used to convert an integer number into the corresponding string representation of the integer number:

x = hex(19)
x    

OUTPUT:

'0x13'
type(x)

OUTPUT:

str
x = bin(65) 
x

OUTPUT:

'0b1000001'
x = oct(65)
x    

OUTPUT:

'0o101'
oct(0b101101)

OUTPUT:

'0o55'

Integers in Python3 can be of unlimited size

x = 787366098712738903245678234782358292837498729182728
print(x)

OUTPUT:

787366098712738903245678234782358292837498729182728
x * x * x   

OUTPUT:

488123970070638215986770162105731315538827586091948617997871122950228891123960901918308618286311523282239313708275589787123005317148968569797875581092352
•Long integers

Python 2 has two integer types: int and long. There is no "long int" in Python3 anymore. There is only one "int" type, which contains both "int" and "long" from Python2. That's why the following code fails in Python 3:

    1L
  File "<stdin>", line 1
    1L
     ^

OUTPUT:

  File "<ipython-input-27-315518d611d8>", line 1
    1L
     ^
SyntaxError: invalid syntax
x = 43
long(x)

OUTPUT:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-49-bad7871d6042> in <module>
      1 x = 43
----> 2long(x)
NameError: name 'long' is not defined
•Floating-point numbers

For example: 42.11, 3.1415e-10

•Complex numbers

Complex numbers are written as

<real part> + <imaginary part>

Examples:

x = 3 + 4j
y = 2 - 3j
z = x + y
print(z)    

OUTPUT:

(5+1j)

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See our Python training courses

See our Machine Learning with Python training courses

Integer Division

There are two kinds of division operators:

•"true division" performed by "/"
•"floor division" performed by "//" 

True Division

True division uses the slash (/) character as the operator sign. Most probably it is what you expect "division" to be. The following examples are hopefully self-explanatory:

10 / 3

OUTPUT:

3.3333333333333335
10.0 / 3.0

OUTPUT:

3.3333333333333335
10.5 / 3.5

OUTPUT:

3.0

Floor Division

The operator "//" performs floor division, i.e., the dividend is divided by the divisor - like in true division - but the floor of the result will be returned. The floor is the largest integer number smaller than the result of the true division. This number will be turned into a float, if either the dividend or the divisor or both are float values. If both are integers, the result will be an integer as well. In other words, "//" always truncates towards negative infinity.

Connection to the floor function: In mathematics and computer science, the floor function is the function that takes as input a real number x and returns the greatest integer floor ( x ) = ⌊ x ⌋ that is less than or equal to x.

If you are confused now by this rather mathematical and theoretical definition, the following examples will hopefully clarifiy the matter:

9 // 3

OUTPUT:

3
10 // 3

OUTPUT:

3
11 // 3

OUTPUT:

3
12 // 3

OUTPUT:

4
10.0 // 3

OUTPUT:

3.0
-7 // 3

OUTPUT:

-3
-7.0 // 3

OUTPUT:

-3.0

Strings