# Python variables and types¶

Python uses many data types. Here we cover the most basic ones with particular emphasis on numerical data types.

## Numerical data types¶

We have already played with two kinds int and float. int stands for integer and float stands for floating point number (an approximation to a real number). You can use the type() function to see the type of something. Here are some int’s:

type(1)

int

type(2)

int


And here are some float’s:

type(1.24235)

float

type(1e-5)

float

type(1.0)

float

type(2.)

float


When you add or multiply two int’s, you get an int:

type(1 + 2)

int

type(2 * 4)

int


Same thing for two float’s:

type(1.0 + 2.3)

float

type(2.1 * 3.5)

float


But what happens when you mix int’s and float’s? Let’s try it out:

type(1 + 2.4)

float


Adding an int to float gave you a float. Let’s try another example:

type(2 * 3.4)

float


Multiplying an int with a float gave you a float. This is a general rule: “When you have an operation that involves both int’s and float’s, the int’s are promoted to float’s and then the operation is carried out. Thus, the result is always a float.

What do we mean by promoted. We mean that when python sees: 2 + 2.4 it changes it first to 2.0 + 2.4 and then carries out the addition. Python can do this by using the float() function. Here is an example:

float(1)

1.0

type(float(1))

float


Similarly, there is an int() function that can change a float to an int:

int(23.4353)

23

type(int(23.33453))

int


Let’s see what type is math.inf and math.nan:

import math
type(math.inf)

float

type(math.nan)

float


## Questions¶

First guess and then try out, the type of the following expressions:

• 1 / 2.0

• 2 // 3

• 2.0 // 3

• 5 % 3

• 5.0 % 3

• int(5.0 % 3)

# Your code here


## Python variables¶

You can use variables to store the values of things. Here is a variable:

x = 1


Here is another variable:

y = 2


Now you can do:

z = x + y


and you can look at z:

z

3


## Questions¶

• In the code block provided below evaluate the angle between the two vectors: $$$\vec{r}_1 = 4\hat{i} + 3.5\hat{j} + 2.5\hat{k},$$$$and$$$$\vec{r}_2 = 1.5\hat{i} + 2.5\hat{j}.$$$$Remembet that the angle between two vectors is:$$$$\theta = \frac{\vec{r}_1\cdot \vec{r}_2}{|\vec{r}_1||\vec{r}_2|}.$$$ Hint: Define variables x1,x2,y1,y2,z1,z2 and use math.acos().

# Your code here


## Boolean types¶

Boolean types are either True or False. As a matter of fact True and False are special Python keywords. Here it is:

True

True

False

False

type(True)

bool

type(False)

bool


What can you do with True and False? We will learn more about them when we talk about conditionals (in another hands-on activity), but here are some examples:

True and True

True


So and is another Python keyword…

True and False

False

True or False

True

False or False

False


So or is another Python keyword…

Here is the final example:

not True

False

not False

True


So not is another Python keyword…

You can also have longer expressions with bool’s:

True and (False or True)

True


And just like for numerical types, there is a bool() function that can turn something into a bool type. For example:

bool(1)

True

bool(0)

False

bool(1.0)

True

bool(0.2242)

True


## The NoneType¶

Python has a special type called the NoneType. It is a special construct that is used to represent the “nothing.” Here it is:

None


Notice that when you trye to see None you do not see anything. This is because it is representing “nothing.” You cannot see the “nothing.” What is the type of None?

type(None)

NoneType