ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro...

29
Introduction to Programming Introduction to Programming with Python with Python with Python with Python 15 15 K.K. Biswas, IIT Delhi

Transcript of ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro...

Page 1: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Introduction to ProgrammingIntroduction to Programmingwith Pythonwith Pythonwith Pythonwith Python

1515K.K. Biswas, IIT Delhi

Page 2: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

PYTHON IS A PROGRAMMINGPYTHON IS A PROGRAMMINGLANGUAGE

K.K. Biswas, IIT Delhi 16

Page 3: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

What is a program?What is a program?

• Sequence of instructions to perform some computationq 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• Cost of shirts = 250 x 9 = 2250• Balance = Amount �– Cost of shirts = 100

17K.K. Biswas, IIT Delhi

Page 4: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Skeleton of a programSkeleton of a program

• Amount = 2360Amount 2360• Shirts = 2360/250 = 9• Cost of shirts = 250 x 9 = 2250Cost of shirts = 250 x 9 = 2250• Balance = Amount �– Cost of shirts = 100

18K.K. Biswas, IIT Delhi

Page 5: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

a python programa python program

• a = 2360a = 2360• c = 250

/• x = a / c• t = c * x• b = a �– t• print bprint b

19K.K. Biswas, IIT Delhi

Page 6: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

a better python programa better python program

• amount = 2360amount = 2360• cost_ shirt = 250

hi / hi• shirts = amount / cost_ shirt• total_cost = cost_ shirt * shirts• balance = amount �– total_cost• print balanceprint balance

20K.K. Biswas, IIT Delhi

Page 7: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

variables in pythonvariables 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– high value (WRONG !) can not have a space– also key words not allowed (See page 11 of Think Python )

23K.K. Biswas, IIT Delhi

Page 8: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Data TypesData Types

• 236 (int)( )• 5000 (int)• 7.62 (floating point)• 0.025 (floating point)• 611285.3 (floating point)

• �‘hello, world !�’ (string)• �‘grade is fine�’ (string)• grade is fine (string)• �‘my marks are�’ (string)

24K.K. Biswas, IIT Delhi

Page 9: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Assignment StatementsAssignment Statements

• The syntax of an assignment statement is thaty 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 expressionan 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

Page 10: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Assignment StatementsAssignment 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_ shirt * shirts is another expression

26K.K. Biswas, IIT Delhi

Page 11: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Assignment StatementsAssignment Statements

• age = age + 1g g• (this means age is assigned the value

of the expression age + 1)• You can write• variable = variable• x = age

• BUT you cannot write• BUT you cannot write• expression = variable• age + 1 = age ( WRONG !)age 1 age ( WRONG !)

27K.K. Biswas, IIT Delhi

Page 12: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

statements in pythonstatements in python

• profit = sale price �– cost priceprofit sale_price cost_price• print profit

• Statements– assignment statements– print statements

28K.K. Biswas, IIT Delhi

Page 13: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Operators and OperandsOperators and Operands

