python novice to ninja

32

Upload: al-sayed-gamal

Post on 02-Jul-2015

751 views

Category:

Technology


4 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Python Novice to Ninja
Page 2: Python Novice to Ninja

PYTHON FROM NOVICE TO NINJA.A CONVERT SESSION BY @ALSAYEDGAMAL

Page 3: Python Novice to Ninja

WARNING: BEGINNERS ONLY SESSION

• Works also as a quick start guide for other language developers.

• No offence Java guys :)

Page 4: Python Novice to Ninja

AGENDA

• Motivation.

• Python essentials.

• Some basic examples.

• What to hack ?

Page 5: Python Novice to Ninja

MOTIVATIONWHY PYTHON ?

Page 6: Python Novice to Ninja

OPEN SOURCE(FREEDOM)

Page 7: Python Novice to Ninja

CROSS PLATFORMWRITE ONCE, RUN EVERYWHERE.

Page 8: Python Novice to Ninja

EASY TO LEARNIT TAKES > 10 SECONDS TO WRITE YOUR HELLO, WORLD.

SYSTEM.OUT.PRINT(“HELLO,WORLD”);IT TAKES 1 LINE TO YOUR “HELLO, WORLD”

Page 9: Python Novice to Ninja

FROM 29TH TO 7TH IN < 8 YEARS*IT’S 8TH SINCE 2012

*TOIBE.COM | TIOBE PROGRAMMING COMMUNITY INDEX FOR NOVEMBER 2014

Page 10: Python Novice to Ninja

PYTHON ESSENTIALS

• Scripting nature.

• Basic data types.

• Operators.

• Flow control.

• Functions are fun.

• Modules.

• OOP.

Page 11: Python Novice to Ninja

EXPRESSION IN PYTHON.

#EXPRESSIONvariable = 3 + 2

OPERATORS

OPERANDS

+ * = / %[] () > = & != |

Variablesx,name, __len__

Data5, “hello”,

[1,2,3]

Page 12: Python Novice to Ninja

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

Page 13: Python Novice to Ninja

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'

Page 14: Python Novice to Ninja

TYPES IN PYTHON Boolean

1. It’s True not true 2. It’s False not false

Page 15: Python Novice to Ninja

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

Page 16: Python Novice to Ninja

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

Page 17: Python Novice to Ninja

PYTHON BLOCKS

if condition:

If block

elif condition:

else If block

else:

else block

Branching

Where is a fixed indentation spaces or tabs

Page 18: Python Novice to Ninja

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

Page 19: Python Novice to Ninja

PYTHON BLOCKS Loop

found_obj = None for obj in objects: if obj.key == search_key: found_obj = obj break else: print (‘No object found.’)

Page 20: Python Novice to Ninja

PYTHON BLOCKS

class Car(Vehicle):

#constructor

def __init__(self, arg1):

pass

Class Definition

Page 21: Python Novice to Ninja

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

Page 22: Python Novice to Ninja

GETTING YOUR HANDS DIRTYTIME TO EXPLAIN IN CODE.

Page 23: Python Novice to Ninja

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.

Page 24: Python Novice to Ninja

SORTED?

Page 25: Python Novice to Ninja

MEDIA.PY

P1: Tawfik Okasha

P2: Amr Adib

Page 26: Python Novice to Ninja

SCRIPT HIGHLIGHTS.

• Basic expression (variable assignment).

• User input and output.

• Function definition / passing function as variable.

• List data-type.

• Basic sorting.

Page 27: Python Novice to Ninja

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

Page 28: Python Novice to Ninja

NEXT: SHAPING YOUR EXPERIENCESYSTEM ADMIN, WEB, DESKTOP, NETWORK, GAME DEVELOPMENT,…

Page 29: Python Novice to Ninja
Page 30: Python Novice to Ninja

QUESTIONS?

Page 31: Python Novice to Ninja

THANKS

Page 32: Python Novice to Ninja

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