9
Variables and Expressions, continued CMSC 201

Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything

Embed Size (px)

Citation preview

Page 1: Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything

Variables and Expressions, continued

CMSC 201

Page 2: Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything

ExpressionsAnything on the right hand side of the equals is an expression. Expressions can be anything that yields a value.

a = 4b = 10 * ac = (100 * 4) / 9 + 2d = sin(40) # This is a function call, which we’ll

# discuss later.

Page 3: Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything

OperatorsSo what can we do in python? Here are some basic operators:

Operation Python Operator

Addition +

Subtraction -

Multiplication *

Division /

Exponentiation **

Modulus %

Page 4: Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything

OperatorsExponentiation:

number ** power

So if we want 3 squared, we say:

3 ** 2

If we want 2 cubed, it would be:

2 ** 3

Page 5: Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything

ModulusModulus:

a % b

Is equivalent to “the remainder of a after a is divided by b”

14 % 6 = 212 % 2 = 010 % 3 = 1

Page 6: Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything

ModulusWhy 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 27th is. All you need to do is figure out 27 % 7, and that’s how many days past Monday you are.

Page 7: Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything

Integers vs. FloatsData 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!

Page 8: Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything

Integers vs. FloatsWhen we divide a float and anything else, the result is a float. However, there is often rounding error.

>> 7.0 / 3.02.3333333333333335

Be careful to never compare two floats after you have done division!

Page 9: Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything

Other Math FunctionsUsing “import math” we can access other math functions, such as:

Function Purpose

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

pi The value of pi

E The value of e