Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of an assignment is an expression. An expression is anything.

Similar presentations


Presentation on theme: "Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of an assignment is an expression. An expression is anything."— Presentation transcript:

1 Variables and Expressions, continued CMSC 201

2 Expressions Anything on the right hand side of an assignment is an expression. An expression is anything that yields a value. a = 4 b = 10 * a c = (100 * 4) // 9 + 2 # This is a function call, # which we’ll discuss later. d = sin(40)

3 Operators Some basic operators: OperationPython Operator Addition+ Subtraction- Multiplication* Division/ // Exponentiation** Modulus%

4 Operators Exponentiation: number ** power So if we want 3 squared, we say: 3 ** 2 If we want 2 cubed, it would be: 2 ** 3

5 Modulus Modulus: a % b gives “the remainder of a after a is divided by b” 14 % 6 == 2 12 % 2 == 0 10 % 3 == 1

6 Modulus Why is modulus useful? Tells us if one thing is divisible by another (if you mod a by b and get zero, b is divisible by a) Remainders are useful! Imagine you know the first of a month is a Monday, and you want to know what day the 27 th is. All you need to do is figure out 27 % 7, and that’s how many days past Monday you are.

7 Integers vs. Floats Data in python remembers what type it is. a = 4 a is an integer. b = 4.4 b is a float. Floats and integers act differently!

8 Integer vs. Float Division a = 7 b = 3 print(a/b) prints 2.33333333 a = 7 b = 3 print(a//b) prints 2

9 Integers vs. Floats When we divide a float and anything else, the result is a float. However, there is often rounding error. 7.0 / 3.0 2.3333333333333335 Be careful to never compare two floats after you have done division! What happens if you print this??? print(2.3333333333333333)

10 Other Math Functions Other math functions: from math import * FunctionPurpose cos(x), sin(x), tan(x)Trigonometric functions log(x, base)Logarithm of x with given base floor(x)Finds the closest integer less than or equal x ceil(x)Finds the closest integer greater than or equal to x sqrt(x)Finds the square root of x piThe value of pi eThe value of e


Download ppt "Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of an assignment is an expression. An expression is anything."

Similar presentations


Ads by Google