Python - Week 2

43
1 Python - Week 2 Mohammad Shokoohi-Yekta

Transcript of Python - Week 2

1

Python - Week 2

Mohammad Shokoohi-Yekta

2

Objective• To write programs that perform simple computations• To obtain input from a program’s user by using the input

function• To use identifiers to name variables• To assign data to variables• To define named constants• To use the operators +, -, *, /, //, %, and **• To write and evaluate numeric expressions• To use augmented assignment operators to simplify coding• To perform numeric type conversion and rounding with the

int and round functions

Introduction to PyCharm

3

4

Print function• Print (“Hello World”)

• Print (4+3)

• Print (variable)

• Print (“4+3”)

5

Identifiers-Variables• An identifier is a sequence of characters that consists

of letters, digits, underscores (_), and asterisk (*).• An identifier must start with a letter or an underscore. It

cannot start with a digit.• An identifier cannot be a reserved word. Reserved

words have special meanings in Python, which we will later.

• An identifier can be of any length.

6

Constants

The value of a variable may change during the execution of a program, but a named constant or simply constant represents permanent data that never changes. Python does not have a special syntax for naming constants. You can simply create a variable to denote a constant. To distinguish a constant from a variable, use all uppercase letters to name a constant.

7

Assignment Statementsx = 1 # Assign 1 to x

x = x + 1

i = j = k = 1

8

Simultaneous Assignment

var1, var2, ..., varn = exp1, exp2, ..., expn

x, y = y, x # Swap x with y

9

Expression

x = 1 # Assign 1 to variable xradius = 1.0 # Assign 1.0 to variable radius

# Assign the value of the expression to xx = 5 * (3 / 2) + 3 * 2

x = y + 1 # Assign the addition of y and 1 to xarea = radius * radius * 3.14159 # Compute area

10

Coding Example 1

Given the radius of a circle, write a code to calculate the area.

11

Data Types

• integer: e.g., 3, 4• float: e.g., 3.0, 4.0

• String: “Hello”, ‘your name’

12

Numeric Operators Name Meaning Example Result

+ Addition 34 + 1 35 - Subtraction 34.0 – 0.1 33.9 * Multiplication 300 * 30 9000 / Float Division 1 / 2 0.5 // Integer Division 1 // 2 0 ** Exponentiation 4 ** 0.5 2.0 % Remainder 20 % 3 2

13

Input from the user

Use the input function • variable = input("Enter a string: ")

14

Coding Example 2

Input the radius of a circle from the user, then calculate the area.

15

The % operator

12 4

3

12 0

7 3

2

6 1

26 8

3

24 2 Remainder

Quotient

20 13

1

13 7

Dividend Divisor

Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd.

16

Coding Example 3

Suppose today is Saturday and you and your friends are going to meet in 10 days. What day is in 10 days?

17

Overflow

When a variable is assigned a value that is too large (in size) to be stored, it causes overflow. For example, executing the following statement causes overflow.

>>>245.0 ** 1000

OverflowError: 'Result too large'

18

Underflow

When a floating-point number is too small (i.e., too close to zero) to be stored, it causes underflow. Python approximates it to zero. So normally you should not be concerned with underflow.

19

Arithmetic Expressions

is translated to

(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

)94(9))(5(10543

yx

xxcbayx +

++++-

-+

20

How to Evaluate an Expression

3 + 4 * 4 + 5 * (4 + 3) - 1 3 + 4 * 4 + 5 * 7 – 1 3 + 16 + 5 * 7 – 1 3 + 16 + 35 – 1 19 + 35 – 1 54 - 1 53

(1) inside parentheses first

(2) multiplication

(3) multiplication

(4) addition

(6) subtraction

(5) addition

21

Augmented Assignment Operators

Operator Example Equivalent+= i += 8 i = i + 8

-= f -= 8.0 f = f - 8.0

*= i *= 8 i = i * 8

/= i /= 8 i = i / 8

%= i %= 8 i = i % 8

22

Type Conversion and Rounding

datatype(value)

i.e., int(4.5) => 4float(4) => 4.0str(4) => “4”

round(4.6) => 5round(4.5) => 4

How to define number of digits after decimal points?

23

Software Development Process

Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

Requirement Specification

24

Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

A formal process that seeks to understand the problem and document in detail what the software system needs to do. This phase involves close interaction between users and designers.

System Analysis

25

Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

Seeks to analyze the business process in terms of data flow, and to identify the system’s input and output.

System Design

26

Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

The process of designing the system’s components.

This phase involves the use of many levels of abstraction to decompose the problem into manageable components, identify classes and interfaces, and establish relationships among the classes and interfaces.

IPO

27

Requirement Specification

System Analysis

System Design

Input, Process , Output

Testing

Implementation

Maintenance

Deployment The essence of system analysis and design is input, process, and output. This is called IPO.

Implementation

28

Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

The process of translating the system design into programs. Separate programs are written for each component and put to work together.

Testing

29

Ensures that the code meets the requirements specification and weeds out bugs.

Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing.

Deployment

30

Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

Deployment makes the project available for use.

Maintenance

31

Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

A software product must continue to perform and improve in a changing environment. This requires periodic upgrades of the product to fix newly discovered bugs and incorporate changes.

Maintenance is concerned with changing and improving the product.

32

Data Mining: Mice Project

33

Mice Models

• Genetic Models of human disease

• Mice type:- WT (Wild Type): which is regular lab mice- KO (Knocked Out): genetically engineered mouse in which researchers

have inactivated or “knocked out” an existing gene

34

Motivation

• Repetitively stacking or lining up objects is a behavior sometimes associated with individuals with autism

• Genes in human and mice are pretty the same, therefore scientists knock out genes in mice to spot autism.

• Analysis of mice vocalizations is a great way to understand the effect of knocked out genes.

35

Ultrasonic Vocalizations

Behavior Descriptiondefensive Female bites, hits, or kicks the malegrooming licking of conspecific's fur

intromission male engages in period of deep slow thrustingmounting male engages in period of fast shallow thrustingno contact mice are not in physical contactrooting male pushes snout under abdomen or hindquarters of femalesniffing male sniffs ano-genital region of female

Mice Behavior

38

UCR-USV Algorithm

Ground Truth

GHT

class1 class5 class7 class7 class7 class3 class7 class3 class7 class4 class7 class5

No Contact

Spectrogram

39

Lab Works

Lab Work 1

Write a program to ask the user’s first and last name, then print their full name.

40

41

Lab Work 2

Computing Loan Payments:Write code to let the user enter the interest rate, number of years, and loan amount, and compute monthly payment.

12)1(11 ´+

-

´=

arsnumberOfYeerestRatemonthlyInt

erestRatemonthlyIntloanAmountmentmonthlyPay

Lab Work 3

Ask the user to enter two data points, then compute their distance.

42

43

C U next week J