Download - Python for High School Programmers
Python for High School Programmers
Sivasubramaniam Arunachalam
April 06, 2013
@sivaa_in
It’s me!
• Application Developer
• Web/Enterprise/Middleware/B2B
• Java/Java EE, Python/Django
• 2002
• Technical Consultant
• Process Mentor • Speaker
It’s about you!
Have you written a code recently?
Yes / No / Never
It doesn’t matter!
Agenda
• Background
• Concepts
• Basics
• Demo
for / else
for ( int i = 0; i < 10; i++ ) {
……
} else {
……
}
Back in 1989
http://www.python.org/~guido/images/guido91.gif
Guido van Rossum
https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRsHF89zT2tSdsm45hFYDaJPocfpwIM_Eh76pjKbTzZI2dxArWa
http://upload.wikimedia.org/wikipedia/en/9/9a/CompleteFlyingCircusDVD.jpg
High Level Language
Dynamic / Scripting Language
• 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 aren't 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 you're Dutch.
• Now is better than never.
• Although never is often better than *right* now.
• If the implementation is hard to explain, it's a bad idea.
• If the implementation is easy to explain, it may be a good idea.
• Namespaces are one honking great idea -- let's do more of those!
Zen of Python
Why?
Clean / Clear Syntax
Less Key Words
31
Portable
• Servers / Desktops / Mobile Devices / Raspberry Pi • Windows/Unix/Linux/Mac • Pre-installed in Mac and Linux
Multi-Language Support
• CPython • Jython • IronPython
Multi-Paradigm
• Functional / Procedural / Object Oriented
Easy to Learn
Highly Readable
No Variable Declaration
More Productivity
...the lines of Python code were 10% of the equivalent C++ code. - Greg Stein
Batteries Included
Open Source
Where I can use? • Stand-Alone Application
• Desktop/GUI Application
• Web Application
• XML Processing
• Database Application
• Network Application
• Scientific Application
• Gaming
• Robotics
Some One using it?
Hello World!
Installation / Tools
• Interpreter • Script Editor
.py .pyc .pyo .pyd
Optimized Code
• Faster Execution • Less CPU Cycles
Increment ‘i’ by 1
i++
i = i + 1
ADD #1,A1 [A1] ← [A1] + 1
Machine Instructions
/
Line of Code (LoC)
Assembly Language
System Languages
Scripting Languages
1 - 2
3 - 7
100 - 1K
Current Versions
2.7.3 & 3.3.0
The Prompt
>>>
How to run?
C:\> python my_program.py
• Start / Exit
• Simple print
• Zen of Python
• Keywords
• Help
Let’s Start
Variables
int int_ex = 10;
<type> var_name = val;
int_ex = 10
int_ex = 10
long_ex = 1000000000000L
float_ex = 1.1
string_ex = “Welcome”
boolean_ex = True
type()
print type(int_ex)
print type(long_ex)
print type(float_ex)
print type(string_ex)
print type(boolean_ex)
boolean_one = True
boolean_two = False
print boolean_one and boolean_two
print boolean_one or boolean_two
print not boolean_two
int_one = 11
int_two = 5
print int_one / int_two
print float(int_one) / float(int_two)
print float(int_one) / int_two
print int_one / float(int_two)
type casting
int_ex = 11
float_ex = 8.8
boolean_ex = True
print float (int_ex)
print int (float_ex)
print int (boolean_ex)
int_one = 11
int_two = 5
print int_one + int_two
print int_one - int_two
print int_one * int_two
print int_one / int_two
print int_one % int_two
print int_one ** int_two
int_one = 11
int_two = 5
print int_one > int_two
print int_one >= int_two
print int_one < int_two
print int_one <= int_two
print int_one == int_two
print int_one != int_two
Lets do some Maths
import math
(or)
from math import sqrt
dir (math)
int_one = 11
int_two = 5
import math
print math.sqrt (int_one)
from math import factorial
print factorial (int_two)
import math
print math.pi
Lets Talk to the User
raw_input()
(or)
raw_input([prompt_string])
user_input = raw_input()
print “You have Entered “ , user_input
user_input = raw_input(“Input Please : “)
print type(user_input)
int_one = raw_input (“Number 1 : “)
int_two = raw_input (“Number 2 : “)
print int_one + int_two
print int(int_one) + int(int_two)
The Nightmare Strings
“Hey, What’s up?”
‘ ’
“ ”
“““ ”””
‘‘‘ ’’’
print ‘ example text ’
print “ example text ”
print “““ example of long …..
…. text ”””
print ‘‘‘ example of long …..
…. text ’’’
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 ” ’’’
Don’t Mess with Quotes
Collections
0 1 2 3 4 5
-6 -1 -2 -3 -4 -5
10.1 10 ‘A’ ‘ABC’ 2 True
0 1 2 3 4 5
-6 -1 -2 -3 -4 -5
10.1 10 ‘A’ ‘ABC’ 2 True
list_ex = [ 10, 10.1, ‘A’, ‘ABC’, 2, True ]
tuple_ex = ( 10, 10.1, ‘A’, ‘ABC’, 2, True )
List [] vs Tuple ()
0 1 2 3 4 5
-6 -1 -2 -3 -4 -5
10.1 10 ‘A’ ‘ABC’ 2 True
tuple_ex = ( 10, 10.1, ‘A’, ‘ABC’, 2, True )
• tuple_ex[0]
• tuple_ex[1]
• tuple_ex[2]
• tuple_ex[3]
• tuple_ex[4]
• tuple_ex[5]
• tuple_ex[-6]
• tuple_ex[-5]
• tuple_ex[-4]
• tuple_ex[-3]
• tuple_ex[-2]
• tuple_ex[-1]
Tuple / List Tricks
tuple_ex = (100, )
list_ex = [100, ]
tuple_ex = tuple(‘gobi’)
list_ex = list(‘gobi’)
Lets Slice!
0 1 2 3 4 5
-6 -1 -2 -3 -4 -5
9
list_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:]
6 7 8 9
1 2 3 4 5 6 7 8 10
-7 -8 -9 -10
0 1 2 3 4 5
-6 -1 -2 -3 -4 -5
9
list_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]
6 7 8 9
1 2 3 4 5 6 7 8 10
-7 -8 -9 -10
0 1 2 3 4 5
-6 -1 -2 -3 -4 -5
9
list_ex = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, ]
print list_ex[0:10:2]
print list_ex[1:10:2]
6 7 8 9
1 2 3 4 5 6 7 8 10
-7 -8 -9 -10
0 1 2 3 4 5
-6 -1 -2 -3 -4 -5
9
list_ex = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, ]
print len(list_ex)
print max(list_ex)
print min(list_ex)
6 7 8 9
1 2 3 4 5 6 7 8 10
-7 -8 -9 -10
Lets Join!
city = list(‘gobi’)
city[len(city):] = “chettipalayam”
“”.join(city)
ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’, ‘SRH’,]
“, ”.join(ipl_teams)
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)
ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,]
ipl_teams.append(‘SRH’)
ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,]
ipl_teams.remove(‘SRH’)
del ipl_teams[-1]
ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,]
ipl_teams.insert (1, ‘SRH’)
ipl_teams = [‘CSK’, ‘SRH’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,]
ipl_teams.sort()
The Control Flow
Lets begin with a
Search
list_ex = [1, 2, 3, 4, 5]
print 3 in list_ex1
title_winners = [‘CSK’, ‘RR’, ‘DC’, ‘KKR’]
print ‘CSK’ in title_winners
my_city = “Gobichettipalayam”
print ‘chetti’ in my_city
while
i = 1
while i <= 10:
print i,
i = i + 1
for
for i in range(1, 11):
print i,
for i in range(11):
if i % 2 == 0: print i
for i in range(11):
if i % 2 == 0: print “Even : ”, i
else : print “Odd: ”, i
for i in range(11):
if i == 0: pass
elif i % 2 == 0: print “Even : ”, i
else : print “Odd: ”, i
for i in range(11):
if i == 0: continue
elif i % 2 == 0: print “Even : ”, i
else : print “Odd: ”, i
for i in range(11):
if i == 0: break
elif i % 2 == 0: print “Even : ”, i
else : print “Odd: ”, i
title_winners = ['CSK', 'RR', 'DC', 'KKR']
for team in title_winners:
print team,
for i in range(11):
print i,
else:
print “No Break executed in for”
Null Factor None
list_ex = []
if list_ex:
print “List is not Empty”
else:
print “List is Empty”
list_ex = None
if list_ex:
print “List is not None”
else:
print “List is None”
Error Handling
The OO Python
Thank You! [email protected]
bit.ly/sivasubramaniam bit.ly/sivaa_in
References
http://www.slideshare.net/narendra.sisodiya/python-presentation-presentation http://www.f-106deltadart.com/photo_gallery/var/albums/NASA-Research/nasa_logo.png?m=1342921398 http://www.seomofo.com/downloads/new-google-logo-knockoff.png http://www.huntlogo.com/wp-content/uploads/2011/11/US-Navy-Logo.png http://3.bp.blogspot.com/_7yB-eeGviiI/TUR471pyRpI/AAAAAAAAIBI/X705lo5Gjqc/s1600/Yahoo_Logo24.JPG http://www.thetwowayweb.com/wp-content/uploads/2012/09/youtube_logo.png