Introduction to Programming with Python
K.K. Biswas, IIT Delhi
15
15
PYTHON IS A PROGRAMMING LANGUAGE
K.K. Biswas, IIT Delhi 16
What is a program?
Sequence of instructions to perform some computation q p p 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) Cost of shirts = 250 x 9 = 2250 Balance = Amount Cost of shirts = 100
K.K. Biswas, IIT Delhi
17
Skeleton of a program
Amount = 2360 Shirts = 2360/250 = 9 Cost of shirts = 250 x 9 = 2250 Balance = Amount Cost of shirts = 100
K.K. Biswas, IIT Delhi
18
a python program
a = 2360 c = 250 x=a/c t=c*x b = a t print b
K.K. Biswas, IIT Delhi
19
a better python program
amount = 2360 cost_ shirt = 250 shirts = amount / cost_ shirt hi hi total_cost = cost_ shirt * shirts balance = amount total_cost print balance
K.K. Biswas, IIT Delhi
20
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 high value (WRONG !) can not have a space also key words not allowed (See page 11 of Think Python )
K.K. Biswas, IIT Delhi
23
Data Types
236 5000 7.62 0.025 611285.3 ( ) (int) (int) (floating point) (floating point) (floating point) (string) (string) (string)
hello, world ! grade is fine grade fine my marks are
K.K. Biswas, IIT Delhi
24
Assignment Statements
The syntax of an assignment statement is that y g 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 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)
K.K. Biswas, IIT Delhi 25
Assignment Statements
shirts = amount / cost_ shirt amount / cost_ shirt is an expression (this means, variable shirts is assigned the value of the above expression) total_cost = cost_ shirt * shirts cost shirt * shirts is another expression cost_
K.K. Biswas, IIT Delhi
26
Assignment Statements
age g = age + 1 g (this means age is assigned the value of the expression age + 1) You can write variable = variable x = age BUT you cannot write expression = variable age + 1 = age ( WRONG !)
K.K. Biswas, IIT Delhi 27
statements in python
profit = sale price cost price sale_price cost_price print profit
Statements
assignment statements print statements
K.K. Biswas, IIT Delhi
28
Operators and Operands
y=b+k ( 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) = 9 and not 9 44 9.44 p = m ** 3 ( m raised to power 3) EXPONENTIATION
K.K. Biswas, IIT Delhi
29
Operators and Operands
How to get the remainder of a division operation? g p 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 23/6 = 3 23%6 = 5
K.K. Biswas, IIT Delhi 30
Order of Operations
The PEMDAS rule 6 + 8 * (3 + 7) 2 % 9 ** 4
Parenthesis are highest order P th i hi h t d Exponentiation Multiplication, Division, (Mod operators) ( ) Addition and Subtraction Operators with same precedence evaluated left to right
K.K. Biswas, IIT Delhi 31
Order of Operations
To handle the arithmetic expressions p p y, the computer p properly, p uses the following order of precedence while doing left to right evaluations. ( )
* / %
Operators with same precedence evaluated left to right
K.K. Biswas, IIT Delhi 32
solving a mathematical expression
7 / 3 * 1.2 + 3 / 2 7 / 3 * 1.2 + 3 / 2 1 2 2 * 1.2 + 3 / 2 2.4 2.4 + 3 / 2 + 3.4
K.K. Biswas, IIT Delhi 33
solving a mathematical expression
2 + 8 * (12 5 ) % 10 2+8* 7 % 10 2 + 56 % 10 2+6
K.K. Biswas, IIT Delhi
34
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 1.0 c=2 f = 14.0 g = 2.5 r = 2.25
K.K. Biswas, IIT Delhi 35
solving arithmetic expressions
Try to evaluate the following expressions 4 +6*2 4*6+2 49 / 6 + 1 8/2*6 8*2/6 8 73 % 9 / 3
K.K. Biswas, IIT Delhi
36
string operations
x = alpha alpha y = beta print x + y alphabeta print y * 3 betabetabeta
K.K. Biswas, IIT Delhi
37
The Comment statement Comment
comment statements are used to explain the program steps. p p g p Basic idea is to help the reader understand the code. Example: Suppose a student gets 43 marks out of 75 and we are computing percentage of marks obtained. ti t f k bt i d # this calculates percentage of marks percent = ( marks * 100) / 75 0 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
K.K. Biswas, IIT Delhi 38
sample program
Find the volume of a sphere with radius 5. An approximate value would do. r=5 pi = 3.14 PI r r volume = (4.0/3) *PI *r * r* r print volume
K.K. Biswas, IIT Delhi
39
sample program
A student gets 92, 72, 83, and 65 in Maths, English, Physics g , , , , g , y and Chemistry. Add 5 marks to science subjects and find the average marks obtained by him. math = 92 th eng = 72 phy = 83 chem = 65 phy = phy + 5 p y p y chem = chem +5 average = ( math + eng + phy + chem)/ 5.0 print average
K.K. Biswas, IIT Delhi 40
sample program
Write a program which uses a persons age to p p g p g print number of years left for retirement (a person retires at 65). age = 45 print you have , 65 age , years until retirement. You can ask the age from the user as well age = input("How old are you? ") print your age is , age your , print you have , 65 age , years until retirement.
K.K. Biswas, IIT Delhi 41
Class exercise
A students camp has got 3 divisions of girls and 5 divisions of p g g boys. Write a program which asks the user to input number of boys and girls in each division. It should print h ld i t number of girls, number of boys and total number of students.
K.K. Biswas, IIT Delhi
42
Exercise
Exercise: Write a Python program that prompts the user for y p g p p his/her amount of money, then reports how many jean pants the person can afford, and how much more money he/she will need to afford an additional jean pant (cost of jean pant = pant. 750) Write a program which converts 13 hours and 32 minutes into seconds.
K.K. Biswas, IIT Delhi
43
Debugging
Removing bugs (errors) from the program g g ( ) p g Principal = 34000 interest = principal * rate * time to r = 1/ 2b r = 1.0 / 2 * b temp 1 = 67 height = 0 width = area / height
K.K. Biswas, IIT Delhi 44
K.K. Biswas, IIT Delhi
45