Python for High School Programmers

106
Python for High School Programmers Sivasubramaniam Arunachalam April 06, 2013 @sivaa_in

description

 

Transcript of Python for High School Programmers

Page 1: Python for High School Programmers

Python for High School Programmers

Sivasubramaniam Arunachalam

April 06, 2013

@sivaa_in

Page 2: Python for High School Programmers

It’s me!

• Application Developer

• Web/Enterprise/Middleware/B2B

• Java/Java EE, Python/Django

• 2002

• Technical Consultant

• Process Mentor • Speaker

Page 3: Python for High School Programmers

It’s about you!

Have you written a code recently?

Yes / No / Never

It doesn’t matter!

Page 4: Python for High School Programmers

Agenda

• Background

• Concepts

• Basics

• Demo

Page 5: Python for High School Programmers

for / else

for ( int i = 0; i < 10; i++ ) {

……

} else {

……

}

Page 6: Python for High School Programmers

Back in 1989

Page 7: Python for High School Programmers

http://www.python.org/~guido/images/guido91.gif

Guido van Rossum

Page 9: Python for High School Programmers

http://upload.wikimedia.org/wikipedia/en/9/9a/CompleteFlyingCircusDVD.jpg

Page 10: Python for High School Programmers

High Level Language

Page 11: Python for High School Programmers

Dynamic / Scripting Language

Page 12: Python for High School Programmers

• 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

Page 13: Python for High School Programmers

Why?

Page 14: Python for High School Programmers

Clean / Clear Syntax

Page 15: Python for High School Programmers

Less Key Words

31

Page 16: Python for High School Programmers

Portable

• Servers / Desktops / Mobile Devices / Raspberry Pi • Windows/Unix/Linux/Mac • Pre-installed in Mac and Linux

Page 17: Python for High School Programmers

Multi-Language Support

• CPython • Jython • IronPython

Page 18: Python for High School Programmers

Multi-Paradigm

• Functional / Procedural / Object Oriented

Page 19: Python for High School Programmers

Easy to Learn

Page 20: Python for High School Programmers

Highly Readable

Page 21: Python for High School Programmers

No Variable Declaration

Page 22: Python for High School Programmers

More Productivity

...the lines of Python code were 10% of the equivalent C++ code. - Greg Stein

Page 23: Python for High School Programmers

Batteries Included

Page 24: Python for High School Programmers

Open Source

Page 25: Python for High School Programmers

Where I can use? • Stand-Alone Application

• Desktop/GUI Application

• Web Application

• XML Processing

• Database Application

• Network Application

• Scientific Application

• Gaming

• Robotics

Page 26: Python for High School Programmers

Some One using it?

Page 27: Python for High School Programmers
Page 28: Python for High School Programmers

Hello World!

Page 29: Python for High School Programmers

Installation / Tools

• Interpreter • Script Editor

Page 30: Python for High School Programmers

.py .pyc .pyo .pyd

Page 31: Python for High School Programmers

Optimized Code

• Faster Execution • Less CPU Cycles

Page 32: Python for High School Programmers

Increment ‘i’ by 1

i++

i = i + 1

ADD #1,A1 [A1] ← [A1] + 1

Page 33: Python for High School Programmers

Machine Instructions

/

Line of Code (LoC)

Page 34: Python for High School Programmers

Assembly Language

System Languages

Scripting Languages

1 - 2

3 - 7

100 - 1K

Page 35: Python for High School Programmers

Current Versions

2.7.3 & 3.3.0

Page 36: Python for High School Programmers

The Prompt

>>>

Page 37: Python for High School Programmers

How to run?

C:\> python my_program.py

Page 38: Python for High School Programmers

• Start / Exit

• Simple print

• Zen of Python

• Keywords

• Help

Let’s Start

Page 39: Python for High School Programmers

Variables

Page 40: Python for High School Programmers

int int_ex = 10;

<type> var_name = val;

int_ex = 10

Page 41: Python for High School Programmers

int_ex = 10

long_ex = 1000000000000L

float_ex = 1.1

string_ex = “Welcome”

boolean_ex = True

Page 42: Python for High School Programmers

type()

Page 43: Python for High School Programmers

print type(int_ex)

print type(long_ex)

print type(float_ex)

print type(string_ex)

print type(boolean_ex)

Page 44: Python for High School Programmers

boolean_one = True

boolean_two = False

print boolean_one and boolean_two

print boolean_one or boolean_two

print not boolean_two

Page 45: Python for High School Programmers

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)

Page 46: Python for High School Programmers

type casting

Page 47: Python for High School Programmers

int_ex = 11

float_ex = 8.8

boolean_ex = True

print float (int_ex)

print int (float_ex)

print int (boolean_ex)

Page 48: Python for High School Programmers

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

Page 49: Python for High School Programmers

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

Page 50: Python for High School Programmers

Lets do some Maths

Page 51: Python for High School Programmers

import math

(or)

from math import sqrt

Page 52: Python for High School Programmers

dir (math)

Page 53: Python for High School Programmers

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

Page 54: Python for High School Programmers

Lets Talk to the User

Page 55: Python for High School Programmers

raw_input()

(or)

raw_input([prompt_string])

Page 56: Python for High School Programmers

user_input = raw_input()

print “You have Entered “ , user_input

user_input = raw_input(“Input Please : “)

print type(user_input)

Page 57: Python for High School Programmers

int_one = raw_input (“Number 1 : “)

int_two = raw_input (“Number 2 : “)

print int_one + int_two

print int(int_one) + int(int_two)

Page 58: Python for High School Programmers

The Nightmare Strings

Page 59: Python for High School Programmers

“Hey, What’s up?”

Page 60: Python for High School Programmers

‘ ’

“ ”

“““ ”””

‘‘‘ ’’’

Page 61: Python for High School Programmers

print ‘ example text ’

print “ example text ”

print “““ example of long …..

…. text ”””

print ‘‘‘ example of long …..

…. text ’’’

Page 62: Python for High School Programmers

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 ” ’’’

Page 63: Python for High School Programmers

Don’t Mess with Quotes

Page 64: Python for High School Programmers

Collections

Page 65: Python for High School Programmers

0 1 2 3 4 5

-6 -1 -2 -3 -4 -5

10.1 10 ‘A’ ‘ABC’ 2 True

Page 66: Python for High School Programmers

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 )

Page 67: Python for High School Programmers

List [] vs Tuple ()

Page 68: Python for High School Programmers

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]

Page 69: Python for High School Programmers

Tuple / List Tricks

Page 70: Python for High School Programmers

tuple_ex = (100, )

list_ex = [100, ]

tuple_ex = tuple(‘gobi’)

list_ex = list(‘gobi’)

Page 71: Python for High School Programmers

Lets Slice!

Page 72: Python for High School Programmers

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

Page 73: Python for High School Programmers

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

Page 74: Python for High School Programmers

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

Page 75: Python for High School Programmers

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

Page 76: Python for High School Programmers

Lets Join!

Page 77: Python for High School Programmers

city = list(‘gobi’)

city[len(city):] = “chettipalayam”

“”.join(city)

Page 78: Python for High School Programmers

ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’, ‘SRH’,]

“, ”.join(ipl_teams)

Page 79: Python for High School Programmers

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)

Page 80: Python for High School Programmers

ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,]

ipl_teams.append(‘SRH’)

Page 81: Python for High School Programmers

ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,]

ipl_teams.remove(‘SRH’)

del ipl_teams[-1]

Page 82: Python for High School Programmers

ipl_teams = [‘CSK’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,]

ipl_teams.insert (1, ‘SRH’)

Page 83: Python for High School Programmers

ipl_teams = [‘CSK’, ‘SRH’, ‘DD’, ‘KXIP’, ‘KKR’, ‘MI’, ‘PWI’, ‘RR’, ‘RCB’,]

ipl_teams.sort()

Page 84: Python for High School Programmers

The Control Flow

Page 85: Python for High School Programmers

Lets begin with a

Search

Page 86: Python for High School Programmers

list_ex = [1, 2, 3, 4, 5]

print 3 in list_ex1

Page 87: Python for High School Programmers

title_winners = [‘CSK’, ‘RR’, ‘DC’, ‘KKR’]

print ‘CSK’ in title_winners

Page 88: Python for High School Programmers

my_city = “Gobichettipalayam”

print ‘chetti’ in my_city

Page 89: Python for High School Programmers

while

Page 90: Python for High School Programmers

i = 1

while i <= 10:

print i,

i = i + 1

Page 91: Python for High School Programmers

for

Page 92: Python for High School Programmers

for i in range(1, 11):

print i,

Page 93: Python for High School Programmers

for i in range(11):

if i % 2 == 0: print i

Page 94: Python for High School Programmers

for i in range(11):

if i % 2 == 0: print “Even : ”, i

else : print “Odd: ”, i

Page 95: Python for High School Programmers

for i in range(11):

if i == 0: pass

elif i % 2 == 0: print “Even : ”, i

else : print “Odd: ”, i

Page 96: Python for High School Programmers

for i in range(11):

if i == 0: continue

elif i % 2 == 0: print “Even : ”, i

else : print “Odd: ”, i

Page 97: Python for High School Programmers

for i in range(11):

if i == 0: break

elif i % 2 == 0: print “Even : ”, i

else : print “Odd: ”, i

Page 98: Python for High School Programmers

title_winners = ['CSK', 'RR', 'DC', 'KKR']

for team in title_winners:

print team,

Page 99: Python for High School Programmers

for i in range(11):

print i,

else:

print “No Break executed in for”

Page 100: Python for High School Programmers

Null Factor None

Page 101: Python for High School Programmers

list_ex = []

if list_ex:

print “List is not Empty”

else:

print “List is Empty”

Page 102: Python for High School Programmers

list_ex = None

if list_ex:

print “List is not None”

else:

print “List is None”

Page 103: Python for High School Programmers

Error Handling

Page 104: Python for High School Programmers

The OO Python

Page 105: Python for High School Programmers

Thank You! [email protected]

bit.ly/sivasubramaniam bit.ly/sivaa_in

Page 106: Python for High School Programmers

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