πŸ–₯️
Berkeley CS61A: Notes
  • 🌍Hello, World!
  • πŸ’­Learn to Think Like Your Computer
  • Building Blocks
    • ❗Expressions in Python
      • ℹ️Mathematical Statements
      • 🀫Variables
      • πŸ‘Data Types in Python
    • 🚧Introduction to Functions
      • πŸƒβ€β™€οΈHow Python Executes a Function Call
      • 🚱Functions that Don't Return Anything
    • 🌊Control Flow
      • ⁉️What is an if statement?
      • πŸ’ΎWhat is a Loop?
      • 🚨What are Logical Operators?
        • πŸ˜–Logical Operators, Seemingly Illogical Behavior
      • 🚰How Does Control Flow?
    • ⚑Higher-Order Functions
      • ⏸️What are Higher-Order Functions Used For?
      • πŸ‘†Self-Reference
    • πŸ‘ΎEnvironments and Scope
      • πŸ–ŠοΈHow to Make Environment Diagrams
  • STRUCTURES OF DATA
    • ➰Recursion
      • πŸ«€Anatomy of Recursion
      • β›½Recursive Leap of Faith
    • πŸŽ„Tree Recursion
      • ⬇️The Use It/Lose It Principle
    • πŸ“€Iterables
      • βœ‚οΈList Slicing
      • πŸ’€Deep Lists
      • πŸŒ‹Iterable Functions
      • πŸ’½List Comprehensions
    • πŸ™ˆAbstraction
    • Trees
    • Linked Lists
    • Iterators and Generators
    • Efficiency
  • Extra Topics
    • 🎭Errors and their Types
      • 🏹Error vs Exception
      • πŸ”™What is Backtracing?
    • 🚑How the Terminal and the File are Different
    • 🍬Decorators
  • Debugging Tools
    • πŸ–¨οΈDebugging with Print Statements
    • πŸ›Debugging with the Debugger
Powered by GitBook
On this page
  • Introduction to Operators
  • Division, Floor Division and Modulo
  • How Python Reads Mathematical Statements

Was this helpful?

  1. Building Blocks
  2. Expressions in Python

Mathematical Statements

Let's do some MATH!

Introduction to Operators

We will start simple, with some mathematical expressions all of you should be familiar with. Let's mess around with them first before we start analyzing what Python is doing behind the scenes.

>>> a, b = 4, 8
>>> b + a
12
>>> b - a
4
>>> b * a
32

Division, Floor Division and Modulo

I'll take a moment to talk about three operators that are closely related to each other β€” division, floor division, and multiplication.

Division in Python always returns a decimal β€”Β known in computer-speak as a "float".

>>> a, b = 4, 8
>>> b / a
2.0
>>> c, d = 6, 4
>>> c / d
1.5

To get an integer result from division, we can use the floor division operator (//), which divides by discarding the decimal section entirely (or rounding down, if that's easier for you to visualize).

>>> a, b = 4, 8
>>> b // a
2
>>> c, d = 6, 4
>>> c // d
1

A complimentary operator to the floor division operator is the modulo operator (%), which returns the remainder derived from the division.

>>> a, b = 4, 8
>>> b % a 
0
>>> c, d = 6, 4
>>> c % d
2 

These are operations some of you may never have seen before! I'd recommend playing around with them to get a feel for both they work. Here's some things to try out:

>>> a, b = 4, 8
>>> a % b
???
>>> c, d = -1, 6
>>> c % d
???

How Python Reads Mathematical Statements

You might be curious about the order in which Python executes statements like the ones we've talked about above. We did so intuitively, but it is important to realize that Python does not have an "intuitive" setting. Let's attempt to understand how Python evaluates these expressions.

>>> a, b = 4, 8
>>> a + b

First, the operator is evaluated β€”Β Python figures out what the + sign means. Then, it figures out what a is β€”Β here, it is 4. Then it figures out what b is β€” here, that is 8. Finally, it executes that expression with the evaluated values β€”Β 4 + 8 = 12.

>>> (3 + 4) % 5 + 7 // 8
PreviousExpressions in PythonNextVariables

Last updated 3 years ago

Was this helpful?

Find out what these values return! The negative modulo is most definitely out of scope, but if you're curious, about it.

One last note: Python follows , which I'll assume you're familiar with. As a closing challenge, try to figure out the process through which Python evaluates the following:

❗
ℹ️
here's an interesting article
PEMDAS