- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Python data types
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.
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. Interestingly, 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/python # 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"
The script uses a random integer generator to simulate our case.
import random
Here 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): print "We will use name John" else: print "We will use name Victoria"
We print the name. The if command works with boolean values. If the 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.
The following script shows some common values that are considered to be True or False.
#!/usr/bin/python # 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)
In fact, in the previous example, we did not have to use the bool()
function.
$ ./boolean.py True False True False True False False True False
This is the output of the boolean.py
script.
None
There is another special data type - None
. Basically, the data type means non existent, not known or empty.
#!/usr/bin/python # None.py def function(): pass print function()
In our example, we define a function. 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
This is the output of the None.py
script.
Numbers
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/python # 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"
In our script, we count the total amount of apples. We use the multiplication operation.
$ ./apples.py There are total of 384 apples
The output of the script.
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/python # 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"
To get the speed, we divide the distance by the time.
print "The average speed of" \ " a sprinter is " , speed, "km/h"
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.
$ ./sprinter.py The average speed of a sprinter is 36.4741641337 km/h
This is the output of the sprinter script. Value 36.4741641337 is a floating point number.
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 triple quotes. When we use triple quotes, strings can span several lines without using the escape character.
#!/usr/bin/python # strings.py a = "proximity alert" b = 'evacuation' c = """ requiem for a tower """ print a print b print c
In our example we assign three string literals to a
, b
, and c
variables. And we print them to the console.
$ ./strings.py proximity alert evacuation requiem for a tower
This is the output of the strings.py script.
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 aaabbb
The carriage return \r
is a control character for end of line return to beginning of line.
#!/usr/bin/python # 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"
The new line is a control character, which begins a new line of text.
$ ./strophe.py 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
Next we examine the backspace control character.
print "Python\b\b\booo" # prints Pytooo
The 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 inferno
The horizontal tab puts a space between text.
"Johnie's dog" '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.
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.py Another world\n
We get the string with the new line character included.
In the next example, we will show string multiplication and concatenation.
#!/usr/bin/python # strings2.py print "eagle " * 5 print "eagle " "falcon" print "eagle " + "and " + "falcon"
The *
operator repeats 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 eagle eagle eagle eagle eagle eagle falcon eagle and falcon
This is the output of the strings.py
script.
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" print fruits # prints ('apples', 'oranges', 'bananas')
The parentheses are not mandatory. We can omit them.
#!/usr/bin/python # 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
This 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 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
This is the output of the script.
Next will will do some indexing.
#!/usr/bin/python # 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]
To 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. For instance, [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. The [:4]
gives (1, 2, 3, 4)
. It goes from the first element. The [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[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)
This is the output.
Tuples can contain several mix data types.
#!/usr/bin/python # 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]
In our example, we have put numbers, string and a tuple into the mix tuple.
$ ./mix.py mix[1] : 2 mix[2] : solaris mix[3] : (1, 2, 3) mix[3][0] : 1 mix[3][1] : 2 mix[3][2] : 3
To get the elements from the nested tuple, we use two square brackets.
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/python # onetuple.py print (3 + 7) print (3 + 7, )
In 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 10 (10,)
Output.
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/python # list.py num = [0, 2, 5, 4, 6, 7] print num[0] print num[2:] print len(num) print num + [8, 9]
As we have stated previously, we can use the same operations on lists as on tuples.
$ ./list.py 0 [5, 4, 6, 7] 6 [0, 2, 5, 4, 6, 7, 8, 9]
Output.
Next we will concentrate on additional operations we can do on lists. For example sorting.
#!/usr/bin/python # sort.py numbers = [4, 3, 6, 1, 2, 0, 5 ] print numbers numbers.sort() print numbers
In our script we have a list of numbers. To sort those numbers, we use the built-in sort()
function.
$ ./sort.py [4, 3, 6, 1, 2, 0, 5] [0, 1, 2, 3, 4, 5, 6]
The reverse()
function will sort the elements of a list in reverse order.
numbers.reverse() # [5, 4, 3, 2, 1, 0]
Counting elements in a list is done with the count()
method.
#!/usr/bin/python # 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"
The script counts number occurences in a list.
$ ./counting.py zero is here 2 times one is here 0 times two is here 1 time three is here 4 times
Next we will deal with inserting and deleting items from the list.
#!/usr/bin/python # 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
In 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 rcodeove()
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 ['Frank', 'Alexis', 'Erika', 'Ludmila'] ['Adriana', 'Frank', 'Alexis', 'Erika', 'Ludmila'] ['Adriana', 'Ludmila'] ['Ludmila']
This is the output of the modify.py
script.
There are other ways, how we can modify a list.
#!/usr/bin/python # 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
The 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 [1, 2, 3, 4, 5, 6] [11, 22, 33, 4, 5, 6] 6 [11, 22, 33, 4, 5]
Output.
In the following example, we will find out indexes of elements.
#!/usr/bin/python # index.py numbers = [0, 1, 2, 3, 3, 4, 5] print numbers.index(1) print numbers.index(3)
To 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 1 3
Output of the index.py
script.
Next we will do some transformations.
#!/usr/bin/python # tuli.py first = [1, 2, 3] second = (4, 5, 6) print tuple(first) print list(second) print first print second
We 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 (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/python # 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
In 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', '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'])
This is the output of the sets.py
script.
Next we introduce some other operations with sets.
#!/usr/bin/python # 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
The add()
method adds an item to the set. The rcodeove()
item removes an item from the set. The clear()
method removes all items from the set. The set1
is superset of set2
if every element in set2
is also in set1
. The set1
is a subset of set2
if every element in set1
is also in set2
.
$ ./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/python # 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
Our 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 Haus ['house', 'girl', 'death'] ['Haus', 'Maedchen', 'Tod'] [('house', 'Haus'), ('girl', 'Maedchen'), ('death', 'Tod')] Maedchen {'house': 'Haus', 'death': 'Tod'} {}
In this part of the Python tutorial, we have described Python data types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论