Python - Week 3 Mohammad Shokoohi-Yekta 1 Objective To solve - - PowerPoint PPT Presentation

python week 3
SMART_READER_LITE
LIVE PREVIEW

Python - Week 3 Mohammad Shokoohi-Yekta 1 Objective To solve - - PowerPoint PPT Presentation

Python - Week 3 Mohammad Shokoohi-Yekta 1 Objective To solve mathematic problems by using the functions in the math module To represent and process strings and characters To use the + operator to concatenate strings To


slide-1
SLIDE 1

1

Python - Week 3

Mohammad Shokoohi-Yekta

slide-2
SLIDE 2

2

Objective

  • To solve mathematic problems by using the functions in the math module
  • To represent and process strings and characters
  • To use the + operator to concatenate strings
  • To write Boolean expressions by using comparison operators
  • To implement selection control by using one-way if statements
  • To implement selection control by using two-way if .. else statements
  • To avoid common errors in if statements
  • To combine conditions by using logical operators (and, or, and not)
  • To use selection statements with combined conditions
slide-3
SLIDE 3

3

Built-in functions

>>> max(2, 3, 4) # Returns the maximum number 4 >>> min(2, 3, 4) # Returns the minimum number 2 >>> round(3.51) # Rounds to its nearest integer 4 >>> round(3.4) # Rounds to its nearest integer 3 >>> abs(-3) # Returns the absolute value 3 >>> pow(2, 3) # Same as 2 ** 3 8

slide-4
SLIDE 4

4

String concatenation

You can use the + operator to add two numbers. The +

  • perator can also be used to concatenate (combine) two
  • strings. Here are some examples:

>>> message = "Welcome " + "to " + "Python" >>> message 'Weclome to Python' >>> chapterNo = 2 >>> s = "Chapter " + str(chapterNo) >>> s 'Chapter 2' >>>

slide-5
SLIDE 5

5

Coding Example 1

Ask the user an amount of money in cents and find the least number of coins equivalent to the user’s money. e.g: number of quarters, dimes, nickels and pennies.

slide-6
SLIDE 6

6

Boolean Data Types

There are six comparison operators (also known as relational operators) that can be used to compare two values. The result of the comparison is a Boolean value: true or false.

b = (1 > 2)

slide-7
SLIDE 7

7

Comparison Operators

Operator Name < less than <= less than or equal to > greater than >= greater than or equal to == equal to != not equal to

slide-8
SLIDE 8

8

One-way if Statements

boolean-expression True Statement(s) False radius >= 0? True area = radius * radius * 3.14159 print("The area for the circle of ", "radius", radius, "is", area) False (a) (b)

if boolean-expression: statement(s) if radius >= 0: area = radius * radius * 3.14159 print("The area for the circle of radius“, radius, "is“, area)

slide-9
SLIDE 9

9

Note

if i > 0:

print("i is positive")

(a) Wrong (b) Correct

if i > 0: print("i is positive")

slide-10
SLIDE 10

10

Coding Example 2

Write a program that prompts the user to enter an

  • integer. If the number is a multiple of 5, print HiFive. If

the number is divisible by 2, print HiEven.

slide-11
SLIDE 11

11

Two-way if Statement

if boolean-expression: statement(s)-for-the-true-case else: statement(s)-for-the-false-case

boolean-expression False True Statement(s) for the false case Statement(s) for the true case

slide-12
SLIDE 12

12

if...else Example

if radius >= 0: area = radius * radius * math.pi print("The area for the circle of radius", radius, "is", area) else: print("Negative input")

slide-13
SLIDE 13

13

Multiple Alternative if Statements

if score >= 90.0:

grade = 'A' else: if score >= 80.0: grade = 'B' else: if score >= 70.0: grade = 'C' else: if score >= 60.0: grade = 'D' else: grade = 'F'

(a) Equivalent

if score >= 90.0: grade = 'A' elif score >= 80.0: grade = 'B' elif score >= 70.0: grade = 'C' elif score >= 60.0: grade = 'D' else: grade = 'F'

