python in 90 minutes

29
Python in 90 minutes Bardia Heydari nejad

Upload: bardia-heydari-nejad

Post on 15-Apr-2017

360 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Python in 90 minutes

Python in 90 minutesBardia Heydari nejad

Page 2: Python in 90 minutes

What is Python?

What normal people think What I think

Page 3: Python in 90 minutes

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

Page 4: Python in 90 minutes

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

Page 5: Python in 90 minutes

Hello, world!

#include<iostream>using namespace std;int main(){

cout<<“Hello, world!”;return 0;

}

C++

Page 6: Python in 90 minutes

Hello, world!

public class Program {public static main(string[] argus){System.out.println(“Hello, world!”);

}}

Java

Page 7: Python in 90 minutes

Hello, world!

<html> <head> <title>PHP Test</title> </head> <body> <?php echo ‘<p>Hello, world!</p>’; ?>

</body></html>

PHP

Page 8: Python in 90 minutes

Hello, world!

print “Hello, world!”

Python

Page 9: Python in 90 minutes

Operator

• + • - • * • / • // • ** • %

• == • != • > • < • >= • <=

• += • -= • *= • /= • **= • //=

• & • | • ^ • ~ • >> • <<

• and • or • not • in • is

Page 10: Python in 90 minutes

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”}

Page 11: Python in 90 minutes

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++

Page 12: Python in 90 minutes

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])

Page 13: Python in 90 minutes

String

• concatenate • slice • find • replace • count • capitalize

Page 14: Python in 90 minutes

List

• slice • concatenate • sort • append - pop - remove - insert

Page 15: Python in 90 minutes

Condition

• if • elif • else

Page 16: Python in 90 minutes

Loop

• while • for • else

Page 17: Python in 90 minutes

Function

• Keyword arguments • Default arguments • Functions are objects

Page 18: Python in 90 minutes

Question?

Page 19: Python in 90 minutes

Object oriented

• Event classes are object • Dynamically add attributes • Static methods and attributes • Property

Page 20: Python in 90 minutes

Beyond OO reflection/introspection

• type(obj) • isinstance(obj, class) • issubclass(class, class) • getattr(obj, key) / hastattr(obj, key)

Page 21: Python in 90 minutes

First class functions

• function assignment • send function as parameter • callable objects

Page 22: Python in 90 minutes

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) .

Page 23: Python in 90 minutes

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

Page 24: Python in 90 minutes

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)

Page 25: Python in 90 minutes

Decorator patterndef register_function(f): print("%s function is registered" % f.__name__) return f @register_function def do_dummy_stuff(): return 1

Page 26: Python in 90 minutes

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

Page 27: Python in 90 minutes

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

Page 28: Python in 90 minutes

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

Page 29: Python in 90 minutes

Question?