04 Python 1
04 Python 1
Programming
Language
Python Overview
Python is a high level scripting language with object
oriented features and it is a interpreted language.
>>> 3 + 7
10
>>> 3 < 15
True
>>> 'print me'
'print me'
>>> print 'print me'
print me
>>>
print Statement
•Elements separated by commas print with a space
between them
>>> x = 7
>>> x
7
>>> x = 'hello'
>>> x
'hello'
>>>
Statements and Expressions
print: Output strings, integers, or any other datatype.
Comparing Strings:
Strings can be compared with the standard operators listed
==, !=, <, >, <=, and >=.
STRINGS
• + is overloaded to do concatenation
>>> x = 'hello'
>>> x = x + ' there'
>>> x
'hello there'
STRINGS
• Can use single or double quotes, and three double quotes for a
multi-line string
>>> print s
And me too!
though I am much longer
than the others :)„
STRINGS
Strings in Python can be subscripted just like an array: s[4] = ‘4'.
>>> s = '012345'
>>> s[3]
'3'
>>> s[1:4]
'123'
>>> s[2:]
'2345'
>>> s[:4]
'0123'
STRINGS
• len(String) – returns the number of characters in the
String
>>> s = „012345‟
>>> len(s)
6
>>> str(10.3)
'10.3'
LISTS
• Arrays in basic Python are actually lists that can contain
mixed datatypes.
• Creating lists: A list can be created by defining it with [ ].
A numbered list can also be created with the range
function which takes start and stop values and an
increment.
print "Hi %s! You are %d years old!" % (name, 2017 - birthyear)
~: python input.py
What's your name?
> Michael
What year were you born?
>1980
Hi Michael! You are 31 years old!
FILES
FILE INPUT
Reads N bytes
S = inflobj.read(N)
(N >= 1)
FILE OUTPUT
import math
x = 30
if x < 30 :
print “FALSE”
elif x > 30 :
print “FALSE”
else :
print “TRUE”
Control Statements
While Loops
x=1
while x < 10 :
print x
x=x+1
Control Statements
for Loops
def max(x,y) :
if x < y :
return x
else :
return y
Functions are first class objects
• Can be assigned to a variable
• Can be passed as a parameter
• Can be returned from a function
• Functions are treated like any other variable in
Python, the def statement simply assigns a
function to a variable
Function names are like any variable
>>> x = 10
>>> x
10
• Functions are objects >>> def x () :
• The same reference ... print 'hello'
rules hold for them as >>> x
for other objects <function x at 0x619f0>
>>> x()
hello
>>> x = 'blah'
>>> x
'blah'
Functions as Parameters
funcinfunc.py
Functions Returning Functions
def foo (x) :
def bar(y) :
return x + y ~: python funcreturnfunc.py
return bar <function bar at 0x612b0>
# main 5
f = foo(3)
print f
print f(2)
funcreturnfunc.py
Parameters: Defaults
• Parameters can be
>>> def foo(x = 3) :
assigned default ... print x
values ...
• They are overridden if >>> foo()
a parameter is given 3
>>> foo(10)
for them 10
• The type of the default >>> foo('hello')
doesn’t limit the type hello
of a parameter
Parameters: Named
• Call by name >>> def foo (a,b,c) :
• Any positional ... print a, b, c
arguments must ...
come before >>> foo(c = 10, a = 2, b = 14)
2 14 10
named ones in a
>>> foo(3, c = 2, b = 19)
call 3 19 2
Anonymous Functions
• A lambda
expression returns a
>>> f = lambda x,y : x + y
function object >>> f(2,3)
• The body can only 5
be a simple >>> lst = ['one', lambda x : x * x, 3]
expression, not >>> lst[1](4)
complex statements 16
THANK YOU