Ok, now that you have mastered strings and integers lets move to variables.
A variable is a name that stores different values, these values can be strings, integers, boolean, classes, lists, tuples, dictionaries and even other variables.
Let's see how to assign variables:
Code: Select all
x = "hello"
y = 1
z = True
w = x
s = some_class()
The variable w does not really store another variable, it actually becomes the same as the other variable:
Code: Select all
>>> x = "hello"
>>> w = x
>>> print x
hello
>>> print w
hello
Code: Select all
>>> some_class().some_function()
HelloCode: Select all
>>> s = some_class()
>>> s.some_function()
HelloNow onto collections.
In python there are three types of collections: tuples, lists and dictionaries.
Lists: a list is just that, a list of values given to a variable. If before we saw how to make variables with one value, here we'll learn how to give different values to one variable, is as simple as:
Code: Select all
x = ["hello", 1, True, some_class()]Code: Select all
>>> print x[0]
hello
>>> print x[1]
1
>>> print x[2]
True
>>> print x[3]
<__main__.some_class instance at 0xb77d096c>Code: Select all
x = [
"hello", #0
1, #1
True, #2
some_class() #3
]Tuples: tuples are created similarly to lists, but work differently:
Code: Select all
>>> x = ("hello", 1, True)
>>> print x
('hello', 1, True)
Code: Select all
>>> x = "hello", 1, True
>>> print x
('hello', 1, True)
Code: Select all
>>> x = {"hello":"world", "hola":"mundo"}
>>> print x["hello"]
world
>>> print x["hola"]
mundo
Previous: viewtopic.php?f=37&t=11835
Next: viewtopic.php?f=37&t=11885
Advertising