(b) This is better

slide-14
SLIDE 14

14

Flowchart

score >= 90 True grade = 'A' False score >= 80 True grade = 'B' False score >= 70 True grade = 'C' score >= 60 True grade = 'D' False False grade = 'F'

slide-15
SLIDE 15

15

Trace if-else statement

if score >= 90.0: grade = 'A' elif score >= 80.0: grade = 'B' elif score >= 70.0: grade = 'C' elif score >= 60.0: grade = 'D' else: grade = 'F'

Suppose score is 70.0 The condition is false

slide-16
SLIDE 16

16

Trace if-else statement

if score >= 90.0: grade = 'A' elif score >= 80.0: grade = 'B' elif score >= 70.0: grade = 'C' elif score >= 60.0: grade = 'D' else: grade = 'F'

Suppose score is 70.0 The condition is false

slide-17
SLIDE 17

17

Trace if-else statement

if score >= 90.0: grade = 'A' elif score >= 80.0: grade = 'B' elif score >= 70.0: grade = 'C' elif score >= 60.0: grade = 'D' else: grade = 'F'

Suppose score is 70.0 The condition is true

slide-18
SLIDE 18

18

Trace if-else statement

if score >= 90.0: grade = 'A' elif score >= 80.0: grade = 'B' elif score >= 70.0: grade = 'C' elif score >= 60.0: grade = 'D' else: grade = 'F'

Suppose score is 70.0 grade is C

slide-19
SLIDE 19

19

Trace if-else statement

if score >= 90.0: grade = 'A' elif score >= 80.0: grade = 'B' elif score >= 70.0: grade = 'C' elif score >= 60.0: grade = 'D' else: grade = 'F'

Suppose score is 70.0 Exit the if statement

slide-20
SLIDE 20

Common Errors

Most common errors in selection statements are caused by incorrect indentation. Consider the following code in (a) and (b).

20

radius = -20

if radius >= 0: area = radius * radius * 3.14 print("The area is", area)

(a) Wrong

radius = -20 if radius >= 0: area = radius * radius * 3.14 print("The area is", area)

(b) Correct

slide-21
SLIDE 21

Tip

21

if number % 2 == 0:

even = True else: even = False

(a) Equivalent

even = number % 2 == 0

(b) This is shorter

slide-22
SLIDE 22

Computing Taxes

The US federal personal income tax is calculated based on the filing status and taxable income. There are four filing statuses: single filers, married filing jointly, married filing separately, and head of household. The tax rates for 2009 are shown in the next slide.

22

slide-23
SLIDE 23

Computing Taxes …

23

Marginal Tax Rate Single Married Filing Jointly

  • r Qualified Widow(er)

Married Filing Separately Head of Household 10% $0 – $8,350 $0 – $16,700 $0 – $8,350 $0 – $11,950 15% $8,351– $33,950 $16,701 – $67,900 $8,351 – $33,950 $11,951 – $45,500 25% $33,951 – $82,250 $67,901 – $137,050 $33,951 – $68,525 $45,501 – $117,450 28% $82,251 – $171,550 $137,051 – $208,850 $68,525 – $104,425 $117,451 – $190,200 33% $171,551 – $372,950 $208,851 – $372,950 $104,426 – $186,475 $190,201 - $372,950 35% $372,951+ $372,951+ $186,476+ $372,951+

slide-24
SLIDE 24

Computing Taxes …

24

if status == 0: # Compute tax for single filers elif status == 1: # Compute tax for married filing jointly elif status == 2: # Compute tax for married filing separately elif status == 3: # Compute tax for head of household else: # Display wrong status

slide-25
SLIDE 25

Coding Example 3

Body Mass Index: Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters. The interpretation of BMI for people 16 years or older is as follows:

25

BMI Interpretation Below 18.5 Underweight 18.5-24.9 Normal 25.0-29.9 Overweight Above 30.0 Obese

