Datatypes & Operations

Variables can store different datatypes, like integers, characters, and lists. Operations are how we can modify those datatypes, for instance dividing integers and concatenating characters or lists.


Basic datatypes

Datatype Examples
Constants True, False, None
Integer 3, 9, 100000
Float 3.1415926, -2.34
Boolean 2<3, "c" in "ace"
Character "4", "d", "P", "*"
String "Bob the Builder!", "Jesse Chan"
List [1, 2, 3], ["hi", 4, True]
Tuple (1, 2, 3), ("hi", 4, True)

In addition, Python has other datatypes like sets and dictionaries. These, along with strings, lists, and tuples, will be explored in later chapters. If you want to know what datatype a variable "x" is, you can look it up with type(x)


Basic operators

Category Operators
Arithmetic +, -, *, /, //, **, %
Relational <, <=, >=, >, ==, !=
Logical and, or, not
Bitwise <<, >>, &, |, ^, ~, &=, |=, ^=

Note that we will not be using the last category (bitwise) of operators in this course. You might encounter them when learning other languages like C or Java, but they are used much less in Python.


Arithmetic in Python

Arithmetic in Python is relatively straightforward, and adheres to most of the rules we're familiar with--for example, we can't divide anything by 0, and Python crashes if you attempt to divide by 0. However, Python does introduce three operator representations you probably aren't familiar with.

1. Integer Division (//)

First off is integer division, represented by the "//" symbol. Integer Division rounds down, which means that when you get a floating point number (decimal) after division, it would round down to the nearest whole number.

For positive values only, you can think of integer divide as removing the decimals after division. So when you do division you have the result and the remainder. Integer Divide is the result. However for negative values, think about it as first doing a normal division to get a float, then round down to the nearest whole number

Also, Integer division can be used for floating points too!



2. Modulus (%)

For positive numbers, modulus returns the remainder of division. For both positive and negative numbers you can think of modulus as finding a multiple of the second argument that is immediately less than the first, then get the difference between the two



A quick mathematical fact--a%b is equal to (a-(a//b)*b)! You can verify this on different numbers.

3. Power (**)

Lastly, "to the power of" is represented with "**". For instance, 2**2 is equivalent to 2 to the power of 2, i.e. 4.



4. int() and float()

To convert a float to an int, use the int() function. To convert an int to a float, use the float() function. For the int() function, we convert the float to an int by simply removing the decimal points. This is known as rounding to 0, instead of rounding down like integer division or modulus.



Something else to take note of is operator precedence and associativity. Say you run 3+4*5/6. Which operation is run first? Addition? Division? It turns out that, as aforementioned, Python adheres to normal mathematical rules:

	



Floating point approximation

Python uses something called floating point approximation to approximate the values of fractions. The issue with this is that the approximations are not going to be 100% matching. Consider the fraction 1/3. It can be represented as 0.33333333 and onwards--an infinitely repeating decimal. Computer programs have limited memory, so we can't actually represent anything in infinite space.

Let's try an example:


Note: Our online code-running interface sometimes runs Python 2 instead of Python 3--normally, you only need one float() around the entire fraction to see the same results.


Short circuit evaluation

When we deal with logical operators, Python will evaluate them sequentially from left-to-right. So, for instance, if we have a statement like "True or False", Python will read "True" first, and since it's an or statement, it will immediately evauate to True without even looking at the "False" part.