In this part of the Python programming tutorial, we will talk about Python data types.
Computer programs work with data. Spreadsheets, text editors, calculators or chat clients. Tools to work with various data types are essential part of a modern computer language. According to the wikipedia definition, a data type is a set of values, and the allowable operations on those values.
Python has a great set of useful data types. Python's data types are built in the core of the language. They are easy to use and straightforward.
Happy parents are waiting a child to be born. They have chosen a name for both possibilities. If it is going to be a boy, they might have chosen John. If it is going to be a girl, they might have chosen Victoria.
The following script shows some common values that are considered to be True or False.
In Python programming language, we have integer numbers, floating point numbers and complex numbers.
If we work with integers, we deal with discrete entities. We would use integers to count apples.
Floating point numbers represent real numbers in computing. Real numbers measure continuous quantities. Let's say a sprinter for 100m ran 9.87s. What is his speed in km/h?
Strings in python can be created using single quotes, double quotes and tripple quotes. When we use tripple quotes, strings can span several lines without using the escape character.
When we work with strings, we can use escape sequences. The escape sequences are special characters, that have a specific purpose, when used within a string.
If we append an r to the string, we get a raw string. The escape sequences are not interpreted.
In the next example, we will show string multiplication and concatenation.
Python programming language has several built-in data types for working with collections of values. These are currently tuples, lists, sets and dictionaries.
Next will will do some indexing.
Tuples can contain several mix data types.
We have an exception when we work with tuples containing one element. Parentheses are also used in expressions. How do we distinguish between an expression and a one element tuple? The creators of the python programming language decided to use a comma to denote, that we are using a tuple.
Next we will concentrate on additional operations we can do on lists. For example sorting.
There are other ways, how we can modify a list.
In the following example, we will find out indexes of elements.
Next we will do some transformations.
Next we introduce some other operations with sets.
Computer programs work with data. Spreadsheets, text editors, calculators or chat clients. Tools to work with various data types are essential part of a modern computer language. According to the wikipedia definition, a data type is a set of values, and the allowable operations on those values.
Python has a great set of useful data types. Python's data types are built in the core of the language. They are easy to use and straightforward.
Boolean values
There is a duality built in our world. There is a Heaven and Earth, water and fire, jing and jang, man and woman, love and hatred. In python programming language, the Boolean datatype is a primitive datatype having one of two values: True or False. This is a fundamental data type. Very common in computer programs. Interesingly, this data type was not there from the beginning, but it was created later on.Happy parents are waiting a child to be born. They have chosen a name for both possibilities. If it is going to be a boy, they might have chosen John. If it is going to be a girl, they might have chosen Victoria.
#!/usr/bin/pythonThe script uses a random integer generator to simulate our case.
# kid.py
import random
male = False
male = bool(random.randint(0, 1))
if (male):
print "We will use name John"
else:
print "We will use name Victoria"
import randomHere we import the random module, that is used to calculate random numbers.
male = bool(random.randint(0, 1))Here we use two functions. the randint() function returns a random number from the given integer boundaries. In our case 0 or 1. The bool() function converts the integers to boolean values.
if (male):We print the name. The if command works with boolean values. If the variale male is True, we print the "We will use name John" to the console. If it has a False value, we print the other string.
print "We will use name John"
else:
print "We will use name Victoria"
The following script shows some common values that are considered to be True or False.
#!/usr/bin/pythonIn fact, in the previous example, we did not have to use the bool() function.
# boolean.py
print bool(True)
print bool(False)
print bool("text")
print bool("")
print bool(' ')
print bool(0)
print bool()
print bool(3)
print bool(None)
$ ./boolean.pyThis is the output of the boolean.py script.
True
False
True
False
True
False
False
True
False
None
There is another special data type - None. Basically, the data type means non existent, not known or empty.#!/usr/bin/pythonIn our example, we define a funtion. Functions will be covered later in the tutorial. The function does nothing. It does not explicitly return any value. Such a function will implicitly return None object.
# None.py
def function():
pass
print function()
NoneThe output of the None.py script.
Numbers
I am not going to define what a number is. This is quite a philosophical question. Ask Pythagoras. I assume, we all know or have some notion what a number is.In Python programming language, we have integer numbers, floating point numbers and complex numbers.
If we work with integers, we deal with discrete entities. We would use integers to count apples.
#!/usr/bin/pythonIn our script, we count the total amount of apples. We use the multiplication operation.
# apples.py
# number of baskets
baskets = 16
# number of apples in a basket
apples_in_basket = 24
# we get the total number of apples
total = baskets * apples_in_basket
print "There are total of", total, "apples"
$ ./apples.pyThe output of the script.
There are total of 384 apples
Floating point numbers represent real numbers in computing. Real numbers measure continuous quantities. Let's say a sprinter for 100m ran 9.87s. What is his speed in km/h?
#!/usr/bin/pythonTo get the speed, we divide the distance by the time.
# sprinter.py
# 100m is 0.1 km
distance = 0.1
# 9.87s is 9.87/60*60 h
time = 9.87 / 3600
speed = distance / time
print "The average speed of" \
" a sprinter is " , speed, "km/h"
print "The average speed of" \The \ character is called an escape character. The escape character changes the meaning of the new line character. We see the code in two lines, but the interpreter drops the new line character, and sees only one line. Without the escape character, the interpreter would complain about indentation.
" a sprinter is " , speed, "km/h"
$ ./sprinter.pyThis is the output of the sprinter script. 36.4741641337 is a floating point number.
The average speed of a sprinter is 36.4741641337 km/h
Strings
String is a data type representing textual data in computer programs. Probable the single most important data type in programming.Strings in python can be created using single quotes, double quotes and tripple quotes. When we use tripple quotes, strings can span several lines without using the escape character.
#!/usr/bin/pythonIn our example we assign three string literals to a, b, c variables. And we print them to the console.
# strings.py
a = "proximity alert"
b = 'evacuation'
c = """
requiem
for
a
tower
"""
print a
print b
print c
$ ./strings.pyThis is the output of the strings.py script.
proximity alert
evacuation
requiem
for
a
tower
When we work with strings, we can use escape sequences. The escape sequences are special characters, that have a specific purpose, when used within a string.
print " bbb\raaa" # prints aaabbbThe carriage return \r is a control character for end of line return to beginning of line.
#!/usr/bin/pythonThe new line is a control character, which begins a new line of text.
# strophe.py
print "Incompatible, it don't matter though\n'cos someone's bound to hear my cry"
print "Speak out if you do\nYou're not easy to find"
$ ./strophe.pyNext we examine the backspace control character.
Incompatible, it don't matter though
'cos someone's bound to hear my cry
Speak out if you do
You're not easy to find
print "Python\b\b\booo" # prints PytoooThe backspace control character \b moves the cursor one character back. In our case, we use three backspace characters to delete three letters and replace them with three o characters.
print "Towering\tinferno" # prints Towering infernoThe horizontal tab puts a space between text.
"Johnie's dog"Single and double quotes can be nested. Or in case we use only single quotes, we can use the backslash to escape the default meaning of a single quote.
'Johnie\'s dog'
print "eagle has", len("eagle"), "characters"We can use the len() function to calculate the length of the string in characters.
If we append an r to the string, we get a raw string. The escape sequences are not interpreted.
#!/usr/bin/python
# raw.py
print r"Another world\n"
$ ./raw.pyWe get the string with the lew line character included.
Another world\n
In the next example, we will show string multiplication and concatenation.
#!/usr/bin/pythonThe * operation repeates the string n times. In our case five times. Two string literals next to each other are automatically concatenated. We can also use the + operator to explicitly concatenate the strings.
# strings2.py
print "eagle " * 5
print "eagle " "falcon"
print "eagle " + "and " + "falcon"
$ ./strings2.pyThis is the output of the strings.py script.
eagle eagle eagle eagle eagle
eagle falcon
eagle and falcon
Python programming language has several built-in data types for working with collections of values. These are currently tuples, lists, sets and dictionaries.
Tuples
A tuple is an immutable sequence data type. The tuple can contain mixed data types.fruits = ("oranges", "apples", "bananas")Tuples are created using round brackets. Here we have a tuple consisting of three fruit types.
fruits = "apples", "oranges", "bananas"The parentheses are not mandatory. We can omit them.
print fruits # prints ('apples', 'oranges', 'bananas')
#!/usr/bin/pythonThis example shows several basic operations with tuples. The len() function returns the number of elements in the first tuple. The max() function returns the maximum value, the min() minimum value. The addition operator adds two tuples, the multiplication operator multiplies the tuple. The in operator determines if the values is in the tuple.
# tuples.py
first = (1, 2, 3)
second = (4, 5, 6)
print "len(first) : ", len(first)
print "max(first) : ", max(first)
print "min(first) : ", min(first)
print "first + second :", first + second
print "first * 3 : ", first * 3
print "1 in first : ", 1 in first
print "5 not in second : ", 5 not in second
$ ./tuples.pyThis is the output of the script.
len(first) : 3
max(first) : 3
min(first) : 1
first + second : (1, 2, 3, 4, 5, 6)
first * 3 : (1, 2, 3, 1, 2, 3, 1, 2, 3)
1 in first : True
5 not in second : False
Next will will do some indexing.
#!/usr/bin/pythonTo get a value from a tuple, we use square brackets []. Note that we count indexes from 0. If there are five objects in a tuple, the indexes are 0...4. If we use a negative index, we get a value from the end of the tuple. So index -1 gets the last element, -2 gets the last but one element. Python enables to create slices from tuples. For this we use the : delimiter. [0:4] gives (1, 2, 3, 4). Note that the last element is not included. We can omit one or both indexes in a slice. It [:4] gives (1, 2, 3, 4). It goes from the first element. [0:] gives (1, 2, 3, 4, 5). This time, the last element is included. If we go out of bounds, we simply get all elements in the tuple.
# tuples2.py
five = (1, 2, 3, 4, 5)
print "five[0] : ", five[0]
print "five[-1] : ", five[-1]
print "five[-2] : ", five[-2]
print "five[:] : ", five[:]
print "five[0:4] : ", five[0:4]
print "five[1:2] : ", five[1:2]
print "five[:2] : ", five[:2]
print "five[:-1] : ", five[:-1]
print "five[:9] : ", five[:9]
$ ./tuples2.pyThe output.
five[0] : 1
five[-1] : 5
five[-2] : 4
five[:] : (1, 2, 3, 4, 5)
five[0:4] : (1, 2, 3, 4)
five[1:2] : (2,)
five[:2] : (1, 2)
five[:-1] : (1, 2, 3, 4)
five[:9] : (1, 2, 3, 4, 5)
Tuples can contain several mix data types.
#!/usr/bin/pythonIn our example, we have put numbers, string and a tuple into the mix tuple.
# mix.py
mix = (1, 2, "solaris", (1, 2, 3))
print "mix[1] :", mix[1]
print "mix[2] :", mix[2]
print "mix[3] :", mix[3]
print "mix[3][0] :", mix[3][0]
print "mix[3][1] :", mix[3][1]
print "mix[3][2] :", mix[3][2]
$ ./mix.pyTo get the elements from the nested tuple, we use two square brackets.
mix[1] : 2
mix[2] : solaris
mix[3] : (1, 2, 3)
mix[3][0] : 1
mix[3][1] : 2
mix[3][2] : 3
We have an exception when we work with tuples containing one element. Parentheses are also used in expressions. How do we distinguish between an expression and a one element tuple? The creators of the python programming language decided to use a comma to denote, that we are using a tuple.
#!/usr/bin/pythonIn the first case we have and expression. We print number 10 to the console. In the second case we deal with a tuple. We print a tuple containing number 10.
# onetuple.py
print (3 + 7)
print (3 + 7, )
$ ./onetuple.pyOutput.
10
(10,)
Lists
A list is a mutable sequence data type. The list can contain mixed data types. A list and a tuple share many common features. Because a list is a modifiable data type, it has some additional operations. A whole chapter is dedicated to the Python list.actors = ["Jack Nicholson", "Antony Hopkins", "Adrien Brody"]The list is created using the square brackests [].
#!/usr/bin/pythonAs we have stated previously, we can use the same operations on lists as on tuples.
# list.py
num = [0, 2, 5, 4, 6, 7]
print num[0]
print num[2:]
print len(num)
print num + [8, 9]
$ ./list.pyOutput.
0
[5, 4, 6, 7]
6
[0, 2, 5, 4, 6, 7, 8, 9]
Next we will concentrate on additional operations we can do on lists. For example sorting.
#!/usr/bin/pythonIn our script we have a list of numbers. To sort those numbers, we use the built-in sort() function.
# sort.py
numbers = [4, 3, 6, 1, 2, 0, 5 ]
print numbers
numbers.sort()
print numbers
$ ./sort.pyThe reverse() function will sort the elements of a list in reverse order.
[4, 3, 6, 1, 2, 0, 5]
[0, 1, 2, 3, 4, 5, 6]
numbers.reverse() # [5, 4, 3, 2, 1, 0]Counting elements in a list is done with the count() method.
#!/usr/bin/pythonThe script counts number occurences in a list.
# counting.py
numbers = [0, 0, 2, 3, 3, 3, 3]
print "zero is here ", numbers.count(0), "times"
print "one is here ", numbers.count(1), "times"
print "two is here ", numbers.count(2), "time"
print "three is here ", numbers.count(3), "times"
$ ./counting.pyNext we will deal with inserting and deleting items from the list.
zero is here 2 times
one is here 0 times
two is here 1 time
three is here 4 times
#!/usr/bin/pythonIn our example we first create an empty names list. We use the append() function to append new items to the list. The elements are appended in the consecutive way. The insert() function inserts new elements at a given position. The existing elements are not deleted, they are moved. The remove() function removes a specific item from the list. If we want to delete an item based on the index, we use the del keyword.
# modify.py
names = []
names.append("Frank")
names.append("Alexis")
names.append("Erika")
names.append("Ludmila")
print names
names.insert(0, "Adriana")
print names
names.remove("Frank")
names.remove("Alexis")
del names[1]
print names
del names[0]
print names
$ ./modify.pyThis is the output of the modify.py script.
['Frank', 'Alexis', 'Erika', 'Ludmila']
['Adriana', 'Frank', 'Alexis', 'Erika', 'Ludmila']
['Adriana', 'Ludmila']
['Ludmila']
There are other ways, how we can modify a list.
#!/usr/bin/pythonThe extend() method appends a whole list to another list. To modify an element in a list, we can use the assignment operator. The pop() method takes an item from the list and returns it.
# modify2.py
first = [1, 2, 3]
second = [4, 5, 6]
first.extend(second)
print first
first[0] = 11
first[1] = 22
first[2] = 33
print first
print first.pop(5)
print first
$ ./modify2.pyOutput.
[1, 2, 3, 4, 5, 6]
[11, 22, 33, 4, 5, 6]
6
[11, 22, 33, 4, 5]
In the following example, we will find out indexes of elements.
#!/usr/bin/pythonTo find an index in a list, we use the index() method. If there are more occurences of an element, the method returns the index of the first element.
# index.py
numbers = [0, 1, 2, 3, 3, 4, 5]
print numbers.index(1)
print numbers.index(3)
$ ./index.pyOutput of the index.py script.
1
3
Next we will do some transformations.
#!/usr/bin/pythonWe can use tuple() function to create a tuple from a list and list()function to create a list from a tuple. Note, that the original objects are not modified, the functions only return those transformed collections.
# tuli.py
first = [1, 2, 3]
second = (4, 5, 6)
print tuple(first)
print list(second)
print first
print second
$ ./tuli.py
(1, 2, 3)
[4, 5, 6]
[1, 2, 3]
(4, 5, 6)
Sets
A set is an unordered collection of data with no duplicate elements. A set supports operations like union, intersection or difference. Similar as in Mathematics.#!/usr/bin/pythonIn our example we have two sets. We use the set() function to create sets. The intersection operation returns elements that are both in set1 and set2. The union operation returns all elements from both sets. The difference returns elements that are in the set1 but not is set2. And finally, the symmetric difference returns elements that are in set1 or set2, but not in both.
# sets.py
set1 = set(['a', 'b', 'c', 'c', 'd'])
set2 = set(['a', 'b', 'x', 'y', 'z'])
print "set1: " , set1
print "set2: " , set2
print "intersection: ", set1 & set2
print "union: ", set1 | set2
print "difference: ", set1 - set2
print "symmetric difference: ", set1 ^ set2
$ ./sets.pyThis is the output of the sets.py script.
set1: set(['a', 'c', 'b', 'd'])
set2: set(['a', 'x', 'b', 'y', 'z'])
intersection: set(['a', 'b'])
union: set(['a', 'c', 'b', 'd', 'y', 'x', 'z'])
difference: set(['c', 'd'])
symmetric difference: set(['c', 'd', 'y', 'x', 'z'])
Next we introduce some other operations with sets.
#!/usr/bin/pythonThe add() method adds an item to the set. The remove() item removes an item from the set. The clear() method removes all items from the set. set1 is superset of set2 if every element in set2 is also in set1. set1 is a subset of set2, if every element in set1 is also in set2.
# sets2.py
set1 = set([1, 2])
set1.add(3)
set1.add(4)
set2 = set([1, 2, 3, 4, 6, 7, 8])
set2.remove(8)
print set1
print set2
print "Is set1 subset of set2 ? : ", set1.issubset(set2)
print "Is set1 superset of set2 ? : ", set1.issuperset(set2)
set1.clear()
print set1
$ ./sets2.py
set([1, 2, 3, 4])
set([1, 2, 3, 4, 6, 7])
Is set1 subset of set2 ? : True
Is set1 superset of set2 ? : False
set([])
fs = frozenset(['a', 'b', 'c'])If we need an immutable set, we create a frozenset
Dictionaries
A Python dictionary is a group of key-value pairs. The elements in a dictionary are indexed by keys. Keys in a dictionary are required to be unique. Because of the importance of the dictionary data type, a whole chapter covers the dictionary in this Python tutorial.#!/usr/bin/pythonOur first example shows some basic usage of the dictionary data type. We print a specific value, keys and values of the dictionary. The items() method returns a list of dictionarie's (key, value) pairs as tuples.
# dict.py
words = { 'girl': 'Maedchen', 'house': 'Haus', 'death': 'Tod' }
print words['house']
print words.keys()
print words.values()
print words.items()
print words.pop('girl')
print words
words.clear()
print words
$ ./dict.pyIn this part of the Python tutorial, we have described Python data types.
Haus
['house', 'girl', 'death']
['Haus', 'Maedchen', 'Tod']
[('house', 'Haus'), ('girl', 'Maedchen'), ('death', 'Tod')]
Maedchen
{'house': 'Haus', 'death': 'Tod'}
{}
0 comments:
Post a Comment