• y = b + ky• ( 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 and not 9.44

• p = m ** 3 ( m raised to power 3) EXPONENTIATION

29K.K. Biswas, IIT Delhi

Page 14: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Operators and OperandsOperators 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• 19%200 = 19• 23/6 = 3• 23%6 = 523%6 5

30K.K. Biswas, IIT Delhi

Page 15: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Order of OperationsOrder of Operations

• The PEMDAS ruleThe PEMDAS rule• 6 + 8 * (3 + 7) �– 2 % 9 ** 4P th i hi h t d• Parenthesis are highest order

• Exponentiation( )• Multiplication, Division, (Mod operators)

• Addition and Subtraction• Operators with same precedence evaluated left toright

31K.K. Biswas, IIT Delhi

Page 16: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Order of OperationsOrder of Operations

• To handle the arithmetic expressions properly, the computerp p p y, puses the following order of precedence while doing left toright evaluations.

•( )* / %/ %

+ �–

• Operators with same precedence evaluated left toright

32K.K. Biswas, IIT Delhi

Page 17: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

solving a mathematical expressionsolving a mathematical expression

7 / 3 * 1.2 + 3 / 2

7 / 3 * 1 2 + 3 / 27 / 3 * 1.2 + 3 / 2

2 * 1.2 + 3 / 2

2.4 + 3 / 2

2.4 + 1

3.433K.K. Biswas, IIT Delhi

Page 18: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

solving a mathematical expressionsolving a mathematical expression

• 2 + 8 * (12 �– 5 ) % 10• 2 + 8 * 7 % 10• 2 + 56 % 10• 2 + 6

34K.K. Biswas, IIT Delhi

Page 19: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Handling int and float valuesHandling int and float values

• a = 9• b = 4• d = 10.0• e = 4• c = a/b• f = d + e• g = d/b• r = a * 1 0 / b• r = a * 1.0 / b• c = 2 f = 14.0• g = 2.5 r = 2.25g 2.5 r 2.25

K.K. Biswas, IIT Delhi 35

Page 20: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

solving arithmetic expressionssolving arithmetic expressions

• Try to evaluate the following expressionsTry to evaluate the following expressions• 4 + 6 * 2• 4 * 6 + 24 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

Page 21: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

string operationsstring operations

• x = �‘alpha�’x alpha• y = �‘beta�’• print x + yprint x + y• alphabeta• print y * 3• print y 3• betabetabeta

37K.K. Biswas, IIT Delhi

Page 22: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

The �“Comment�” statementThe Comment statement

• comment statements are used to explain the program steps.p p g pBasic idea is to help the reader understand the code.

• Example: Suppose a student gets 43 marks out of 75 and weti t f k bt i dare computing percentage of marks obtained.

• # this calculates percentage of marks• percent = ( marks * 100) / 75 0percent = ( 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

Page 23: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

sample programsample program

• Find the volume of a sphere with radius 5. AnFind the volume of a sphere with radius 5. Anapproximate value would do.

• r = 5• pi = 3.14• volume = (4.0/3) *PI *r * r* rvolume (4.0/3) PI r r r• print volume

39K.K. Biswas, IIT Delhi

Page 24: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

sample programsample program

• A student gets 92, 72, 83, and 65 in Maths, English, Physicsg , , , , g , yand Chemistry. Add 5 marks to science subjects and find theaverage marks obtained by him.

th 92• math = 92• eng = 72• phy = 83phy = 83• chem = 65• phy = phy + 5p y p y• chem = chem +5• average = ( math + eng + phy + chem)/ 5.0• print average

40K.K. Biswas, IIT Delhi

Page 25: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

sample programsample program

• Write a program which uses a person�’s age to print number ofp g p g pyears 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• You can ask the age from the user as well• age = input("How old are you? ")• print ‘your age is ‘, ageprint your age is , age• print ‘ you have ‘, 65 – age , ‘years until retirement.’

41K.K. Biswas, IIT Delhi

Page 26: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

Class exerciseClass exercise

• A students camp has got 3 divisions of girls and 5 divisions ofp g gboys. Write a program which asks the user to input number ofboys and girls in each division.It h ld i t• It should print

• number of girls,• number of boys andnumber of boys and• total number of students.

42K.K. Biswas, IIT Delhi

Page 27: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

ExerciseExercise• Exercise:Write a Python program that prompts the user fory p g p p

his/her amount of money, then reports how many jean pantsthe person can afford, and how much more money he/she willneed to afford an additional jean pant (cost of jean pant =need to afford an additional jean pant. (cost of jean pant =750)

• Write a program which converts 13 hours and 32 minutes intoseconds.

43K.K. Biswas, IIT Delhi

Page 28: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

DebuggingDebugging

• Removing �“bugs�” (errors) from the programg g ( ) p g• Principal = 34000• interest = principal * rate * time

• to r = 1/ 2b/ *• r = 1.0 / 2 * b

• temp 1 = 67• temp 1 = 67• height = 0• width = area / heightwidth area / height

44K.K. Biswas, IIT Delhi

Page 29: ogn g Pythonwith Python - ERNETpkalra/csl101/python1.pdf · • t s e used to ex plain the ppg ro gm ste ps. Basic idea is to help the r nd the . • ample: Suppose a t ts 43 s out

45K.K. Biswas, IIT Delhi