Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Learning programming with Python, part II

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

[Tutorial] Learning programming with Python, part II

Post by Acid_Snake » Sat May 26, 2012 11:13 am

Part II.
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()
As you can see the variable x stores a string, y stores an integer, z stores a bool.
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
The variable s has a class, imagine we have a class some_class() and inside we have a function some_function() that prints "Hello", we would need to use a variable in the class and call the function as if inside the variable. Doing this:

Code: Select all

>>> some_class().some_function()
Hello
Is the same as this:

Code: Select all

>>> s = some_class()
>>> s.some_function()
Hello
More on classes and functions later.

Now 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()]
The fist value is 0, then 1, 2, 3 and so on, we need to know the number of the value to call it:

Code: Select all

>>> print x[0]
hello
>>> print x[1]
1
>>> print x[2]
True
>>> print x[3]
<__main__.some_class instance at 0xb77d096c>
for a better knowledge of the values position it is better to use one line per value and use the number symbol (#) along with the number of the value:

Code: Select all

x = [
"hello",      #0
1,      #1
True,   #2
some_class()    #3
]
The result is the same and the readability is better.

Tuples: tuples are created similarly to lists, but work differently:

Code: Select all

>>> x = ("hello", 1, True)
>>> print x
('hello', 1, True)
You don't need to use brackets:

Code: Select all

>>> x = "hello", 1, True
>>> print x
('hello', 1, True)
Dictionaries: dictionaries are used to associate a value to another value:

Code: Select all

>>> x = {"hello":"world", "hola":"mundo"}
>>> print x["hello"]
world
>>> print x["hola"]
mundo
OK, I think that is all for now, alcohol is starting to affect my concentration :D

Previous: viewtopic.php?f=37&t=11835
Next: viewtopic.php?f=37&t=11885
Advertising

Post Reply

Return to “Programming and Security”