Introduction to Python Programming - Computer Science and ...

adventurescoldSoftware and s/w Development

Nov 7, 2013 (3 years and 9 months ago)

99 views

Introduction to Python 
Programming 
Professor K.K. Biswas
f&
Department o
f
 computer Sc 
&
?Engg
1K.K. Biswas, IIT Delhi
WHYDOWENEED
WHY
  
DO
?
WE
?
NEED
??
PROGRAMMING? 
2K.K. Biswas, IIT Delhi
3K.K. Biswas, IIT Delhi
4K.K. Biswas, IIT Delhi
5K.K. Biswas, IIT Delhi
6K.K. Biswas, IIT Delhi
7K.K. Biswas, IIT Delhi
8K.K. Biswas, IIT Delhi
DVD WALKMAN
Computer controlled Chemical Plants
K.K. Biswas, IIT Delhi9
Computer controlled large textile plant
K.K. Biswas, IIT Delhi10
11K.K. Biswas, IIT Delhi
ComputerassistedGraphicArts
12K.K. Biswas, IIT Delhi
Computer

assisted

Graphic

Arts
Programsareusedin……
Programs
  
are
?
used
?
in……
•video games
•Animated movies (Jurassic park, Shrek, My friend Ganesha…)
•Running  Metro trains
•Flying Aircrafts
•Sending rockets and Controlling  satellites
•Controlling Robots
•Controlling  satellites
•Controlling power plants, chemical plants
•Computer aided design
•Buildings
•Automobiles
•Mechanical parts 
•ships
13K.K. Biswas, IIT Delhi
MoreApplicationsofProgramming
More
 
Applications
?
of
?
Programming
•Booking train tickets or flight tickets
•Accounting  and billing
•Communication
ilbilhVii

Ema
il
s, mo
bil
e p
h
one, T
V
 transm
i
ss
i
on
•Maintaining academic records of students

Librarymanagementsystems
Library
 
management
?
systems
•Hospital management systems
•Automated surveillance
•Entertainment  
•Graphic Art Design

Helpingsurgeonsandotherdoctors
Helping
 
surgeons
?
and
?
other
?
doctors
14K.K. Biswas, IIT Delhi
Introduction to Pro
g
rammin
g
Introduction to Pro
g
rammin
g
withPythonwithPython
with

Pythonwith

Python
15
15K.K. Biswas, IIT Delhi
PYTHONISAPROGRAMMING
PYTHON
 
IS
???
A
??
PROGRAMMING
??
LANGUAGE
K.K. Biswas, IIT Delhi16
Whatisaprogram?
What
 
is
?
a
?
program?
•Se
q
uence of instructions to 
p
erform some com
p
utation
qpp
•Example: A shirt costs Rs 250. How many shirts can one buy 
given Rs Rs 2350? What will be the balance amount left?
•Solution worked on paper: 
•Shirts = 2360/250  = 9  (and some money left)

Costofshirts250x92250

Cost
  
of
?
shirts
?= 
250
?
x
?
9
?=       
2250
•Balance = Amount –Cost of shirts = 100 
17K.K. Biswas, IIT Delhi
Skeletonofaprogram
Skeleton
 
of
?
a
?
program

Amount
=
2360
Amount
  
2360
•Shirts = 2360/250  = 9

Costofshirts=250x9=2250
Cost
  
of
?
shirts
?
=
?
250
?
x
?
9
?
=
???????
2250
•Balance = Amount –Cost of shirts = 100 
18K.K. Biswas, IIT Delhi
apythonprogram
a
 
python
?
program

a=2360
a
 
=
?
2360
•c = 250
/
•x = a 
/
?c
•t = c * x
•b  = a –t 

printb
print
 
b
19K.K. Biswas, IIT Delhi
abetterpythonprogram
a
 
better
?
python
?
program

amount=2360
amount
 
=
?
2360
•cost_ shirt = 250
hi/hi
•s
hi
rts = amount 
/
?cost_ s
hi
rt
•total_cost = cost_ shirt * shirts
•balance = amount –total_cost 

printbalance
print
 
balance
20K.K. Biswas, IIT Delhi
Making computers understand programs written
by us

Mostslideswithbrowncolouredheadings

Most

slides

with

brown

coloured

headings

have been adapted from internet.
21
.
21K.K. Biswas, IIT Delhi
Compiling and interpreting
Many languages require you to compile (translate) your program into a form
that the machine understands.
compil
e
execut
e
output
source code
Hello.java
byte code
Hello.class
Python is instead directly interpreted into machine instructions.
interpret
output
sourcecode
output
source

code
Hello.py
22
22K.K. Biswas, IIT Delhi
variablesinpython
variables
 
in
?
python
•amount = 2360
•amount  ( Variable)
•2360 ( value)
•Variable names 
–amt, b, A, amount5, amt66

