Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Python Novice to Ninja

1,481 views

Published on

Python convert session it should help people migrate from another programming language to python.

Published in: Technology
  • Be the first to comment

Python Novice to Ninja

  1. 1. PYTHON FROM NOVICE TO NINJA. A CONVERT SESSION BY @ALSAYEDGAMAL
  2. 2. WARNING: BEGINNERS ONLY SESSION • Works also as a quick start guide for other language developers. • No offence Java guys :)
  3. 3. AGENDA • Motivation. • Python essentials. • Some basic examples. • What to hack ?
  4. 4. MOTIVATION WHY PYTHON ?
  5. 5. OPEN SOURCE (FREEDOM)
  6. 6. CROSS PLATFORM WRITE ONCE, RUN EVERYWHERE.
  7. 7. SYSTEM.OUT.PRINT(“HELLO,WORLD”); IT TAKES 1 LINE TO YOUR “HELLO, WORLD” EASY TO LEARN IT TAKES > 10 SECONDS TO WRITE YOUR HELLO, WORLD.
  8. 8. FROM 29TH TO 7TH IN < 8 YEARS* IT’S 8TH SINCE 2012 *TOIBE.COM | TIOBE PROGRAMMING COMMUNITY INDEX FOR NOVEMBER 2014
  9. 9. PYTHON ESSENTIALS • Scripting nature. • Basic data types. • Operators. • Flow control. • Functions are fun. • Modules. • OOP.
  10. 10. EXPRESSION IN PYTHON. OPERATORS variable = 3 + 2 #EXPRESSION OPERANDS + * = / % [] () > = & != | Variables x,name, __len__ Data 5, “hello”, [1,2,3]
  11. 11. 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
  12. 12. 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'
  13. 13. TYPES IN PYTHON Boolean 1. It’s True not true 2. It’s False not false
  14. 14. TYPES IN PYTHON Data structures List names = [‘Ahmed’, ‘Sayed’, ‘AbdulHameed’] first_name = names[0] names[1] = “AlSayed” #names = [‘Ahmed’, ‘AlSayed’, ‘AbdulHameed’] Tuple family = (“AlSayed”, ”Reem”) family[1] = “Some one else” TypeError: 'tuple' object does not support item assignment x = 5 y = 6 x, y = y, x
  15. 15. TYPES IN PYTHON Data structures Dictionary phonebook = {‘name’:”Ahmed Gamal”, ‘age’ : 28, ‘telephone’: ‘0123456789’,‘email’: ‘ahmed@example.com’} Traversing Dictionary for key in phonebook.keys(): print key for val in phonebook.values(): print val
  16. 16. PYTHON BLOCKS if condition: If block elif condition: else If block else: else block Branching Where is a fixed indentation spaces or tabs
  17. 17. 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
  18. 18. PYTHON BLOCKS Loop found_obj = None for obj in objects: if obj.key == search_key: found_obj = obj break else: print (‘No object found.’)
  19. 19. PYTHON BLOCKS class Car(Vehicle): #constructor def __init__(self, arg1): pass Class Definition
  20. 20. 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
  21. 21. GETTING YOUR HANDS DIRTY TIME TO EXPLAIN IN CODE.
  22. 22. 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.
  23. 23. SORTED?
  24. 24. MEDIA.PY P1: Tawfik Okasha P2: Amr Adib
  25. 25. SCRIPT HIGHLIGHTS. • Basic expression (variable assignment). • User input and output. • Function definition / passing function as variable. • List data-type. • Basic sorting.
  26. 26. 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
  27. 27. NEXT: SHAPING YOUR EXPERIENCE SYSTEM ADMIN, WEB, DESKTOP, NETWORK, GAME DEVELOPMENT,…
  28. 28. QUESTIONS?
  29. 29. THANKS
  30. 30. 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

×