Python 1
Python 1
15
15
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
17
Skeleton of a program
Amount = 2360 Shirts = 2360/250 = 9 Cost of shirts = 250 x 9 = 2250 Balance = Amount Cost of shirts = 100
18
a python program
a = 2360 c = 250 x=a/c t=c*x b = a t print b
19
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 )
23
Data Types
236 5000 7.62 0.025 611285.3 ( ) (int) (int) (floating point) (floating point) (floating point) (string) (string) (string)
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_
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
28
29
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. ( )
* / %
34
36
string operations
x = alpha alpha y = beta print x + y alphabeta print y * 3 betabetabeta
37
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
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.
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.
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
45