Values in Python have different *types*. You can find the type of a value using type()

In [23]:

```
type(42)
```

Out[23]:

In Python, the integer type is called `int`

In [2]:

```
type(3.14)
```

Out[2]:

In [24]:

```
type("Hello")
```

Out[24]:

In Python, the string type is called `str`

Note that an expression like `40 + 2`

is evaluated to a value with a type

In [12]:

```
type(40 + 2)
```

Out[12]:

Something like this would also work:

In [13]:

```
n = 5
type(n + 2)
```

Out[13]:

In [25]:

```
type(n)
```

Out[25]:

In [7]:

```
type(5/2)
```

Out[7]:

In [8]:

```
type(4/2)
```

Out[8]:

`type(4/2)`

still `float`

, even though 4 is divisible by 2? For consistency. Division always produces a `float`

.

`"ha" * 2`

is `"haha"`

. Multiplying an integer by a float produces a float (think about this like this: if we multiply a fraction by an integer, we can't be sure we'll get an integer. For consistency, we'll *always* produce a float)

In [21]:

```
type(2 * 3.14)
```

Out[21]:

We can go a little crazy:

In [10]:

```
type(type(42))
```

Out[10]:

Looking a little ahead:

In [11]:

```
type(print)
```

Out[11]:

`int`

): a whole number (positive or negative). In Python 3 (which we are using), ints can be as large as you like, as long as there is enough space in memory to store them.

Floating-point number (`float`

): a fractional number (like 3.14, 5.0, 4.0, or 10000.5). In the computer, floats are stored in scientific notation, with up to about 16 (decimal) digits in the mantissa and about 3 digits in the exponent (the exponent can go from about -300 to 300).

(Reminder about scientific notation: in scientific notation, numbers are stored in the format $m\times b^n$. For example, Avogadro's number is $6.02\times 10^23 mol^{-1}$. $m$ is called the *mantissa*, and $n$ is called the exponent, and $b$ is called the base.)

This means that we can't have numbers that are too large, or too close to 0, and can't store number with more than 16 digits of precision.

In [19]:

```
1.0 * 10**300
```

Out[19]:

In [26]:

```
1.0 * 10**350
```

`"Hi"`

, `"H2O"`

, and `'Hi' + "there"`

are all of type `str`

. You can enclose text in single or double quotes, as you as you are consistent. The string must be written on a single line:

In [27]:

```
'abc'
```

Out[27]:

In [28]:

```
"abc"
```

Out[28]:

In [29]:

```
"ab
c"
```

You *can* store strings that contain multiple lines:

In [77]:

```
'''a
bcd
ef'''
```

Out[77]:

`\n`

is a special character. It means "switch to a new line."

In [31]:

```
print("ab\ncd")
```

Suppose we want quotes inside quotes:

In [32]:

```
"Getting into medical school is "easy""
```

`"Getting into medical school is "`

makes sense, but the `easy`

that follows makes no sense to Python.

There are two solutions: one is to mix single quotes and double quotes:

In [34]:

```
'Getting into medical school is "easy"'
```

Out[34]:

In [35]:

```
"Getting into medical school is `easy'"
```

Out[35]:

`\"`

inside double quotes to let Python know that you don't mean to terminate the string

In [36]:

```
"Getting into medical school is \"easy\""
```

Out[36]:

(Note that Python itself uses the mixed quotes notation)

(But how do you put forward slashes in quotes? Use `"\\"`

for a single forward slash.)

We already saw an example of converting from one type to another when we used `n = float("Give me a number")`

. You can convert a string to a float:

In [41]:

```
float("3.14")
```

Out[41]:

`print()`

, both string and floats are printed without quotes)

In [43]:

```
print(float("3.14"))
print("3.14")
```

In [46]:

```
print("42" * 2)
print(float("42") * 2)
```

If we try to convert something that's not a number to a float, we'll get an error

In [48]:

```
float("forty-two")
```

Converting floats to string makes sense if, for example, we want to use "string addition":

In [50]:

```
"abc" + 123
```

But:

In [51]:

```
"abc" + str(123)
```

Out[51]:

Floats and strings can be converted to integers:

In [52]:

```
int(3.14)
```

Out[52]:

In [53]:

```
int(3.75)
```

Out[53]:

(Integers are *truncated* rather than rounded)

In [54]:

```
int("42")
```

Out[54]:

In [55]:

```
int("42.3")
```

Doesn't work: the string must contain an integer to be converted. But we have the tools to do this:

In [57]:

```
int(float("42.3"))
```

Out[57]:

You can convert strings to lists:

In [60]:

```
list("abc")
```

Out[60]:

`list("abc")[1]`

). But in fact, there is an easier way: you can just treat the string the way we treated lists:

In [61]:

```
s = "abc"
s[1]
```

Out[61]:

In fact, you can directly go

In [62]:

```
"abc"[2]
```

Out[62]:

You can also find the length of a string (i.e., the number of characters the string has) like so:

In [64]:

```
len("hello")
```

Out[64]:

Another type that we already so was Booleans (`bool`

type in Python). Those values can be either `True`

or `False`

. We already saw this kind of thing:

In [65]:

```
2 == 3
```

Out[65]:

`2 == 3`

is an expression in the same way that `2 + 3`

is an expression, and both of those are evaluated to a value. In the same way that we can say `sum23 = 2 + 3`

we can go

In [66]:

```
are_equal_2_3 = (2 == 3)
print(are_equal_2_3)
```

In [72]:

```
x = are_equal_2_3
print(x == are_equal_2_3)
```

In [73]:

```
if x == are_equal_2_3:
print("The world makes sense")
else:
print("The world doesn't make sense")
```

In [75]:

```
if are_equal_2_3:
print("The world doesn't make sense")
else:
print("The world makes sense")
```

(We can check that the values are indeed of type `bool`

:

In [70]:

```
type(2 != 3)
```

Out[70]:

In [71]:

```
type(False)
```

Out[71]:

)