amount_given,  num_boys,  numgirls
–23amount (WRONG !) can not start with a number
highvalue
(WRONG!)cannothaveaspace

high
 
value
?
(WRONG
?
!)
?
can
?
not
?
have
?
a
?
space
?
–also key words not allowed (See page 11 of Think Python )
23K.K. Biswas, IIT Delhi
DataTypes
Data
 
Types
•236
(
int
)
(
)
•5000 (int)
•7.62(floating point)
•0.025(floating point) 
•611285.3(floating point) 
•‘hello, world !’(string)


gradeisfine

(string)

grade
 
is
?
fine
(string)
•‘my marks are’(string)
24K.K. Biswas, IIT Delhi
AssignmentStatements
Assignment
  
Statements
•The s
y
ntax of an assi
g
nment statement is that  
yg
•a variable is always on the left hand side of an equal sign, 
•and the right hand side it could be
–a value
–another variable

anarithmeticexpression
an
 
arithmetic
?
expression
•variable  = expression
•amount = 2360
•(this means, amount is assigned the value 2360)
•x = amount 
•(this means,  x is assigned the value held by amount)
25K.K. Biswas, IIT Delhi
AssignmentStatements
Assignment
  
Statements
•shirts = amount 
/
?cost shirt
/
_
•amount / cost_ shirt  is an expression
•(this means,  variable shirtsis assigned the value of the above 
expression)
•total_cost= cost_ shirt * shirts

costshirt*shirts
isanotherexpression

cost

shirt
?
*
?
shirts
??
is
?
another
?
expression
26K.K. Biswas, IIT Delhi
AssignmentStatements
Assignment
  
Statements
•a
g
e = a
g
e + 1
g
g
•(this means  ageis assigned the value 
of the expression  age + 1)
•You can write 
•variable = variable 
•x  = age

BUTyoucannotwrite

BUT
  
you
?
cannot
?
write
?
•expression  = variable

age
+
1
=
age
(WRONG!)
age
  
1
??
age
??
(
?
WRONG
?
!)
27K.K. Biswas, IIT Delhi
statementsinpython
statements
 
in
?
python

profit
=
saleprice

costprice
profit
     
sale
_
price
  
cost
_
price
•print profit
•Statements

assignment statements
–print statements
28K.K. Biswas, IIT Delhi
OperatorsandOperands
Operators
 
and
?
Operands

y
 = b + k
y
•( b and k are called operands)
•z = y –a 
•w = y * z
•shirts = amount / cost_one
/
•shirts = 2360 
/
?250  
(Note: INTEGER DIVISION, result is always an integer) 
=9andnot944
=
?
9
??
and
?
not
?
9
.
44
•p = m ** 3  ( m raised to power 3) EXPONENTIATION
29K.K. Biswas, IIT Delhi
OperatorsandOperands
Operators
 
and
?
Operands
•How to 
g
et the remainder of a division o
p
eration?
gp
•use the mod operator  %
•a%b evaluates to the remainder of a divided by b. 

•13%5 = 3
•19%6 = 1
•14%7 = 0

19%200=19

19%200
 
=
?
19
•23/6  = 3

23%6
=
5
23%6
  
5
30K.K. Biswas, IIT Delhi
OrderofOperations
Order
 
of
?
Operations

ThePEMDASrule
The
 
PEMDAS
?
rule

6 + 8 * (3 + 7) –2 % 9 ** 4 
Pthihihtd

P
aren
th
es
i
s are 
hi
g
h
es
t
 or
d
er
•Exponentiation
()
•Multiplication, Division,              
(
Mod operators
)
•Addition and Subtraction
•Operators with same precedence evaluated left to 
right
31K.K. Biswas, IIT Delhi
OrderofOperations
Order
 
of
?
Operations
•To handle the arithmetic ex
p
ressions 
p
ro
p
erl
y,
 the com
p
uter 
pppy,p
uses the following order of precedence while doing left to 
right evaluations.

(   )
*
/
%
/
%
+  –
•Operators with same precedence evaluated left to 
right
32K.K. Biswas, IIT Delhi
solvingamathematicalexpression
solving
 
a
?
mathematical
?
expression
7 / 3 * 1.2 + 3 / 2


僧



















2* 1.2
+
3 / 2
2.4+ 3 / 2
2.4 + 1
3.4
33K.K. Biswas, IIT Delhi
solvingamathematicalexpression
solving
 
a
?
mathematical
?
expression
•2 + 8 * (12 –5 ) % 10 
•2 + 8 *      7        % 10 
•2 + 56                 % 10 
•2 + 6
34K.K. Biswas, IIT Delhi
Handlingintandfloatvalues
Handling
 
int
?
and
?
float
?
values
•a = 9
•b = 4
•d = 10.0
•e = 4
•c = a/b
•f = d + e
•g = d/b

r=a*10/b

r
 
=
?
a
?
*
?
1
.
0
 
/
?
b
•c = 2f = 14.0

g
=
2.5
r
=
2.25
g
  
2.5
r
??
2.25
K.K. Biswas, IIT Delhi35
solvingarithmeticexpressions
solving
 
arithmetic
?
expressions

Trytoevaluatethefollowingexpressions
Try
 
to
?
evaluate
?
the
?
following
?
expressions
•4  + 6 * 2

4
*
6+2
4
  
6
?
+
?
2
•49 /   6 + 1

8/2
*
6

8
 
/
?
2
??
6
•8 * 2 / 6

8
73%9/3

8
 

73
?
%
?
9
?
/
?
3
36K.K. Biswas, IIT Delhi
stringoperations
string
 
operations

x
=‘
alpha

x
  
alpha
•y = ‘beta’

printx+y
print
 
x
?
+
?
y
•alphabeta

printy
*
3

print
  
y
??
3
•betabetabeta
37K.K. Biswas, IIT Delhi
The

Comment

statement
The
Comment
statement
•comment statements are used to ex
p
lain the 
p
ro
g
ram ste
p
s. 
ppgp
Basic idea is to help the reader understand the code.
•Example: Suppose a student gets 43 marks out of 75 and we 
titfkbtid
are compu
ti
ng percen
t
age o
f
 mar
k
s o
bt
a
i
ne
d

•# this calculates percentage of marks

percent=(marks
*
100)/750
percent
 
=
?
(
?
marks
??
100)
?
/
?
75
.
0
•# marks is int value but percent is floating point
•percent = ( marks * 100) / 75.0      # percent is float value
•vel = 6 # velocity is in metres/sec
38K.K. Biswas, IIT Delhi
sampleprogram
sample
 
program

Findthevolumeofaspherewithradius5.An
Find
 
the
?
volume
?
of
?
a
?
sphere
?
with
?
radius
?
5.
?
An
?
approximate value would do.
•r = 5
•pi = 3.14

volume
=
(4.0/3)
*
PI
*
r
*
r
*
r
volume
  
(4.0/3)
?
PI
?
r
??
r
?
r
•print volume
39K.K. Biswas, IIT Delhi
sampleprogram
sample
 
program
•A student 
g
ets 92
,
 72
,
 83
,
 and 65 in Maths
,
 En
g
lish
,
 Ph
y
sics 
g,,,,g,y
and Chemistry.  Add 5 marks to science subjects and find the 
average marks obtained by him.
th92
•ma
th
 = 
92
•eng =  72

phy=83
phy
 
=
??
83
•chem = 65

p
h
y
 = 
p
h
y
 + 5
pypy
•chem = chem +5
•average = ( math + eng + phy + chem)/ 5.0
•print average
40K.K. Biswas, IIT Delhi
sampleprogram
sample
 
program
•Write a 
p
ro
g
ram which uses a 
p
erson’s a
g
e to 
p
rint number of 
pgpgp
years left for retirement (a person retires at 65).
•age = 45
•print ‘ you have ‘, 65 

age , ‘years until retirement.’

Youcanasktheagefromtheuseraswell

You
 
can
?
ask
?
the
?
age
?
from
?
the
?
user
?
as
?
well
•age = input("How old are you? ")

print

yourageis

,age
print

your

age

is

,

age
•print ‘ you have ‘, 65 –age , ‘years
until retirement.’
41K.K. Biswas, IIT Delhi
Classexercise
Class
 
exercise
•A  students cam
p
 has 
g
ot 3 divisions of 
g
irls and 5 divisions of 
pgg
boys. Write a program which asks the user to input number of 
boys and girls in each division. 
Ithldit

It
 s
h
ou
ld
 pr
i
n
t
 
•number of girls,

numberofboysand
number
 
of
?
boys
?
and
?
•total number of students. 
42K.K. Biswas, IIT Delhi
Exercise
Exercise
•Exercise: Write a P
y
thon 
p
ro
g
ram that 
p
rom
p
ts the user for 
ypgpp
his/her amount of money,  then reports how many jean pants 
the person can afford, and how much more money he/she will 
needtoaffordanadditionaljeanpant(costofjeanpant=
need
?
to
?
afford
?
an
?
additional
?
jean
?
pant

(cost
?
of
?
jean
?
pant
?
=
?
750)
•Write a program which converts 13 hours and 32 minutes into 
seconds. 
43K.K. Biswas, IIT Delhi
Debugging
Debugging
•Removin
g
 “bu
g
s” 
(
errors
)
 from the 
p
ro
g
ram
gg()pg
•Principal = 34000
•interest = principal * rate * time
•to r = 1/ 2b
/*
•r = 1.0 
/
?2 
*
?b

temp1=67

temp
  
1
?
=
?
67
?
•height = 0

width
=
area/height
width
  
area
?
/
?
height
44K.K. Biswas, IIT Delhi
45K.K. Biswas, IIT Delhi