4. What is Python or What
the hell I am doing here?
- Programming Language
-Easy to Read
-Productive ( ha ha, Java programmer!!)
-Portable (.net anyone?)
-Big Library ( We got all type of wheels!)
...Fun
5. Created by Guido Van Rossum
I am in the same
picture, awesome!!
This is Guido
9. Python Building Blocks
A language is made by Statements
An statement is an order of what You want the
computer to do.
Examples:
Print in the screen the name of the user
Create a new windows
Calculate age
Show Image
10. Python Building Blocks
A language is made by Statements
An statement is an order of what You want the computer to do.
Examples:
11. Python Building Blocks
Variable
We have to keep track of a lot of information.
Examples:
"Engineer"
01/27/2013
example@example.com
12,0000
12. Python Building Blocks
Variable
Reserved Words.
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
finally is return continue
def for lambda try
18. Python Building Blocks
Object
Int ex.1234 long ex. 35L float1234 35L 3.1415
bool True False
str
"Hello World" , 'simply "hello" ' """ Complex strings"""
list
dict
tuple
college = [ "awful teachers", "boring books", "some partying"]
words_said_by_women_meaning= { "best_friend" : " You lose your chance"}
(1, 'other item', 54, 'U')
19. Python Building Blocks -Int
Int ex.1234 long ex. 35L float1234 35L 3.1415
Naturals Complex
Numbers ( a + bj)
Real numbers
(a+bj)
Basic operations
+,-,*,/ , %, **
Compare
=, <, >, !=
Division by integers
Division by zero
26. Python Building Blocks -String
Some useful methods:
str.capitalize()
str.count(sub[, start[, end]])
str.find(sub, [,start[, end]])
str.index(sub, [,start[, end]])
str.replace(old, mes[,count])
str.lower() / str.upper()
str.join(iterable)
str.split([sep[,maxsplit]])
Pretty function
len(str)
27. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
28. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
training = [ ]
Training
29. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
training = [ ]
training.append(50)
training.append(60)
training.append("Mouse")
[0]
[1]
[2]
Training
50
"Mouse"
60
30. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
training = [ ]
training.append(50)
training.append(60)
training.append("Mouse")
[0]
[1]
[2]
Training
50
"Mouse"
60
Index
31. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
training = [ ]
training.append(50)
training.append(60)
training.append("Mouse")
Another way of creating it is:
training = [ 50, 60, "Mouse"]
[0]
[1]
[2]
Training
50
"Mouse"
60
32. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
training.extend([3])
[0]
[1]
[2]
[3]
Training
50
"Mouse"
60
3
33. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
training.append(50)
[0]
[1]
[2]
[3]
[4]
Training
50
"Mouse"
60
3
50
34. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
training.remove(50)
[0]
[1]
[2]
[3]
Training
50
"Mouse"
60
3
35. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
training.remove(50)
[0]
[1]
[2]
[3]
Training
50
"Mouse"
60
3
36. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
training.pop()
[0]
[1]
[2]
Training
50
"Mouse"
60
3
37. Python Building Blocks -Lists
A list if a basic structure on python, is defined as :
list = [ n1
, n2
, .... nn
] , where N is any basic python data
structure
training.pop()
[0]
[1]
[2]
Training
50
"Mouse"
60
3
38. Python Building Blocks -Lists
We treated:
list.append(x)*
list.extend(L)*
list.remove(x)*
Other Important functions:
list.insert(i, x)*
list.pop([i])*
list.count(x)*
list.sort()
list.reverse()
*Notes : x: element, i : index, l: list
39. Python Building Blocks -Lists
We treated:
list.append(x)*
list.extend(L)*
list.remove(x)*
Other Important functions:
list.insert(i, x)*
list.pop([i])*
list.count(x)*
list.sort()
list.reverse()
*Notes : x: element, i : index, l: list
40. Python Building Blocks -Tuples
Remember the lists, they are the same but You can't modify
them. They are immutable.
41. Python Building Blocks -Tuples
Remember the lists, they are the same but You
can't modify them. They are immutable.
Let's have the immutability talk!
42. Python Building Blocks -Dictionaries
Also called Hash or associative Array. Is defined as a collection
of references called keys that point to a value.
dictionary = {
'key1
' : value1
,
'key2
' : value2
,
.....................
'keyN
' : valueN
,
}
43. Python Building Blocks -Dictionaries
Also called Hash or associative Array. Is defined as a collection
of references called keys that point to a value.
dictionary = {
'key1
' : value1
,
'key2
' : value2
,
.....................
'keyN
' : valueN
,
}
44. Python Building Blocks -Dictionaries
Also called Hash or associative Array. Is defined as a collection
of references called keys that point to a value.
Key
Value
45. Python Building Blocks -Dictionaries
Also called Hash or associative Array. Is defined as a collection
of references called keys that point to a value.
You can initialize a Dictionary with the following:
d = {} #empty dictionary
d = dict()#empty dictionary
d = { "hello": "World"}
d = dict(hello="world")
46. Python Building Blocks -Dictionaries
Some Useful Dictionary methods:
len(d)
d[key]
d[key] = value
del d[key]
key in d / key not in d
d.clear()
d.has_key(key)
d.keys()
d.values()
pop(key[, default])
popitem()
48. Python Functions or How to be lazy with style?
1- def goes before the function you are defining. the function name can't be name of
another python defined function.
2 - It can have multiple arguments.
4- All functions must have a return value
Functions are reusable block
codes.
def function_name(argument1
, argument2
,.. .., argumentn
):
code block
return result
1 2
3
4
50. Control Structures, What If...?
Conditionals
if condition :
do something
If you have more than one option else comes handy
if condition :
do something
else:
do something else
51. Control Structures, What If...?
Conditionals
When if and else is not enough, maybe you need
elif
if condition :
do something
elif:
do something different
else:
do something else
52. Control Structures, What are you waiting for?
Conditionals
The for is one of the most used control structures
example 1:
for i in structure :
do something with i block
example 2:
for i in range(n):
do something with i block
53. Control Structures, While We wait...
Conditionals
The while is another control structure used
example 1:
variable = value
while i has condition :
do something with i block
54. Control Structures, Other things
Somethings of interest
Continue:
jumps one step
Break:
break the loop
55. List Comprehension (optional)
Somethings of interest
[ F(x) for x in object/range + conditional]
[F(x1
, x2
, ... xn
) for (x1
, x2
, ... xn
) in object/range(x1
, x2
, ... xn
) +
conditional(x1
, x2
, ... xn
)]
56. Tuple Comprehension
Somethings of interest
tuple([ F(x) for x in object/range + conditional])
tuple([F(x1
, x2
, ... xn
) for (x1
, x2
, ... xn
) in object/range(x1
, x2
, ...
xn
) + conditional(x1
, x2
, ... xn
)])
57. Tuple Comprehension
Somethings of interest
tuple([ F(x) for x in object/range + conditional])
tuple([F(x1
, x2
, ... xn
) for (x1
, x2
, ... xn
) in object/range(x1
, x2
, ...
xn
) + conditional(x1
, x2
, ... xn
)])
58. Dictionary Comprehension
Somethings of interest
{ F(x): G(x) for x in object/range + conditional}
{F(x1
, x2
, ... xn
):G(x1
, x2
, ... xn
) for (x1
, x2
, ... xn
) in object/range
(x1
, x2
, ... xn
) + conditional(x1
, x2
, ... xn
)}
60. The Files examples
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace