Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Python Slides

No description

Harsha M Setty

on 5 June 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Python Slides

Bhaskar &
Harsha Basic Introduction to
Python Unwinding the basics
of the language In Python Style Python's principal author
Guido von Rossum The scripting programming language Python was named after the British comedy group Monty Python.

Still from Flying Circus History Creator Multiple Programming Models Salient Features of Python? Programming
Models Procedural Functional Multi Paradigm Reflective OOP Object oriented Programming language. Python Dynamic, strongly typed scripting language Automatic memory management Large and comprehensive standard library Free and open source Example: C#, C++, Java, Python,... A procedural program is composed of one or more units or modules, either user coded or provided in a code library; each module is composed of one or more procedures/function. Example: C++, C#, Java, Python, ... Functional programming languages define programs and subroutines as mathematical functions. Examples: Lisp, Clean, Python,... Multiparadigm languages support more than one programming paradigm. They allow a program to use more than one programming style. Example: C++, C#, Ruby, Python,... Reflective languages let programs examine and possibly modify their high level structure at runtime. Example: Java, Lisp, C#, Python,... Things to know Most of the examples shown is based on Python 2.7.x
You can perform any long integer operation on command prompt.

Python Who in Industry uses Python ? Problem Things to learn ? You've just feasted on a truly delicious meal. The restaurant's computer is down, however, so you'll need to compute the cost of your meal yourself.

Here's how it'll break down:
Cost of meal: Rs. 4400
Restaurant tax: 25% (Hint: 25/100)
Tip: 10%

Display in standard Bill format. Variables
Format printing Variables Integer
i = 5 + 5

myfloat = 7.0
myfloat = float(7)

s1 = 'literal string‘
s2 = “another string”

m = """multi-line big

us = u'üñîçø∂é string‘

f=False Array
vals = [ 1, 3, 5, 7, ]

t = ('a', 1, vals, None)

d = { 'a':'a value',
'b':'b value',
d['a'] Format Printing %s - String (or any object with a string representation)
%d - Integers
%f - Floating point numbers
%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot. Operators & Conditions +, -, *, /, **, %

==, !=, <, <=, >, >= Better Readability Whitespace for block structures
Minimal punctuation
Built-in documentation Code Blocks Minimal Punctuation Inline Documentation Control Flow If Code Block IF While For Code Block While Code Block For Tip: Just remember we need to put ":" where every we used "{}" in other
languages, and statements following 'if' should always be indented. Example:
if a == 1:
print "value of a is 1"
elif a == 2:
print "value of a is 2"
print "value of a is not 1 or 2" if <Condition>:
# Write your codeelif <Condition>:
#Write your codeelse: Write your code Syntax: Syntax:

for <loop-var> in <object> :
<statement-block> Example:

for i in range( 1, 5 ) :
print i Tip: Just remember we need to put ":" where every we used "{}" in other
languages, and statements following 'if' should always be indented. Syntax:
while <Condition> : # Your codeelse: #Your else code Example:
while x < 5 : print x x = x + 1
else : print 'i got here' While has an optional else clause which executes when the condition evaluates to false.
The optional else clause runs only if the loop exits normally (not by break). A dictionary is mutable container
Dictionaries consist of pairs of keys and their corresponding values.


dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
dict2 = { 'abc': 123, 98.6: 37 }
print "dict[‘Alice']: ", dict[‘Alice'];
print "dict2[98.6]: ", dict2[98.6]; Dictionaries Tuple is a sequence of immutable objects


tup1 = ('physics', 'chemistry', 1997, 2000)
print tup1[1:3] #Output: Chemistry, 1997, 2000
print tup1[-1] #Output: 2000
Functions: max, min, len, cmp, tuple(seq) Tuples print(mylist[0]) # prints 1
print(mylist[1]) # prints 2
print(mylist[2]) # prints 3
mylist[1]= 10
# prints out 1,10,3
for x in mylist:
print x Lists are very similar to arrays
Can hold any type of variable
Can be iterated over
Lists are mutable Lists Learning a programming language is only a part of the story. The difference is seen, only when you learn to use it to good effect. Functions Function blocks begin with the keyword def.
The first statement of a function can be an optional statement
The code block within every function starts with a colon (:) and is indented

def functionname( parameters ):
return [expression] Example:

mylist = []
mylist.append(3) Higher order Functions ZIP MAP REDUCE FILTER You can pass a function as a parameter into another function Map function does exactly what foreach does. Takes a list and outputs something “smaller” like a number. Construct a list from those elements of iterable for which function returns true. This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. Example:

def sqr_elem(x): return x*x
m=map(sqr_elem, [3,2,1,4]) Output:
m=[9,4,1,16] Example:
def add(x,y):
return x+y
reduce(add,[3,2,1,4],0) Output:
10 Example:
def odd(x)
return bool(x%2)
filter(odd,[7,2,6,9,34] Output:
[7,9] Example:
>>> z=[1,2,3]
>>> x=[9,8,7] Output:
>>> zip(x,z)
[(9, 1), (8, 2), (7, 3)]
>>> zip(z,x)
[(1, 9), (2, 8), (3, 7)] Tip: The higher order functions, map, reduce and filter all work on homogenous lists, i.e., lists with a single type of data in them. List Comprehension > vec_a=[4,5,6]
> vec_b=[5,7,3]
>[(x,y) for x in vec_a for y in vec_b] Output:
[(4, 5), (4, 7), (4, 3), (5, 5), (5, 7), (5, 3), (6, 5), (6, 7), (6, 3)]
Full transcript