Lecture 1

Numbers

Two Types of Numbers

Python natively supports two types of numbers:

  1. ints
  2. floats

Python does support other kinds of numbers (e.g. imaginary, complex), but it requires a module.

Python will automatically convert between the two types of numbers. However, if you want to force a particular type, you can:

  1. Convert a value into an int: int(value)
  2. Convert a value into a float: float(value)

Makes sense, right? We'll talk more about this in a future lecture.

Math Operators

Python supports the basic math operations:

Operator Description Example
+ Addition: Adds values on either side of the operator. 5 + 3
(equals 8)
- Subtraction: Subtracts right hand operand from left hand operand. 3 - 5
(equals -2)
* Multiplication: Multiplies values on either side of the operator 5 * 3
(equals 15)
/ Division: Divides left hand operand by right hand operand 5 / 3
(equals 1.6666666666666667)
// Floor Division: The division of operands where the result is the quotient in which the digits after the decimal point are removed. 5 // 3
(equals 1)
% Modulus: Divides left hand operand by right hand operand and returns remainder 5 % 3
(equals 2)
** Exponent: Performs exponential (power) calculation on operators. Do not use ^, that is the bitwise XOR operation. 5 ** 3
(equals 125)

Python supports a lot of other math operations in the math module.

The order of operations in Python is the same as in math:

  1. Parentheses: Evaluate what's in the parentheses before what's outside.
  2. Exponentiation: Evaulation exponentiation next.
  3. Multiplication and Division: The multiplication and all three divisions (/, %, //) are evaluated next.
  4. Addition and Subtraction: These are evaluated last.

For each item above, if more than one is in the expression, work from left to right evaluating them.

Variables

Math is boring without variables. Python supports variables (as almost every programming language does). To create a variable, you just decide on a name and give it a value.

Variable Naming

Variable names must follow these rules:

Some suggestions when deciding on variable names:

Assigning Values to Variables

We use the assignment operator (=) to assign values to variables. The variable goes on the left and the value goes on the right.

>>> number_nine = 9

The value can be (almost) anything:

>>> another_nine = number_nine
>>> two_nines = 9*number_nine

Variable Type

If you've programmed in other languages, you might be wondering about how you specify a variable's type. In Python, you don't! Python will figure out the type for you.

In fact, it's possible for a variable's type to change. At one point, the variable could hold a float, then later an int, and later text.

If you want to know a variable's type you can use the type function:

>>> number_nine = 9
>>> type(number_nine)
<class 'int'>

The <class 'int'> is telling you that number_nine is an int.

Comments

Comments are a great way to leave notes for other programmers (or yourself). Python will ignore comments, so you can type anything you want into a comment. They're great for explaining how a piece of code works or how to use it.

In Python, a comment starts with # and ends at the end of the line.

If you want multi-line comments (also called block comments), it's possible to get that with multi-line strings.

Next Notes >>