Python - Week 3. Mohammad Shokoohi-Yekta

Similar documents
Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Selections. CSE 114, Computer Science 1 Stony Brook University

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

CS1150 Principles of Computer Science Boolean, Selection Statements

Selection Statements. Pseudocode

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

Chapter 3 Selection Statements

Lecture 4: Conditionals

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Text Input and Conditionals

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

CONDITIONAL EXECUTION

Chapter 3 Problem Solving and the Computer

Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Lecture 1 Java SE Programming

Chapter 3. Selections. Program Listings

Lab 6B Coin Collection

Introduction to Computer Science using JAVA

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Sand Springs Public Schools 3rd Grade Math Common Core State Standards

Chapter 2. Elementary Programming

Making Decisions In Python

CS 2316 Individual Homework 1 Python Practice Due: Wednesday, August 28th, before 11:55 PM Out of 100 points

CS 115 Exam 3, Fall 2011

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

2016 Fall Startup Event Thursday, September 29th, 2016

Mental Math. Grade 9 Mathematics (10F) General Questions. 2. What is the volume of a pool that measures 7 m by 3 m by 2 m? 42 m 3

Module 1: Types and Expressions

Virginia Grade Level Alternative Worksheet

Advanced Algebra I Simplifying Expressions

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Chapter 3 Mathematical Functions, Strings, and Objects

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Chapter 4: Making Decisions

Key Learning for Grade 3

Chapter 4: Making Decisions

3chapter C ONTROL S TATEMENTS. Objectives

cs1114 REVIEW of details test closed laptop period

A453 Task 1: Analysis: Problem: Solution:

LECTURE 04 MAKING DECISIONS

UEE1302(1102) F10: Introduction to Computers and Programming

2009 Fall Startup Event Thursday, September 24th, 2009

4 Decision making in programs

Lecture 2: Operations and Data Types

11th Annual NB (English) High School Programming Competition

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Building Java Programs

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Programming Paradigms: Overview to State Oriented

Diocese of Boise Math Curriculum 5 th grade

Prerequisites: Read all chapters through Chapter 4 in the textbook before attempting this lab. Read through this entire assignment before you begin.

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Course Outlines. Elementary Mathematics (Grades K-5) Kids and Numbers (Recommended for K-1 students)

Flow of Control. bool Data Type. Flow Of Control. Expressions. C++ Control Structures < <= > >= ==!= ! &&

Python Activity 5: Boolean Expressions and Selection Statements

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

ROCHESTER COMMUNITY SCHOOL MATHEMATICS SCOPE AND SEQUENCE, K-5 STRAND: NUMERATION

CSc 110, Autumn Lecture 11: if / else. Adapted from slides by Marty Stepp and Stuart Reges

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Chapter 5. Conditions, Logical Expressions, and Selection Control Structures

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Chapter 5 Topics. Chapter 5 Topics. Flow of Control. bool Data Type. Flow of Control. Chapter 5

Control Structures 1 / 17

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

SBT 645 Introduction to Scientific Computing in Sports Science #3

2.N.2.1 Use the relationship between addition and subtraction to generate basic facts up to 20.

WEST VIRGINIA ADULT BASIC EDUCATION SKILLS CHECKLIST ABE MATHEMATICS Federal Functioning Level 1 Beginning Literacy (0 1.9)

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Conditional Statements

A GREATER GOODS BRAND

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Simplifying Expressions

AP Computer Science. if/else, return values. Copyright 2010 by Pearson Education

Advanced features of the Calculate signal tool

DLM Mathematics Year-End Assessment Model Blueprint

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Chapter 6. Decision and Control Statements

Boolean Expressions (Conditions)

Analogies, Name Game and $10,000 Pyramid

Com S 127x - Lab 6 1. READING FLOWCHARTS WITH CONDITIONAL ACTIONS!

Chapter 3. Selections

Java I/O and Control Structures

Mississippi College and Career Readiness Standards for Mathematics Scaffolding Document. Grade 2

Decision Making. <statements before if statement > if <condition >: <then statements > <statements after if statement >

Represent and solve problems involving addition and subtraction

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Use the scantron sheet to enter the answer to questions (pages 1-6)

learning objectives learn about variables, their types and their values learn about different number representations

Flow Control. So Far: Writing simple statements that get executed one after another.

Lecture 3. Input, Output and Data Types

Transcription:

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 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 2

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 3

String concatenation You can use the + operator to add two numbers. The + operator 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' >>> 4

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. 5

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) 6

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

One-way if Statements if boolean-expression: statement(s) if radius >= 0: area = radius * radius * 3.14159 print("the area for the circle of radius, radius, "is, area) boolean-expression False radius >= 0? False True True Statement(s) area = radius * radius * 3.14159 print("the area for the circle of ", "radius", radius, "is", area) (a) (b) 8

Note if i > 0: print("i is positive") if i > 0: print("i is positive") (a) Wr ong (b) Correct 9

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. 10

Two-way if Statement if boolean-expression: statement(s)-for-the-true-case else: statement(s)-for-the-false-case True boolean-expression False Statement(s) for the true case Statement(s) for the false case 11

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") 12

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 This is better 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) 13

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

Trace if-else statement Suppose score is 70.0 The condition is false 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' 15

Trace if-else statement Suppose score is 70.0 The condition is false 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' 16

Trace if-else statement Suppose score is 70.0 The condition is true 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' 17

Trace if-else statement Suppose score is 70.0 grade is C 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' 18

Trace if-else statement Suppose score is 70.0 Exit the if 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' 19

Common Errors Most common errors in selection statements are caused by incorrect indentation. Consider the following code in (a) and (b). 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 20

Tip if number % 2 == 0: even = True else: even = False (a ) Equivalent This is shorter even = number % 2 == 0 (b) 21

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

Computing Taxes Marginal Tax Rate Single Married Filing Jointly or 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+ 23

Computing Taxes 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 24

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: BMI Interpretation Below 18.5 Underweight 18.5-24.9 Normal 25.0-29.9 Overweight Above 30.0 Obese 25

Coding Example 3 Write a code which inputs a user s height and weight, then outputs the interpretation of user s BMI 26

Logical Operators Operator not and or Description logical negation logical conjunction logical disjunction 27

Truth Table for Operator not p True False not p 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. 28

Truth Table for Operator and 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. 29

Truth Table for Operator or 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. 30

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

Data Mining: Prediction Project 32

What is Time Series Data? x y z r p y 33

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

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

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 0 10,000 20,000 30,000 36

37

38

Lets consider some Zebra Finch songs (MFCC space) 500 1000 1500 2000 2500 3000 0 50 100 0 20 40 60 Rule learned on day 40 (post hatch). Lets wait 60 days to see if the rule still applies 39

Lets consider some Zebra Finch songs (MFCC space) The rule firing on the singing of a 100 day old zebra finch 0 50 100 0 20 40 60 3500 4000 4500 5000 5500 6000 40

maxlag = 20 minutes 15500 16000 16500 17000 0 120 0 180 IF we see a Clothes Washer used THEN we will see Clothes Dryer used within 20 minutes 41

Lab Works 42

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. dog rooster monkey sheep pig horse rat ox tiger rabbit dragon snake year % 12 = 0: monkey 1: rooster 2: dog 3: pig 4: rat 5: ox 6: tiger 7: rabbit 8: dragon 9: snake 10: horse 11: sheep 43

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

C U next week J 45