Python Tutorial_ a Tutorial Data Types and Variables
Python Tutorial_ a Tutorial Data Types and Variables
Home Python 2 Tutorial Python 3 Tutorial Advanced Topics Numerical Programming Machine Learning Tkinter Tutorial Contact
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:
Python automatically takes care of the physical representation for the different data types, i.e. an integer values will be stored in a different memory location than a float or a string.
In a programming
language Data Types
are a classification on
the set of all possible
data. This
classification is based Object References
on the possible We want to take a closer look on variables now. Python variables are references to objects, but the actual data is contained in the objects:
values of the types
and the operations
which can be
performed on them.
Most programming
languages come with
basic data types like
integer, real, float od
boolean. In
programming
languages like C,
C++ or Java, there is
a one-to-one
correspondence
between data types
and a varialbe name.
This is different in
Python, where a
variable name
corresponds to the
objects it references.
The module Numpy
has to take the
traditional approach
to data types, see
the chapters Data
Types and Variables"
and Data Types and
Variables" of our
numpy tutorial.
This website is
created by:
Python Training As variables are pointing to objects and objects can be of arbitrary data type, variables cannot have types associated with them. This is a huge difference to C, C++ or Java, where a variable is associated with a
Courses in Toronto, fixed data type. This association can't be changed as long as the program is running.
Canada
On site trainings in Therefore it is possible to write code like the following in Python:
Europe, Canada and
the US. >>> x = 42
>>> print(x)
42
>>> x = "Now x references a string"
>>> print(x)
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:
y = 78
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:
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.
id Function
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)
10107136
>>> y = x
>>> id(x), id(y)
(10107136, 10107136)
>>> y = 78
>>> id(x), id(y)
(10107136, 10108288)
>>>
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. This means that 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.
>>> height = 10
>>> maximum_height = 100
>>>
>>> υψος = 10
>>> μεγιστη_υψος = 100
>>>
>>> MinimumHeight = 1
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 in help() in the interactive, but please don't forget the parenthesis:
>>> help()
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".
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.
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 natation convention!
Programming means data processing. Data in a Python program is represented by objects. These objects can be
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)
8
Hexadecimal literals (base 16)
Hexadecimal literals have to be prefixed either by "0x" or "0X".
example:
>>> hex_number = 0xA0F
>>> print(hex_number)
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 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
'0x13'
>>> type(x)
<class 'str'>
>>> x = bin(65)
>>> x
'0b1000001'
>>> x = oct(65)
>>> x
'0o101'
>>> oct(0b101101)
'0o55'
>>>
>>> x = 787366098712738903245678234782358292837498729182728
>>> print(x)
787366098712738903245678234782358292837498729182728
>>> x * x * x
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. This means that the following code
fails in Python 3:
>>> 1L
File "<stdin>", line 1
1L
^
SyntaxError: invalid syntax
>>> x = 43
>>> long(x)
Traceback (most recent call last):
File "<h;stdin>", line 1, in <module>
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>j
examples:
>>> x = 3 + 4j
>>> y = 2 - 3j
>>> z = x + y
>>> print(z)
(5+1j)
Integer Division
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-explaining:
Floor Division
The operator "//" performs floor division, i.e. the divident 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 divident 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 return 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:
bernd@marvin $ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 9 // 3
3
>>> 10 // 3
3
>>> 11 // 3
3
>>> 12 // 3
4
>>> 10.0 // 3
3.0
>>> -7 // 3
-3
>>> -7.0 // 3
-3.0
>>>
Strings
The task of the first-generation computers in the forties and fifties had been - due to technical restraints - focussed on number processing. Text processing
had been just a dream that time. Nowadays, one of the main tasks of computers is text processing in all its varieties; the most prominent applications are
search engines like Google. To enable text processing programming languages need suitable data types. Strings are used in all modern programming
languages to store and process textual information. Logically, a string - like any text - is a sequence of characters. The question remains what a character
consists of. In a book, or in a text like the one your are reading now, characters consist of graphical shapes, so-called graphemes, consisting of lines, curves
and crossings in certain angles or positions and so on. The ancient Greeks associated with the word the engraving on coins or the stamps on seals.
In computer science or computer technology, a character is a unit of information. These Characters correspond to graphemes, the
fundamental units of written or printed language. Before Unicode came into usage, there was a one to one relationship between bytes and
characters, i.e. every character - of a national variant, i.e. not all the characters of the world - was represented by a single byte. Such a
character byte represents the logical concept of this character and the class of graphemes of this character. In the image on the right side,
we have depicted various representations of the letter "A", i.e. "A" in different fonts. So in printing there are various graphical
representations or different "encodings" of the abstract concept of the letter A. (By the way, the letter "A" can be ascribed to an Egyptian
hieroglyph with a pictogram of an ox.) All of these graphical representations having certain features in common. In other words, the
meaning of a character or a written or printed text doesn't depend on the font or writing style used. On a computer the capital A is encoded
in binary form. If we use ASCII it is encoded - like all the other characters - as the byte 65.
ASCII is restricted to 128 characters and "Extended ASCII" is is still limited to 256 bytes or characters. This is good enough for languages like English, German and French, but by far not sufficient for Chinese,
Japanese and Korean. That's where Unicode gets into the game. Unicode is a standard designed to represent every character from every language, i.e. it can handle any text of the world's writing systems. These
writing systems can also be used simultaneously, i.e. Roman alphabet mixed with Cyrillic or even Chinese characters.
It is a different story in Unicode. A character maps to a code point. A code point is a theoretical concept. This means, for example, that the character "A" is assigned a code point U+0041. The "U+" means "Unicode"
and the "0041" is a hexadecimal number, 65 in decimal notation.
>>> hex(65)
'0x41'
>>> int(0x41)
65
Up to four bytes are possible per character in Unicode. Theoretically, this means a huge number of 4294967296 possible characters. Due to restrictions from UTF-16 encoding, there are "only" 1,112,064 characters
possible. Unicode version 8.0 assigned. 120,737 characters. This means that there are slightly more than 10 % of all possible characters assigned, or in other words: We can still add nearly a million characters to
Unicode.
Unicode Encodings
Name Description
UTF- It's a one to one encoding, i.e. it takes each Unicode character (a 4-byte number) and stores it in 4 bytes. One advantage of this encoding is that you can find the Nth character of a string in linear time,
32 because the Nth character starts at the 4×Nth byte. A serious disadvantage of this approach is due to the fact that it needs four bytes for every character.
UTF- Hardly anybody needs more than 65535 characters, so UTF-16, which needs 2 bytes, is a more space efficient alternative to UTF-32. But it is very difficult to access characters outside the range 0 - 65535,
16 i.e. characters from the so-called "astral plane"
Another problem of both UTF-32 and UTF-16 consists in the byte ordering, which is depending on the operating system.
UTF-8 UTF8 is a variable-length encoding system for Unicode, i.e. different characters take up a different number of bytes. ASCII characters use solely one byte per character, which are even the same as the
used to be for the first 128 characters (0 127), i.e. these characters in UTF-8 are indistinguishable from ASCII. But the so-called "Extended Latin" characters like the Umlaute ä, ö and so on take up two
bytes. Chinese characters need three bytes. Finally, the very seldom used characters of the "astral plane" need four bytes to be encoded in UTF-8.
A Disadvantage of this approach is that finding the Nth character is more complex, the longer the string, the longer it takes to find a specific character.
After this lengthy but necessary introduction, we finally come to python and the way it deals with strings. All strings in Python 3 are sequences of "pure" Unicode characters, no specific encoding like UTF-8.
Both s and s2 of the previous example are variables referencing string objects. We can see, that string literals can either be enclosed in matching single (') or in double quotes ("). Single quotes will have to be
escaped with a backslash (\), if the string is defined with single quotes:
Analogously, we will have to escape a double quote inside a double quoted string:
>>> txt = "He said: \"It doesn't matter, if you enclose a string in single or double quotes!\""
>>> print(txt)
He said: "It doesn't matter, if you enclose a string in single or double quotes!"
>>>
They can also be enclosed in matching groups of three single or double quotes. In this case they are called triple-quoted strings. The backslash (\) character is used to escape characters that otherwise have a
special meaning, such as newline, backslash itself, or the quote character.
In triple-quoted strings, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the string. (A "quote" is the character used to open the string, i.e.
either ' or ".)
A string in Python consists of a series or sequence of characters - letters, numbers, and special characters. Strings can be subscripted or indexed. Similar to C, the first character of a string has the index 0.
>>> s[len(s)-1]
'd'
Yet, there is an easier way in Python. The last character can be accessed with -1, the second to last with -2 and so on:
>>> s[-1]
'd'
>>> s[-2]
'l'
>>>
Some readers might find it confusing, when we use "to subscript" as a synonym for "to index". Usually, a subscript is a number, figure, symbol, or indicator that is smaller than the normal line of type and is set
slightly below or above it. When we wrote s[0] or s[3] in the previous examples, this can be seen as an alternative way for the notation s0 or s3. So, both s3 and s[3] describe or denote the 4th character. Btw. there
exists no character type in Python. A character is simply a string of size one.
It's possible to start counting the indices from the right, as we have mentioned previously. In this case negative numbers are used, starting with -1 for the most right character.
Concatenation
Strings can be glued together (concatenated) with the + operator:
"Hello" + "World" will result in "HelloWorld"
Repetition
String can be repeated or repeatedly concatenated with the asterisk operator "*":
"*-*" * 3 will result in "*-**-**-*"
Indexing
"Python"[0] will result in "P"
Slicing
Substrings can be created with the slice or slicing notation, i.e. two indices in square brackets separated by a colon:
"Python"[2:4] will result in "th"
Size
len("Python") will result in 6
Immutable Strings
Like strings in Java and unlike C or C++, Python strings cannot be changed. Trying to change an indexed position will raise an error:
Beginners in Python are often confused, when they see the following codelines:
The variable "txt" is a reference to a string object. We define a completely new string object in the second assignment. So, you shouldn't confuse the variable name with the referenced object!
A String Peculiarity
Strings show a special effect, which we will illustrate in the following example. We will need the "is"-Operator. If both a and b are strings, "a is b" checks if they have the same identity, i.e. share the same memory
location. If "a is b" is True, then it trivially follows that "a == b" has to be True as well.
Yet, "a == b" True doesn't imply that "a is b" is True as well!
>>> a = "Linux"
>>> b = "Linux"
>>> a is b
True
Okay, but what happens, if the strings are longer? We use the longest village name in the world in the following example. It's a small village with about 3000 inhabitants in the South of the island of Anglesey in the
North-West of Wales:
>>> a = "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch"
>>> b = "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch"
>>> a is b
True
Nothing has changed to our first "Linux" example. But what works for Wales doesn't work e.g. for Baden-Württemberg in Germany:
>>> a = "Baden-Württemberg"
>>> b = "Baden-Württemberg"
>>> a is b
False
>>> a == b
True
You are right, it has nothing to do with geographical places. The special character, i.e. the hyphen, is to "blame".
>>> a = "Baden!"
>>> b = "Baden!"
>>> a is b
False
>>> a = "Baden1"
>>> b = "Baden1"
>>> a is b
True
\newline Ignored
\\ Backslash (\)
Byte Strings
Python 3.0 uses the concepts of text and (binary) data instead of Unicode strings and 8-bit strings. Every string or text in Python 3 is Unicode, but encoded Unicode is represented as binary data. The type used to
hold text is str, the type used to hold data is bytes. It's not possible to mix text and data in Python 3; it will raise TypeError.
While a string object holds a sequence of characters (in Unicode), a bytes object holds a sequence of bytes, out of the range 0 .. 255, representing the ASCII values.
Defining bytes objects and casting them into strings:
>>> x = b"Hallo"
>>> t = str(x)
>>> u = t.encode("UTF-8")
© 2011 - 2018, Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein