This document provides an overview of the Python programming language. It discusses what Python is, its key features like being multi-purpose, readable, and productive. It then demonstrates Hello World programs in Python, C++, Java, and PHP. The rest of the document covers Python concepts like operators, variables, strings, lists, conditions, loops, functions, object oriented programming, and design patterns like strategy, closure, generator, and decorator patterns.
3. 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
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
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++
23. 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
24. 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)
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