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.
outcomes:
In this lab, you will learn to:
- Declare variables using string, int, and float.
- Convert variables from one data type to another.
- Recognize exceptions that result from mixed data types.
- Implement basic math equations in Python according to the rules of precedence.
- Introduce program control structures using conditions, comparison operators, and the if-elif-else construct.