Python 1

587 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
587
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python 1

  1. 1. H appy women'sday
  2. 2. python(p1) Presenter : Ramin Najjarbashi Email: ramin.najarbashi@ .com
  3. 3. python(p1) Presenter : Ramin Najjarbashi Email: ramin.najarbashi@ .com
  4. 4. Who am I?
  5. 5. Who am I? ● Farhamg.Name ● Robocup Server 2D ● GNegar ● BadTag ● BlueWay ● UMS ● Xbuilder ● ...
  6. 6. Contents● What is Python? ● Hello world! ● RUN SCRIPTS! ● What is the object in Python? ● Python like as calculator! ● String and other types in Python! ● Function & Classes! ● Repeat the decision in Program! ● I/O files ;D ● HEEEEEEEEEEEEEEEEEEEEEELP!
  7. 7. Contents● What is Python? ● Hello world! ● RUN SCRIPTS! ● What is the object in Python? ● Python like as calculator! ● String and other types in Python! ● Function & Classes! ● Repeat the decision in Program! ● I/O files ;D ● HEEEEEEEEEEEEEEEEEEEEEELP!
  8. 8. ● Guido van Rossum history
  9. 9. ● Guido van Rossum history https://soundcloud.com/mashhadsoftwaretalks http://www.slideshare.net/ramin311/python-part-0
  10. 10. Hello world
  11. 11. Hello world printprint ""hello worldhello world""
  12. 12. from interpreter $ python$ python >>>>>>print "print "hello worldhello world"" hello worldhello world
  13. 13. Python 3 $ python$ python >>>>>>print ("print ("hello worldhello world")") hello worldhello world
  14. 14. Contents● What is Python? ● Hello world! ● RUN SCRIPTS! ● What is the object in Python? ● Python like as calculator! ● String and other types in Python! ● Function & Classes! ● Repeat the decision in Program! ● I/O files ;D ● HEEEEEEEEEEEEEEEEEEEEEELP!
  15. 15. repl Read, Eval, Print,Loop
  16. 16. repl $ python$ python # ...# ... >>>>>> 2 + 22 + 2 # Read, Eval# Read, Eval 44 # Print# Print >>>>>> # Loop# Loop
  17. 17. Linux script
  18. 18. Linux script
  19. 19. Linux script
  20. 20. Contents● What is Python? ● Hello world! ● RUN SCRIPTS! ● What is the object in Python? ● Python like as calculator! ● String and other types in Python! ● Function & Classes! ● Repeat the decision in Program! ● I/O files ;D ● HEEEEEEEEEEEEEEEEEEEEEELP!
  21. 21. ObjectsEverything is an Object!
  22. 22. Objects Obj id Value Immutable Mutable Tuple Integer String List Dictionary
  23. 23. Objects Obj id Value Immutable Mutable Tuple Integer String List Dictionary
  24. 24. Mutable >>>>>> bb == [ ][ ] >>>>>> idid ((bb)) 140675605442000140675605442000 >>>>>> bb .. appendappend(( 33 )) >>>>>> bb [3][3] >>>>>> idid ((bb)) 140675605442000140675605442000 # SAME!# SAME!
  25. 25. Immutable >>>>>> aa == 44 >>>>>> idid ((aa)) 64068966406896 >>>>>> aa == a + 1a + 1 >>>>>> idid ((aa)) 64068726406872 # DIFFERENT!# DIFFERENT!
  26. 26. Variables
  27. 27. Variables >>>>>> a =a = 44 # Integer# Integer >>>>>> b =b = 5.65.6 ## FloatFloat >>>>>> c =c = “hello”“hello” ## StringString >>>>>> d =d = “4”“4” ## rebound to Stringrebound to String
  28. 28. naming
  29. 29. naming Lowercase underscore_between_words don't start with numbers
  30. 30. naming Lowercase underscore_between_words don't start with numbers SEE PEP 8
  31. 31. naming Lowercase underscore_between_words don't start with numbers SEE PEP 8
  32. 32. PEP Python Enhancement Proposal (similar to JSR in Java)
  33. 33. PEP Python Enhancement Proposal (similar to JSR in Java) http://www.python.org/dev/peps/
  34. 34. Contents● What is Python? ● Hello world! ● RUN SCRIPTS! ● What is the object in Python? ● Python like as calculator! ● String and other types in Python! ● Function & Classes! ● Repeat the decision in Program! ● I/O files ;D ● HEEEEEEEEEEEEEEEEEEEEEELP!
  35. 35. math
  36. 36. math Operator Description + addition - subtraction * multiplication / division // integer division % remainder ** power
  37. 37. MATH $ python$ python >>>>>> 3/43/4 00 >>>>>> 3/4.3/4. 0.750.75
  38. 38. MATH $ python$ python >>>>>> 2.5+3j * 52.5+3j * 5 (2.5+15j)(2.5+15j) >>>>>> 2 ** 1000002 ** 100000 999002093014384...L999002093014384...L (in next slide!)(in next slide!)
  39. 39. MATH
  40. 40. MATH
  41. 41. MATH $ python$ python >>>>>> 0.1 + 0.20.1 + 0.2 0.300000000000000040.30000000000000004
  42. 42. MATH $ python$ python >>>>>> 0.1 + 0.20.1 + 0.2 0.300000000000000040.30000000000000004
  43. 43. MATH $ python$ python >>>>>> 0.1 + 0.20.1 + 0.2 0.300000000000000040.30000000000000004 You can check out the decimal module if you need more exact answers.
  44. 44. Contents● What is Python? ● Hello world! ● RUN SCRIPTS! ● What is the object in Python? ● Python like as calculator! ● String and other types in Python! ● Function & Classes! ● Repeat the decision in Program! ● I/O files ;D ● HEEEEEEEEEEEEEEEEEEEEEELP!
  45. 45. Strings >>>>>> printprint 'He said, "I'He said, "I '' m sorry"'m sorry"' He said, "I'm sorry"He said, "I'm sorry" >>>>>> printprint '''He said, "I'm sorry"''''''He said, "I'm sorry"''' He said, "I'm sorry"He said, "I'm sorry" >>>>>> printprint """He said, "I'm sorry"""He said, "I'm sorry "" """""" He said, "I'm sorry"He said, "I'm sorry" >>>>>> a =a = """He said, "I'm sorry"""He said, "I'm sorry "" """"""
  46. 46. Strings
  47. 47. Strings >>>>>> #c-like#c-like >>>>>> " %s %s "" %s %s " % (% ( 'hello''hello' ,, 'world''world' )) 'hello world''hello world' >>>>>> #PEP 3101 style#PEP 3101 style >>>>>> "{0} {1}""{0} {1}" . format(. format( 'hello''hello' ,, 'world''world' )) 'hello world''hello world' >>>>>> ''' Comment string''' Comment string .….… multiline! '''multiline! ''' >>>>>>
  48. 48. none
  49. 49. boolean
  50. 50. sequences Set Dict List tuple
  51. 51. sequences {Set} {Dict} [List] (tuple)
  52. 52. Terminl time
  53. 53. Contents● What is Python? ● Hello world! ● RUN SCRIPTS! ● What is the object in Python? ● Python like as calculator! ● String and other types in Python! ● Function & Classes! ● Repeat the decision in Program! ● I/O files ;D ● HEEEEEEEEEEEEEEEEEEEEEELP!
  54. 54. function defdef add_numadd_num(x, y=1):(x, y=1): '''Get x, y and return:'''Get x, y and return: X + Y '''X + Y ''' returnreturn x + yx + y >>>>>> printprint add_num(2, 3)add_num(2, 3) 55 >>>>>> printprint add_num(2)add_num(2) 33
  55. 55. function
  56. 56. function defdef add_numadd_num(x, y):(x, y): '''Get x, y and return:'''Get x, y and return: X + Y '''X + Y ''' z = x + yz = x + y returnreturn zz >>>>>> printprint add_num(2, 3)add_num(2, 3) 55
  57. 57. def defdef add_num(x, y):add_num(x, y): '''Get x, y and return:'''Get x, y and return: X + Y '''X + Y ''' z = x + yz = x + y return zreturn z >>> print add_num(2, 3)>>> print add_num(2, 3) 55
  58. 58. name defdef add_numadd_num(x, y):(x, y): '''Get x, y and return:'''Get x, y and return: X + Y '''X + Y ''' z = x + yz = x + y return zreturn z >>> print add_num(2, 3)>>> print add_num(2, 3) 55
  59. 59. parameters def add_numdef add_num(x, y)(x, y):: '''Get x, y and return:'''Get x, y and return: X + Y '''X + Y ''' z = x + yz = x + y return zreturn z >>> print add_num(2, 3)>>> print add_num(2, 3) 55
  60. 60. : + indent def add_num(x, y)def add_num(x, y):: --------'''Get x, y and return:'''Get x, y and return: -------- X + Y '''X + Y ''' --------z = x + yz = x + y --------return zreturn z >>> print add_num(2, 3)>>> print add_num(2, 3) 55
  61. 61. documentation def add_num(x, y):def add_num(x, y): '''Get x, y and return:'''Get x, y and return: X + Y '''X + Y ''' z = x + yz = x + y return zreturn z >>> print add_num(2, 3)>>> print add_num(2, 3) 55
  62. 62. body def add_num(x, y):def add_num(x, y): '''Get x, y and return:'''Get x, y and return: X + Y '''X + Y ''' z = x + yz = x + y return zreturn z >>> print add_num(2, 3)>>> print add_num(2, 3) 55
  63. 63. return def add_num(x, y):def add_num(x, y): '''Get x, y and return:'''Get x, y and return: X + Y '''X + Y ''' z = x + yz = x + y returnreturn zz >>> print add_num(2, 3)>>> print add_num(2, 3) 55
  64. 64. naming Lowercase underscore_between_words don't start with numbers verb
  65. 65. naming Lowercase underscore_between_words don't start with numbers verb SEE PEP 8
  66. 66. documentation def add_num(x, y):def add_num(x, y): '''Get x, y and return:'''Get x, y and return: X + Y '''X + Y ''' z = x + yz = x + y return zreturn z >>>>>> helphelp(add_num)(add_num) Help on function add_num in module __main__:Help on function add_num in module __main__: add_num()add_num() Get x,y and return:Get x,y and return: X + YX + Y (END)(END)
  67. 67. KLAz
  68. 68. KLAz classclass StudentStudent((objectobject):): defdef __init____init__ ((self,self, namename):): self.self.name = namename = name defdef print_nameprint_name ((selfself):): printprint self.self.namename
  69. 69. KLAz classclass Student(object):Student(object): def __init__ (self, name):def __init__ (self, name): self.name = nameself.name = name def print_name (self):def print_name (self): print self.nameprint self.name
  70. 70. name classclass StudentStudent(object):(object): def __init__ (self, name):def __init__ (self, name): self.name = nameself.name = name def print_name (self):def print_name (self): print self.nameprint self.name
  71. 71. type class Studentclass Student((objectobject)):: def __init__ (self, name):def __init__ (self, name): self.name = nameself.name = name def print_name (self):def print_name (self): print self.nameprint self.name
  72. 72. : + indent class Student(object)class Student(object):: ----def __init__ (self, name):def __init__ (self, name): --------self.name = nameself.name = name ----def print_name (self):def print_name (self): --------print self.nameprint self.name
  73. 73. __Init__ method class Student(object):class Student(object): defdef __init____init__ ((self,self, namename):): self.self.name = namename = name def print_name (self):def print_name (self): print self.nameprint self.name
  74. 74. self class Student(object):class Student(object): def __init__ (def __init__ (selfself, name):, name): self.name = nameself.name = name def print_name (self):def print_name (self): print self.nameprint self.name
  75. 75. Sub-KLAz classclass KharKhoonKharKhoon(Student):(Student): defdef print_nameprint_name ((selfself):): printprint self.self.name +name + '' CRAMer '''' CRAMer '' >>>>>> m = KharKhoon(“ahmad”)m = KharKhoon(“ahmad”) >>>>>> m.print_name()m.print_name() ahmad CRAMerahmad CRAMer
  76. 76. naming
  77. 77. naming CamelCase don't start with numbers Nouns
  78. 78. Contents● What is Python? ● Hello world! ● RUN SCRIPTS! ● What is the object in Python? ● Python like as calculator! ● String and other types in Python! ● Function & Classes! ● Repeat the decision in Program! ● I/O files ;D ● HEEEEEEEEEEEEEEEEEEEEEELP!
  79. 79. while >>>>>>whilewhile b < 10:b < 10: …… printprint bb …… a, b = b, a+ba, b = b, a+b …… 11 11 22 33 55 88
  80. 80. while >>>>>>whilewhile b < 10:b < 10: …… print bprint b …… a, b = b, a+ba, b = b, a+b …… 11 11 22 33 55 88
  81. 81. while >>>while>>>while b < 10b < 10:: …… print bprint b …… a, b = b, a+ba, b = b, a+b …… 11 11 22 33 55 88
  82. 82. while >>>while b < 10>>>while b < 10:: ……--------print bprint b ……--------a, b = b, a+ba, b = b, a+b …… 11 11 22 33 55 88
  83. 83. while >>>while b < 10:>>>while b < 10: …… printprint bb …… a, b = b, a+ba, b = b, a+b …… 11 11 22 33 55 88
  84. 84. while >>>while b < 10:>>>while b < 10: …… print bprint b …… a,a, b =b = b,b, a+ba+b …… 11 11 22 33 55 88
  85. 85. forever >>>>>>whilewhile 1:1: …… passpass
  86. 86. if >>>>>>ifif b < 10:b < 10: …… ifif b > 8:b > 8: …… printprint '9''9'
  87. 87. if >>>>>>ifif b < 10b < 10 andand b > 8:b > 8: …… printprint '9''9'
  88. 88. if >>>>>>ifif 8 < b < 108 < b < 10:: …… printprint '9''9'
  89. 89. if-else >>>>>>ifif b >= 10:b >= 10: …… printprint 'big''big' …… elifelif b =< 8:b =< 8: …… printprint 'small''small' …… elseelse:: …… printprint '9''9'
  90. 90. if-else >>>>>>ifif b >= 10:b >= 10: …… print 'big'print 'big' …… elif b =< 8:elif b =< 8: …… print 'small'print 'small' …… else:else: …… print '9'print '9'
  91. 91. if-else >>>if>>>if b >= 10b >= 10:: …… print 'big'print 'big' …… elif b =< 8:elif b =< 8: …… print 'small'print 'small' …… else:else: …… print '9'print '9'
  92. 92. if-else >>>if b >= 10>>>if b >= 10:: ……--------print 'big'print 'big' …… elif b =< 8:elif b =< 8: ……--------print 'small'print 'small' …… else:else: ……--------print '9'print '9'
  93. 93. if-else >>>if b >= 10:>>>if b >= 10: …… printprint 'big''big' …… elif b =< 8:elif b =< 8: …… print 'small'print 'small' …… else:else: …… print '9'print '9'
  94. 94. if-else >>>if b >= 10:>>>if b >= 10: …… print 'big'print 'big' …… elifelif b =< 8:b =< 8: …… printprint 'small''small' …… else:else: …… print '9'print '9'
  95. 95. if-else >>>if b >= 10:>>>if b >= 10: …… print 'big'print 'big' …… elifelif b =< 8:b =< 8: …… print 'small'print 'small' …… else:else: …… print '9'print '9'
  96. 96. if-else >>>if b >= 10:>>>if b >= 10: …… print 'big'print 'big' …… elifelif b =< 8b =< 8:: …… print 'small'print 'small' …… else:else: …… print '9'print '9'
  97. 97. if-else >>>if b >= 10:>>>if b >= 10: …… print 'big'print 'big' …… elif b =< 8:elif b =< 8: …… printprint 'small''small' …… else:else: …… print '9'print '9'
  98. 98. if-else >>>if b >= 10:>>>if b >= 10: …… print 'big'print 'big' …… elif b =< 8:elif b =< 8: …… print 'small'print 'small' …… elseelse:: …… printprint '9''9'
  99. 99. if-else >>>if b >= 10:>>>if b >= 10: …… print 'big'print 'big' …… elif b =< 8:elif b =< 8: …… print 'small'print 'small' …… elseelse:: …… print '9'print '9'
  100. 100. for >>>>>> forfor nn inin [2, 3, 4, 5, 6, 7, 8, 9]:[2, 3, 4, 5, 6, 7, 8, 9]: …… forfor xx inin rangerange(2, n):(2, n): …… ifif n % x == 0:n % x == 0: ...... printprint nn ...... breakbreak ...... elseelse:: ...... printprint n,n, 'is a prime''is a prime'
  101. 101. for >>>>>> forfor n in [2, 3, 4, 5, 6, 7, 8, 9]:n in [2, 3, 4, 5, 6, 7, 8, 9]: …… for x in range(2, n):for x in range(2, n): …… if n % x == 0:if n % x == 0: ...... print nprint n ...... breakbreak ...... else:else: ...... print n, 'is a prime'print n, 'is a prime'
  102. 102. for >>> for>>> for nn in [2, 3, 4, 5, 6, 7, 8, 9]:in [2, 3, 4, 5, 6, 7, 8, 9]: …… for x in range(2, n):for x in range(2, n): …… if n % x == 0:if n % x == 0: ...... print nprint n ...... breakbreak ...... else:else: ...... print n, 'is a prime'print n, 'is a prime'
  103. 103. for >>> for n>>> for n inin [2, 3, 4, 5, 6, 7, 8, 9]:[2, 3, 4, 5, 6, 7, 8, 9]: …… for x in range(2, n):for x in range(2, n): …… if n % x == 0:if n % x == 0: ...... print nprint n ...... breakbreak ...... else:else: ...... print n, 'is a prime'print n, 'is a prime'
  104. 104. for >>> for n in>>> for n in [2, 3, 4, 5, 6, 7, 8, 9][2, 3, 4, 5, 6, 7, 8, 9]:: …… for x in range(2, n):for x in range(2, n): …… if n % x == 0:if n % x == 0: ...... print nprint n ...... breakbreak ...... else:else: ...... print n, 'is a prime'print n, 'is a prime'
  105. 105. for >>> for n in [2, 3, 4, 5, 6, 7, 8, 9]:>>> for n in [2, 3, 4, 5, 6, 7, 8, 9]: …… forfor xx inin rangerange(2, n):(2, n): …… ifif n % x == 0:n % x == 0: ...... printprint nn ...... breakbreak ...... elseelse:: ...... printprint n,n, 'is a prime''is a prime'
  106. 106. for >>> for n in [2, 3, 4, 5, 6, 7, 8, 9]:>>> for n in [2, 3, 4, 5, 6, 7, 8, 9]: …… for x infor x in rangerange(2, n)(2, n):: …… if n % x == 0:if n % x == 0: ...... print nprint n ...... breakbreak ...... else:else: ...... print n, 'is a prime'print n, 'is a prime'
  107. 107. for >>> for n in [2, 3, 4, 5, 6, 7, 8, 9]:>>> for n in [2, 3, 4, 5, 6, 7, 8, 9]: …… for x in range(2, n):for x in range(2, n): …… if n % x == 0:if n % x == 0: ...... print nprint n ...... breakbreak ...... else:else: ...... print n, 'is a prime'print n, 'is a prime'
  108. 108. for >>>>>> dict = {dict = { 'a''a':1 ,:1 , 'b''b'::'AB''AB',, 'c''c':[1,{}] }:[1,{}] } >>>>>>forfor key, valkey, val inin dict.items()dict.items():: …… printprint key, valkey, val a 1a 1 b ABb AB c [1,{}]c [1,{}]
  109. 109. Contents● What is Python? ● Hello world! ● RUN SCRIPTS! ● What is the object in Python? ● Python like as calculator! ● String and other types in Python! ● Function & Classes! ● Repeat the decision in Program! ● I/O files ;D ● HEEEEEEEEEEEEEEEEEEEEEELP!
  110. 110. Input file >>>>>>fin = (fin = ('foo.txt''foo.txt')) >>>>>>forfor lineline inin finfin:: …… printprint lineline >>>>>>fin.close()fin.close()
  111. 111. Input file >>>>>>fin = (fin = ('foo.txt''foo.txt')) >>>for line in fin:>>>for line in fin: …… print lineprint line >>>fin.close()>>>fin.close()
  112. 112. Input file >>>fin = ('foo.txt')>>>fin = ('foo.txt') >>>>>>forfor lineline inin finfin:: …… printprint lineline >>>fin.close()>>>fin.close()
  113. 113. Input file >>>fin = ('foo.txt')>>>fin = ('foo.txt') >>>for line in fin:>>>for line in fin: …… print lineprint line >>>>>>fin.close()fin.close()
  114. 114. Output file >>>>>>fout = (fout = ('foo.txt''foo.txt',,'w''w')) >>>>>>fout.writefout.write(('Hello world!''Hello world!')) >>>>>>fout.close()fout.close()
  115. 115. Output file >>>>>>fout = (fout = ('foo.txt''foo.txt',,'w''w')) >>>fout.write('Hello world!')>>>fout.write('Hello world!') >>>fout.close()>>>fout.close()
  116. 116. Output file >>>fout = ('foo.txt','w')>>>fout = ('foo.txt','w') >>>>>>fout.write(fout.write('Hello world!''Hello world!')) >>>fout.close()>>>fout.close()
  117. 117. Output file >>>fout = ('foo.txt','w')>>>fout = ('foo.txt','w') >>>fout.write('Hello world!'>>>fout.write('Hello world!')) >>>>>>fout.close()fout.close()
  118. 118. file
  119. 119. Contents● What is Python? ● Hello world! ● RUN SCRIPTS! ● What is the object in Python? ● Python like as calculator! ● String and other types in Python! ● Function & Classes! ● Repeat the decision in Program! ● I/O files ;D ● HEEEEEEEEEEEEEEEEEEEEEELP!
  120. 120. TRAce printprint
  121. 121. TRAce pdbpdb
  122. 122. trace >>>>>>importimport pdbpdb >>>>>>pdb.run(pdb.run('my_func()''my_func()')) OrOr python -m pdb myscript.pypython -m pdb myscript.py
  123. 123. trace ●● h - helph - help ●● s - step intos - step into ●● n - nextn - next ●● c - continuec - continue ●● w - where am I (in stack)?w - where am I (in stack)? ●● l - list code around mel - list code around me
  124. 124. trace hh - help- help ss - step into- step into nn - next- next cc - continue- continue ww - where am I (in stack)?- where am I (in stack)? ll - list code around me- list code around me
  125. 125. trace trytry:: f =f = openopen(arg, 'r')(arg, 'r') exceptexcept IOErrorIOError:: printprint 'cannot open''cannot open', arg, arg elseelse:: printprint argarg f.close()f.close()
  126. 126. comment ##
  127. 127. Dir & help >>>>>> dirdir([])([]) ['__add__', '__class__',['__add__', '__class__', '__contains__',...'__contains__',... '__iter__',... '__len__',... ,'__iter__',... '__len__',... , 'append', 'count','append', 'count', 'extend', 'index', 'insert','extend', 'index', 'insert', 'pop', 'remove','pop', 'remove', 'reverse', 'sort']'reverse', 'sort']
  128. 128. Python Doc http://www.python.org/doc/
  129. 129. StackOverFlow
  130. 130. Google
  131. 131. #IRC #python #python-ir
  132. 132. M.L general@tehpug.pyiran.com Pyiran Mailing list
  133. 133. &Coming soon...
  134. 134. Iranian Python Community
  135. 135. -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GE/IT/P/SS d---(-)@?>--pu s--(): a- C++++(+++)$@>++ ULC++++(+++)@ P+() L+++(+++)$@>+++ !E--- !W+++(++)@>+ !N* !o K-- !w---? !O---? M-- !V- PS++(++)@>+ !PE Y? PGP++(++)@>+++ !t !5 !X R+ tv? b++++(+++) DI D+++@ G++@ e+++@ h++ r---?>$ !y-- ------END GEEK CODE BLOCK------
  136. 136. Join us

×