operations (slides)

Basic Types

  • string: "hello" or 'hello'
  • integer: 3
  • float: 3.14
  • bool: True or False

Use the function type() with each literal type.

Operations

  • + : plus

  • - : minus

  • * : multiple

  • / : divide

  • ** : power

  • // : divide and floor

  • % : modulos


Operations - examples

Think of the whiteboard as your program’s console. Write down what would display as the output for the following code:

print(10 + 5 - 5) 
print(10 * 5 / 4)
print(25 ** 2)  
print(25 ** 0.5) 
print(13 // 4) 
print(13 % 4) 

Operations - answers

print(10 + 5 - 5) # addition and subtraction
print(10 * 5 / 4) # multiplication and division
print(25 ** 2) # square 
print(25 ** 0.5) # square root 
print(13 // 4) # division and round down to the nearest integer
print(13 % 4) # remainder 
10
12.5
625
5.0
3
1

Order of Operations

PEMDAS

  • What does PEMDAS stand for?
  • The operator precedence:
    • Parentheses
    • Exponentiation
    • Multiplication and Division (including // and %)
    • Addition and Subtraction

PEMDAS

What value will each of these variables take on? No computers!

a1 = 5 /  5 * 10  * 5
a2 = 5 / (5 * 10) * 5

b1 = 5 *  10 - 2
b2 = 5 * (10 - 2)

c = (3 // (4 // 5)) + 1

PEMDAS – answer

a1 = 5 /  5 * 10  * 5
a2 = 5 / (5 * 10) * 5

b1 = 5 *  10 - 2
b2 = 5 * (10 - 2)

# c = (3 // (4 // 5)) + 1 ERROR -- Zero Division

print(a1)
print(a2)
print(b1)
print(b2)
50.0
0.5
48
40

Note that the division operator returns a float even when both numerator and denominator are integers

Rounding numbers with built-in function round()

round()

Use the round() function to get a floating-point number rounded to the specified number of decimals.

Syntax:

round(number, ndigits*)

The number of digits (ndigits) is optional, but we will often round number to two decimals:

round(392.68750000000006, 2)
392.69

round()

  • Run the following code on your laptop

  • Discuss with your partner how the output is calculated

print(round(51.6))
print(round(51.4))
print(round(51.5))
print(round(50.5))

round()

Use the round() function to get a floating-point number rounded to the specified number of decimals.

Syntax:

round(number, ndigits*)

Rounding is done toward the nearest even choice:

print(round(51.6))
print(round(51.4))
print(round(51.5))
print(round(50.5))
52
51
52
50

Operations and Variable Assignments

Variable assignments are not like math — they can appear in equations that make no mathematical sense.


x = 5
print(x)
x = x + 2
print(x)


x = x + 2 means “take the current value of x, add it with 2, and assign the resulting value (7) to x as its new value.”

Combining variables and operations

Write code to calculate the area of a circle when radius is 3:

  1. Assign integer 3 to radius
  2. Calculate the area (use \(\pi\) as 3.1415) and round it to two decimals
  3. Print the area

Comments

Comments are text that is meant to be read, but not executed. The purpose is to improve readability.

# assign a radius value
radius = 3
# compute the area of a circle and round to two decimals
area = round(3.1415 * radius ** 2, 2)
# print the area
print(area)
28.27

Questions

  • What if we want to get the area of circles when the radius is 1, 2, 3, 4 and 5?
  • Can we do it more efficiently?

Quiz 02

current time

You have 10 minutes to complete the quiz.