Basic Python data types
From Wikiversity
Contents |
[edit] You have the control
When programming you have the control. The computer is going to do what you tell it to do. If the program doesn't work it will continue to not work every time you run it until you fix it. This is the consistency of programming. Programming copes with changes and you can easily change programs to do something else.
[edit] Types
Types are a category for things within Python with which Python will work. Types are integers, strings, tuples, lists, and dictionaries. Type the following into the shell:
[edit] Opening IDLE
First open your IDLE and then open your Shell. To open the shell look through the menu bar, some have it under windows, and some have their own shell list. Many also have the shortcut F5.
[edit] Integers
Integers are numeric values and can be stored, manipulated, and expressed inside variables without quotes.
Typing:
>>>23
Returns
23
Typing:
>>>-44
Returns
-44
You can also perform basic math using integers as well.
Typing
>>>45 - 19
Will return
26
[edit] Strings
Strings are a type. They are the basic unit of text in Python and all the other types except integers may contain strings.
Type the following into the shell:
>>>"I Love Python"
It will return
'I Love Python'
You can also make a variable refer to a string.
Type:
>>>x = "I Love Python"
Now Type:
>>>x
'I Love Python'
Now Type:
>>>print x
I Love Python
The print command prints the value that 'x' stands for on the screen. It removes the quotations. Whenever you type something into a type that isn't an integer, syntax (the commands that you give python, such as print), or variable (such as x just was) you need to put it into quotations. You can use 'single' or "double" quotations. If you wrote parenthesis outside of the quotations you would have made a tuple (the 'u' sounds like 'touch', not 'tooch') instead of a string. You can also call on a single element in a string. Type:
>>>print x[3]
o
Now Type:
>>>print x[9]
t
As you can see the elements are numbered starting with 0 (due to Python using Ordinals).
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| I | L | o | v | e | P | y | t | h | o | n |
[edit] Tuples
A tuple is an unchangeable sequence of values. When you typed ('I love Python') you only included one element. type:
>>>x = ("element 1", "element 2", "element 3")
When you do this you create a tuple with three elements. You can access these elements individually by typing the variable and the then inside brackets directly to the right of the variable type the number of the element to which you are referring.
Type:
>>> print x[1]
element 2
You may think that it is odd that it returned element 2 instead of element 1. The reason that it did this is because Python starts numbering at 0. element 1 = 0, element 2= 1, element 3= 2. You can also call on the elements in reverse order.
Type:
>>>print x[-1]
element 3
The reason that it returned element 3 and not element 2 is because there is no such thing as -0 so it must start numbering in reverse with -1. If you used brackets instead of parenthesis you would have made a list.
[edit] Lists
A list is a changeable sequence of data.
Type:
>>>x = ["I", "Love", "Python"]
Now type:
>>>x[2] = "Wikiversity"
Now type:
>>>print x
["I", "Love", "Wikiversity"]
It changed element number 2. The last thing we learn is dictionaries.
[edit] Dictionaries
Dictionaries contain a key and a value. { } enclose dictionaries.
Type:
>>>x = {'key1':'value1', 'key2':'value2'}
Now Type:
>>>print x
{'Key2':'value2', 'key1':'value1'}
Depending on what IDLE you are using these may be in different order. The reason for the different order is because dictionaries have no order. You cannot type x[0] and be referring to 'key1':'value1' . What you do to refer to a value is type the key.
Type:
>>>x['key1'] = 'I Love Python'
Now Type:
>>>print x
{'Key2':'value2', 'key1':'I Love Python'}
The keys stay the same but the values are changeable. You can also only have one occurrence of a key in a dictionary, but you may have the values all be the same.
Type:
>>>x = {'key':'value1', 'key':'value2'}
Now Type:
>>>print x
{'key': 'value2'}
The first key is overwritten by the second.
Type:
>>>x = {'key1':'value', 'key2':'value'}
Now Type:
>>>print x
{'key2': 'value', 'key1': 'value'}
This example shows that you can create two separate keys with the same value.
next: Python Operators