python novice to ninja
DESCRIPTION
Python convert session it should help people migrate from another programming language to python.TRANSCRIPT
PYTHON FROM NOVICE TO NINJA.A CONVERT SESSION BY @ALSAYEDGAMAL
WARNING: BEGINNERS ONLY SESSION
• Works also as a quick start guide for other language developers.
• No offence Java guys :)
AGENDA
• Motivation.
• Python essentials.
• Some basic examples.
• What to hack ?
MOTIVATIONWHY PYTHON ?
OPEN SOURCE(FREEDOM)
CROSS PLATFORMWRITE ONCE, RUN EVERYWHERE.
EASY TO LEARNIT TAKES > 10 SECONDS TO WRITE YOUR HELLO, WORLD.
SYSTEM.OUT.PRINT(“HELLO,WORLD”);IT TAKES 1 LINE TO YOUR “HELLO, WORLD”
FROM 29TH TO 7TH IN < 8 YEARS*IT’S 8TH SINCE 2012
*TOIBE.COM | TIOBE PROGRAMMING COMMUNITY INDEX FOR NOVEMBER 2014
PYTHON ESSENTIALS
• Scripting nature.
• Basic data types.
• Operators.
• Flow control.
• Functions are fun.
• Modules.
• OOP.
EXPRESSION IN PYTHON.
#EXPRESSIONvariable = 3 + 2
OPERATORS
OPERANDS
+ * = / %[] () > = & != |
Variablesx,name, __len__
Data5, “hello”,
[1,2,3]
TYPES IN PYTHON
#ASSIGNMENT !#CHECK FOO’S TYPE !#ANOTHER ASSIGNMENT !#FOO’S TYPE CHANGED
variable = 10
type(variable)
variable = “ten”
type(variable)
Dynamic
Implicit
x = 10 + “10” #ERROR
TYPES IN PYTHON Strings
line = “Hello” multi = “”” I use this to write multiline strings like html tags””” raw = r’I’ll not replace \n or \t’ #used in regex print(line[0]) #will print H try -3 template = "%d developers founded %s” % (3, ‘GDG Mansoura’) #3 developers founded GDG Mansoura ‘{0}, {1}, {2}’.format(‘A’, ‘B’, ‘C’) #A, B, C unicode_str = u’A unicode \u018e string \xf1'
TYPES IN PYTHON Boolean
1. It’s True not true 2. It’s False not false
TYPES IN PYTHON Data structures
names = [‘Ahmed’, ‘Sayed’, ‘AbdulHameed’] first_name = names[0] names[1] = “AlSayed” #names = [‘Ahmed’, ‘AlSayed’, ‘AbdulHameed’]
List
family = (“AlSayed”, ”Reem”) family[1] = “Some one else” TypeError: 'tuple' object does not support item assignment x = 5 y = 6 x, y = y, x
Tuple
TYPES IN PYTHON Data structures
phonebook = {‘name’:”Ahmed Gamal”, ‘age’ : 28, ‘telephone’: ‘0123456789’,‘email’: ‘[email protected]’}
Dictionary
for key in phonebook.keys(): print key for val in phonebook.values(): print val
Traversing Dictionary
PYTHON BLOCKS
if condition:
If block
elif condition:
else If block
else:
else block
Branching
Where is a fixed indentation spaces or tabs
PYTHON BLOCKS
for item in xrange(10):
for block
else:
@ normal end
Loop
while true:
loop on this
else:
@ normal end
Normal end means no break For is used basically for traversing iterable types (lists for example) xrange is generating an iterable object
PYTHON BLOCKS Loop
found_obj = None for obj in objects: if obj.key == search_key: found_obj = obj break else: print (‘No object found.’)
PYTHON BLOCKS
class Car(Vehicle):
#constructor
def __init__(self, arg1):
pass
Class Definition
PYTHON BLOCKS
def foo(x,y):
function body
Functions
def foo(x,y=‘default’):
function body
def foo(x,y=‘default’):
function body
def foo(x,y):
return exprsn
def foo(x,y=‘default’):
function body
def foo(x,y=‘default’):
function body
GETTING YOUR HANDS DIRTYTIME TO EXPLAIN IN CODE.
BEFORE WE START
• If you are linux or unix based you are good to go.
• Else If you are on windows (I hope not) download and install
• sublime text editor
• python runtime
• Our code will be perfect for Python 2.x and may be 3.x.
SORTED?
MEDIA.PY
P1: Tawfik Okasha
P2: Amr Adib
SCRIPT HIGHLIGHTS.
• Basic expression (variable assignment).
• User input and output.
• Function definition / passing function as variable.
• List data-type.
• Basic sorting.
MODULES AND PACKAGES
• from foo import bar • from foor import * # please, don’t use it often • Packages are directory with __init__.py file • import this #Zen of python
NEXT: SHAPING YOUR EXPERIENCESYSTEM ADMIN, WEB, DESKTOP, NETWORK, GAME DEVELOPMENT,…
QUESTIONS?
THANKS
RESOURCES
• Google Education (Python Class)|https://developers.google.com/edu/python/
• Python Docs | https://docs.python.org
• www.tiobe.com/index.php/content/paperinfo/tpci/index.html