# Math in Python Lab

Infosec Learning
Virtual Lab

The structure of a mathematical equation in Python is very similar to equations you might write on paper or perform in a calculator. However, whereas the result might be shown on either side of an equals sign in a mathematical statement, in Python, the result must always be to the left of the assignment operator (i.e., equals sign). Evaluation of a...

Time
1 hour 30 minutes
Difficulty
Beginner
Join over 3 million cybersecurity professionals advancing their career
or

Overview

The structure of a mathematical equation in Python is very similar to equations you might write on paper or perform in a calculator. However, whereas the result might be shown on either side of an equals sign in a mathematical statement, in Python, the result must always be to the left of the assignment operator (i.e., equals sign). Evaluation of an equation follows the normal rules of precedence, flowing from left to right.

Since variable names may consist of a single letter, x cannot be used for multiplication. Instead, the asterisk, *, is used.

Division in Python version 2 is a little quirky because division of two integers results in a truncated integer result. That is, the result is a whole number that is truncated (rounded down) rather than rounded off. Sometimes this is the desired behavior; sometimes it’s not. We’ll explore the consequences and alternatives in the exercises that follow.

The notion of modulo is foreign to many students, even though they probably learned the concept very early in grade school. Even though it’s a concept you may never have used in your day-to-day life, it has many applications in programming. The modulo operation will be examined in the exercises below.