Python in 90 minutes
Bardia Heydari nejad
What is Python?
What normal people think What I think
Why Python?
• It’s multiiiiiiiiiiiiiiii purpose 😃
Web, GUI, Scripting, Data mining, Artificial intelligence ….
• It’s Readable 😊
No goddamn semicolon;
• It’s Productive 😮
10 times faster than C too produce a software
about Python
• interpreted: no more compiling no more binaries
• object-oriented: but you are not forced
• strongly-typed: explicits are better than implicit
• dynamically-typed: simple is better than complex
• cross platform: everywhere that interpreted exists
• indenting is a must: beautiful is better than ugly
• every thing is object
Hello, world!
#include<iostream>
using namespace std;
int main()
{
cout<<“Hello, world!”;
return 0;
}
C++
Hello, world!
public class Program {
public static main(string[] argus){
System.out.println(“Hello, world!”);
}
}
Java
Hello, world!
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo ‘<p>Hello, world!</p>’; ?>
</body>
</html>
PHP
Hello, world!
print “Hello, world!”
Python
Operator
• +
• -
• *
• /
• //
• **
• %
• ==
• !=
• >
• <
• >=
• <=
• +=
• -=
• *=
• /=
• **=
• //=
• &
• |
• ^
• ~
• >>
• <<
• and
• or
• not
• in
• is
Variables
•int i = 1
•float f = 2.1
•bool b = False
•str s = ‘hi’
•list l = [1, 2, “hello again”, 4]
•tuple t = (True, 2.1)
•dict d = {1:”One”, 2:”Two”}
•set s = {“some”, “unique”, “objects”}
Variables - Assignment
>>> dummy_variable = 2
>>> dummy_variable = ‘hi’
>>> a, b = 2, 3
>>> a, b = b, a
int
main()
{
int
a
=
2,
b
=
3
,
k;
k
=
a;
a
=
b;
b
=
a;
return
0;
}
in c++
Variables - Casting
>>> float(1)
1.0
>>> str(1)
‘1’
>>> int(‘1’)
1
>>> list(1)
[1, ]
>>> list(‘hello’)
['h', 'e', 'l', 'l', ‘o']
>>> set([1, 0, 1, 1, 0, 1, 0, 0])
set([1, 0])
String
• concatenate
• slice
• find
• replace
• count
• capitalize
List
• slice
• concatenate
• sort
• append - pop - remove - insert
Condition
• if
• elif
• else
Loop
• while
• for
• else
Function
• Keyword arguments
• Default arguments
• Functions are objects
Question?
Object oriented
• Event classes are object
• Dynamically add attributes
• Static methods and attributes
• Property
Beyond OO
reflection/introspection
• type(obj)
• isinstance(obj, class)
• issubclass(class, class)
• getattr(obj, key) / hastattr(obj, key)
First class functions
• function assignment
• send function as parameter
• callable objects
Strategy pattern


class Player: 

def __init__(self, play_behavior): 

self.play_behavior = play_behavior 



def play(self): 

return self.play_behavior 





def attack(): 

print("I attack”) 



attacker = Player(attack) .
Closure pattern
• function in function
def add(x): 

def add2(y): 

return x + y 

return add2
or with lambda
def add(x): 

return lambda y: x+y
• callable
class Add: 

def __init__(self, x): 

self.x = x 



def __call__(self, y): 

return self.x + y 

Generator pattern
• subroutine vs co-routine


def get_primes(number): 

while True: 

if is_prime(number): 

yield number 

number += 1




def solve_number_10(): 

total = 0 

for next_prime in get_primes(2): 

if next_prime < 2000000: 

total += next_prime 

else: 

break 

print(total)
Decorator pattern
def register_function(f): 

print("%s function is registered" % f.__name__) 

return f 





@register_function 

def do_dummy_stuff(): 

return 1


Decorator pattern
add some tricks
def log(f): 

def logger_function(*args, **kwargs): 

result = f(*args, **kwargs) 

print("Result: %s" % result) 

return result 



return logger_function




@log 

def do_dummy_stuff(): 

return 1
Decorator pattern
add some tricks
def log(level=‘DEBUG'): 

def decorator(f): 

def logger_function(*args, **kwargs): 

result = f(*args, **kwargs) 

print("%s: %s" % (level, result)) 

return result 



return logger_function 



return decorator 





@log(level=‘INFO') 

def do_dummy_stuff(): 

return 1
Complex syntax
One-line if:
print('even' if i % 2 == 0 else 'odd')

One line for:
l = [num ** 2 for num in numbers]

None or … :
value = dummy_function_may_return_none() or 0


Question?

Python in 90 minutes

  • 1.
    Python in 90minutes Bardia Heydari nejad
  • 2.
    What is Python? Whatnormal people think What I think
  • 3.
    Why Python? • It’smultiiiiiiiiiiiiiiii purpose 😃 Web, GUI, Scripting, Data mining, Artificial intelligence …. • It’s Readable 😊 No goddamn semicolon; • It’s Productive 😮 10 times faster than C too produce a software
  • 4.
    about Python • interpreted:no more compiling no more binaries • object-oriented: but you are not forced • strongly-typed: explicits are better than implicit • dynamically-typed: simple is better than complex • cross platform: everywhere that interpreted exists • indenting is a must: beautiful is better than ugly • every thing is object
  • 5.
    Hello, world! #include<iostream> using namespacestd; int main() { cout<<“Hello, world!”; return 0; } C++
  • 6.
    Hello, world! public classProgram { public static main(string[] argus){ System.out.println(“Hello, world!”); } } Java
  • 7.
    Hello, world! <html> <head> <title>PHP Test</title> </head> <body> <?phpecho ‘<p>Hello, world!</p>’; ?> </body> </html> PHP
  • 8.
  • 9.
    Operator • + • - •* • / • // • ** • % • == • != • > • < • >= • <= • += • -= • *= • /= • **= • //= • & • | • ^ • ~ • >> • << • and • or • not • in • is
  • 10.
    Variables •int i =1 •float f = 2.1 •bool b = False •str s = ‘hi’ •list l = [1, 2, “hello again”, 4] •tuple t = (True, 2.1) •dict d = {1:”One”, 2:”Two”} •set s = {“some”, “unique”, “objects”}
  • 11.
    Variables - Assignment >>>dummy_variable = 2 >>> dummy_variable = ‘hi’ >>> a, b = 2, 3 >>> a, b = b, a int main() { int a = 2, b = 3 , k; k = a; a = b; b = a; return 0; } in c++
  • 12.
    Variables - Casting >>>float(1) 1.0 >>> str(1) ‘1’ >>> int(‘1’) 1 >>> list(1) [1, ] >>> list(‘hello’) ['h', 'e', 'l', 'l', ‘o'] >>> set([1, 0, 1, 1, 0, 1, 0, 0]) set([1, 0])
  • 13.
    String • concatenate • slice •find • replace • count • capitalize
  • 14.
    List • slice • concatenate •sort • append - pop - remove - insert
  • 15.
  • 16.
  • 17.
    Function • Keyword arguments •Default arguments • Functions are objects
  • 18.
  • 19.
    Object oriented • Eventclasses are object • Dynamically add attributes • Static methods and attributes • Property
  • 20.
    Beyond OO reflection/introspection • type(obj) •isinstance(obj, class) • issubclass(class, class) • getattr(obj, key) / hastattr(obj, key)
  • 21.
    First class functions •function assignment • send function as parameter • callable objects
  • 22.
    Strategy pattern 
 class Player:
 def __init__(self, play_behavior): 
 self.play_behavior = play_behavior 
 
 def play(self): 
 return self.play_behavior 
 
 
 def attack(): 
 print("I attack”) 
 
 attacker = Player(attack) .
  • 23.
    Closure pattern • functionin function def add(x): 
 def add2(y): 
 return x + y 
 return add2 or with lambda def add(x): 
 return lambda y: x+y • callable class Add: 
 def __init__(self, x): 
 self.x = x 
 
 def __call__(self, y): 
 return self.x + y 

  • 24.
    Generator pattern • subroutinevs co-routine 
 def get_primes(number): 
 while True: 
 if is_prime(number): 
 yield number 
 number += 1 
 
 def solve_number_10(): 
 total = 0 
 for next_prime in get_primes(2): 
 if next_prime < 2000000: 
 total += next_prime 
 else: 
 break 
 print(total)
  • 25.
    Decorator pattern def register_function(f):
 print("%s function is registered" % f.__name__) 
 return f 
 
 
 @register_function 
 def do_dummy_stuff(): 
 return 1 

  • 26.
    Decorator pattern add sometricks def log(f): 
 def logger_function(*args, **kwargs): 
 result = f(*args, **kwargs) 
 print("Result: %s" % result) 
 return result 
 
 return logger_function 
 
 @log 
 def do_dummy_stuff(): 
 return 1
  • 27.
    Decorator pattern add sometricks def log(level=‘DEBUG'): 
 def decorator(f): 
 def logger_function(*args, **kwargs): 
 result = f(*args, **kwargs) 
 print("%s: %s" % (level, result)) 
 return result 
 
 return logger_function 
 
 return decorator 
 
 
 @log(level=‘INFO') 
 def do_dummy_stuff(): 
 return 1
  • 28.
    Complex syntax One-line if: print('even'if i % 2 == 0 else 'odd')
 One line for: l = [num ** 2 for num in numbers]
 None or … : value = dummy_function_may_return_none() or 0 

  • 29.