Python for High School Programmers

1,764 views

Published on

Published in: Technology, Education
2 Comments
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
1,764
On SlideShare
0
From Embeds
0
Number of Embeds
687
Actions
Shares
0
Downloads
23
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide

Python for High School Programmers

  1. 1. Python for HighSchool Programmers April 06, 2013Sivasubramaniam Arunachalam @sivaa_in
  2. 2. It’s me!• Application Developer • Web/Enterprise/Middleware/B2B • Java/Java EE, Python/Django • 2002• Technical Consultant• Process Mentor• Speaker
  3. 3. It’s about you!Have you written a code recently? Yes / No / Never It doesn’t matter!
  4. 4. Agenda• Background• Concepts• Basics• Demo
  5. 5. for / elsefor ( int i = 0; i < 10; i++ ) { ……} else { ……}
  6. 6. Back in 1989
  7. 7. Guido van Rossum http://www.python.org/~guido/images/guido91.gif
  8. 8. https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRsHF89zT2tSdsm45hFYDaJPocfpwIM_Eh76pjKbTzZI2dxArWa
  9. 9. http://upload.wikimedia.org/wikipedia/en/9/9a/CompleteFlyingCircusDVD.jpg
  10. 10. High Level Language
  11. 11. Dynamic / Scripting Language
  12. 12. Zen of Python• Beautiful is better than ugly.• Explicit is better than implicit.• Simple is better than complex.• Complex is better than complicated.• Flat is better than nested.• Sparse is better than dense.• Readability counts.• Special cases arent special enough to break the rules.• Although practicality beats purity.• Errors should never pass silently.• Unless explicitly silenced.• In the face of ambiguity, refuse the temptation to guess.• There should be one-- and preferably only one --obvious way to do it.• Although that way may not be obvious at first unless youre Dutch.• Now is better than never.• Although never is often better than *right* now.• If the implementation is hard to explain, its a bad idea.• If the implementation is easy to explain, it may be a good idea.• Namespaces are one honking great idea -- lets do more of those!
  13. 13. Why?
  14. 14. Clean / Clear Syntax
  15. 15. Less Key Words 31
  16. 16. Portable • Servers / Desktops / Mobile Devices / Raspberry Pi • Windows/Unix/Linux/Mac • Pre-installed in Mac and Linux
  17. 17. Multi-Language Support • CPython • Jython • IronPython
  18. 18. Multi-Paradigm • Functional / Procedural / Object Oriented
  19. 19. Easy to Learn
  20. 20. Highly Readable
  21. 21. No Variable Declaration
  22. 22. More Productivity...the lines of Python code were 10% of the equivalent C++ code. - Greg Stein
  23. 23. Batteries Included
  24. 24. Open Source
  25. 25. Where I can use?• Stand-Alone Application• Desktop/GUI Application• Web Application• XML Processing• Database Application• Network Application• Scientific Application• Gaming• Robotics
  26. 26. Some One using it?
  27. 27. Hello World!
  28. 28. Installation / Tools • Interpreter • Script Editor
  29. 29. . py .pyc .pyo .pyd
  30. 30. Optimized Code • Faster Execution • Less CPU Cycles
  31. 31. Increment ‘i’ by 1 i++ i=i+1 ADD #1,A1 [A1] ← [A1] + 1
  32. 32. Machine Instructions / Line of Code (LoC)
  33. 33. Assembly Language 1-2System Languages 3-7Scripting Languages 100 - 1K
  34. 34. Current Versions 2.7.3 & 3.3.0
  35. 35. The Prompt >>>
  36. 36. How to run?C:> python my_program.py
  37. 37. Let’s Start• Start / Exit• Simple print• Zen of Python• Keywords• Help
  38. 38. Variables
  39. 39. <type> var_name = val; int int_ex = 10; int_ex = 10
  40. 40. int_ex = 10long_ex = 1000000000000Lfloat_ex = 1.1string_ex = “Welcome”boolean_ex = True
  41. 41. type()
  42. 42. print type(int_ex)print type(long_ex)print type(float_ex)print type(string_ex)print type(boolean_ex)
  43. 43. boolean_one = True boolean_two = Falseprint boolean_one and boolean_twoprint boolean_one or boolean_twoprint not boolean_two
  44. 44. int_one = 11 int_two = 5print int_one / int_twoprint float(int_one) / float(int_two)print float(int_one) / int_twoprint int_one / float(int_two)
  45. 45. type casting
  46. 46. int_ex = 11 float_ex = 8.8 boolean_ex = Trueprint float (int_ex)print int (float_ex)print int (boolean_ex)
  47. 47. int_one = 11 int_two = 5print int_one + int_twoprint int_one - int_twoprint int_one * int_twoprint int_one / int_twoprint int_one % int_twoprint int_one ** int_two
  48. 48. int_one = 11 int_two = 5print int_one > int_twoprint int_one >= int_twoprint int_one < int_twoprint int_one <= int_twoprint int_one == int_twoprint int_one != int_two
  49. 49. Lets do some Maths
  50. 50. import math (or)from math import sqrt
  51. 51. dir (math)
  52. 52. int_one = 11 int_two = 5import mathprint math.sqrt (int_one) import mathfrom math import factorial print math.piprint factorial (int_two)
  53. 53. Lets Talk to the User
  54. 54. raw_input() (or)raw_input([prompt_string])
  55. 55. user_input = raw_input()print “You have Entered “ , user_inputuser_input = raw_input(“Input Please : “)print type(user_input)
  56. 56. int_one = raw_input (“Number 1 : “)int_two = raw_input (“Number 2 : “)print int_one + int_twoprint int(int_one) + int(int_two)
  57. 57. The Nightmare Strings
  58. 58. “Hey, What’s up?”
  59. 59. ‘ ’ “ ”“““ ””” ‘‘‘ ’’’
  60. 60. print ‘ example text ’print “ example text ”print “““ example of long ….. …. text ”””print ‘‘‘ example of long ….. …. text ’’’
  61. 61. print ‘ String with “double” quotes ’print “String with ‘single’ quote ”print ““ Hey what‘s up””print ‘“ Hey what‘s up”’print “““ “ Hey what‘s up ” ”””print ‘‘‘ “ Hey what‘s up ” ’’’
  62. 62. Don’t Mess with Quotes
  63. 63. Collections
  64. 64. 0 1 2 3 4 510 10.1 ‘A’ ‘ABC’ 2 True-6 -5 -4 -3 -2 -1
  65. 65. 0 1 2 3 4 5 10 10.1 ‘A’ ‘ABC’ 2 True -6 -5 -4 -3 -2 -1list_ex = [ 10, 10.1, ‘A’, ‘ABC’, 2, True ]tuple_ex = ( 10, 10.1, ‘A’, ‘ABC’, 2, True )
  66. 66. List [] vs Tuple ()
  67. 67. 0 1 2 3 4 5 10 10.1 ‘A’ ‘ABC’ 2 True -6 -5 -4 -3 -2 -1tuple_ex = ( 10, 10.1, ‘A’, ‘ABC’, 2, True )• tuple_ex[0] • tuple_ex[-6]• tuple_ex[1] • tuple_ex[-5]• tuple_ex[2] • tuple_ex[-4]• tuple_ex[3] • tuple_ex[-3]• tuple_ex[4] • tuple_ex[-2]• tuple_ex[5] • tuple_ex[-1]
  68. 68. Tuple / List Tricks
  69. 69. tuple_ex = (100, )list_ex = [100, ]tuple_ex = tuple(‘gobi’)list_ex = list(‘gobi’)
  70. 70. Lets Slice!
  71. 71. 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1list_ex = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, ] print list_ex[:3] print list_ex[3:] print list_ex[:-3] print list_ex[-3:]
  72. 72. 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10-10 -9 -8 -7 -6 -5 -4 -3 -2 -1list_ex = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, ] print list_ex[0:1] print list_ex[0:2] print list_ex[-2:-1] print list_ex[-3:-1]
  73. 73. 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10-10 -9 -8 -7 -6 -5 -4 -3 -2 -1list_ex = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, ] print list_ex[0:10:2] print list_ex[1:10:2]
  74. 74. 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10-10 -9 -8 -7 -6 -5 -4 -3 -2 -1list_ex = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, ] print len(list_ex) print max(list_ex) print min(list_ex)
  75. 75. Lets Join!
  76. 76. city = list(‘gobi’)city[len(city):] = “chettipalayam” “”.join(city)
  77. 77. ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’, ‘SRH’,] “, ”.join(ipl_teams)
  78. 78. str_ipl_teams = “, ”.join(ipl_teams)list_ipl_teams = list(str_ipl_teams )list_ipl_teams[str_ipl_teams.rfind(", ")] = " and“print "".join(list_ipl_teams)
  79. 79. ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,] ipl_teams.append(‘SRH’)
  80. 80. ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,] ipl_teams.remove(‘SRH’) del ipl_teams[-1]
  81. 81. ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,] ipl_teams.insert (1, ‘SRH’)
  82. 82. ipl_teams = [‘CSK’, ‘SRH’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,] ipl_teams.sort()
  83. 83. The Control Flow
  84. 84. Lets begin with a Search
  85. 85. list_ex = [1, 2, 3, 4, 5]print 3 in list_ex1
  86. 86. title_winners = [‘CSK’, ‘RR’, ‘DC’, ‘KKR’]print ‘CSK’ in title_winners
  87. 87. my_city = “Gobichettipalayam”print ‘chetti’ in my_city
  88. 88. while
  89. 89. i=1while i <= 10: print i, i=i+1
  90. 90. for
  91. 91. for i in range(1, 11): print i,
  92. 92. for i in range(11): if i % 2 == 0: print i
  93. 93. for i in range(11): if i % 2 == 0: print “Even : ”, i else : print “Odd: ”, i
  94. 94. for i in range(11): if i == 0: pass elif i % 2 == 0: print “Even : ”, i else : print “Odd: ”, i
  95. 95. for i in range(11): if i == 0: continue elif i % 2 == 0: print “Even : ”, i else : print “Odd: ”, i
  96. 96. for i in range(11): if i == 0: break elif i % 2 == 0: print “Even : ”, i else : print “Odd: ”, i
  97. 97. title_winners = [CSK, RR, DC, KKR]for team in title_winners: print team,
  98. 98. for i in range(11): print i,else: print “No Break executed in for”
  99. 99. Null Factor None
  100. 100. list_ex = []if list_ex: print “List is not Empty”else: print “List is Empty”
  101. 101. list_ex = Noneif list_ex: print “List is not None”else: print “List is None”
  102. 102. Error Handling
  103. 103. The OO Python
  104. 104. Thank You! siva@sivaa.inbit.ly/sivaa_in bit.ly/sivasubramaniam
  105. 105. Referenceshttp://www.slideshare.net/narendra.sisodiya/python-presentation-presentationhttp://www.f-106deltadart.com/photo_gallery/var/albums/NASA-Research/nasa_logo.png?m=1342921398http://www.seomofo.com/downloads/new-google-logo-knockoff.pnghttp://www.huntlogo.com/wp-content/uploads/2011/11/US-Navy-Logo.pnghttp://3.bp.blogspot.com/_7yB-eeGviiI/TUR471pyRpI/AAAAAAAAIBI/X705lo5Gjqc/s1600/Yahoo_Logo24.JPGhttp://www.thetwowayweb.com/wp-content/uploads/2012/09/youtube_logo.png

×