slide-26
SLIDE 26

Coding Example 3 …

Write a code which inputs a user’s height and weight, then outputs the interpretation of user’s BMI

26

slide-27
SLIDE 27

Logical Operators

27

Operator Description

not logical negation and logical conjunction

  • r logical disjunction
slide-28
SLIDE 28

Truth Table for Operator not

28

p not p

True False False True Example (assume age = 24, gender = 'F') not (age > 18) is False, because (age > 18) is True. not (gender == 'M') is True, because (grade == 'M') is False.

slide-29
SLIDE 29

Truth Table for Operator and

29

p1 p2 p1 and p2

False False False False True False True False False True True True Example (assume age = 24, gender = 'F') (age > 18) and (gender == 'F') is True, because (age > 18) and (gender == 'F') are both True. (age > 18) and (gender != 'F') is False, because (gender != 'F') is False.

slide-30
SLIDE 30

Truth Table for Operator or

30

p1 p2 p1 or p2

False False False False True True True False True True True True Example (assume age = 24, gender = 'F') (age > 34) or (gender == 'F') is true, because (gender == 'F') is True. (age > 34) or (gender == 'M') is False, because (age > 34) and (gender == 'M') are both Talse.

slide-31
SLIDE 31

Coding Example 4

Write a program that checks whether a number is divisible by 2 and 3, whether a number is divisible by 2 or 3, and whether a number is divisible by 2 or 3 but not both.

31

slide-32
SLIDE 32

32

Data Mining: Prediction Project

slide-33
SLIDE 33

What is Time Series Data?

r p y x y z

33

slide-34
SLIDE 34

A baby in a PICU at Children's Hospital Los Angles She produces 12 time series, possibly for weeks..

34

slide-35
SLIDE 35

Why Predict the (short-term) Future?

If a robot can predict that it is about to fall, it may be able to..

  • Prevent the fall
  • Mitigate the damage of the fall

More importantly, if the robot can predict a human’s actions

  • A robot could catch a falling human!
  • This would allow more natural human/robot interaction.
  • Real time is not fast enough for interaction! It needs to be

real time, minus a second.

35

slide-36
SLIDE 36

10,000 20,000 30,000

  • Leafhoppers suck sap from the leaves

cause mottled discoloration

  • In Iowa, the potato leafhopper causes

$15 M damage

  • Glue a wire to insect to activate the

circuit

36

slide-37
SLIDE 37

37

slide-38
SLIDE 38

38

slide-39
SLIDE 39

Lets consider some Zebra Finch songs (MFCC space)

500 1000 1500 2000 2500 3000

50 100 20 40 60

Rule learned on day 40 (post hatch). Lets wait 60 days to see if the rule still applies…

39

slide-40
SLIDE 40

Lets consider some Zebra Finch songs (MFCC space)

50 100 20 40 60

3500 4000 4500 5000 5500 6000

The rule firing on the singing of a 100 day old zebra finch

40

slide-41
SLIDE 41

maxlag = 20 minutes 120 180 15500 16000 16500 17000

IF we see a Clothes Washer used THEN we will see Clothes Dryer used within 20 minutes

41

slide-42
SLIDE 42

42

Lab Works

slide-43
SLIDE 43

Lab Work 1

Write a program to find out the Chinese Zodiac sign for a given

  • year. The Chinese Zodiac sign is based on a 12-year cycle,

each year being represented by an animal: rat, ox, tiger, rabbit, dragon, snake, horse, sheep, monkey, rooster, dog, and pig, in this cycle.

43

rat

  • x

tiger rabbit dragon snake horse sheep monkey rooster dog pig

0: monkey 1: rooster 2: dog 3: pig 4: rat 5: ox 6: tiger 7: rabbit 8: dragon 9: snake 10: horse 11: sheep year % 12 =

slide-44
SLIDE 44

Lab Work 2

Write a program which first prompts the user to enter a year as an int value and checks if it is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400.

44

slide-45
SLIDE 45

45

C U next week J