python in 90 minutes
TRANSCRIPT
Python in 90 minutesBardia 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 patterndef 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 syntaxOne-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?