## Types and type()¶

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

In :
type(42)

Out:
builtins.int

In Python, the integer type is called int

In :
type(3.14)

Out:
builtins.float
In :
type("Hello")

Out:
builtins.str

In Python, the string type is called str

Note that an expression like 40 + 2 is evaluated to a value with a type

In :
type(40 + 2)

Out:
builtins.int

Something like this would also work:

In :
n = 5
type(n + 2)

Out:
builtins.int
In :
type(n)

Out:
builtins.int

## Floats¶

If we perform division, we will get a type that's different from the type of the values in the expression:

In :
type(5/2)

Out:
builtins.float
In :
type(4/2)

Out:
builtins.float

Why is type(4/2) still float, even though 4 is divisible by 2? For consistency. Division always produces a float.

Multiplying a string by an integer produces a string (remember that "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 :
type(2 * 3.14)

Out:
builtins.float

We can go a little crazy:

In :
type(type(42))

Out:
builtins.type

In :
type(print)

Out:
builtins.builtin_function_or_method

Here are the types of values that we've seen so far: Integer (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 :
1.0 * 10**300

Out:
1e+300
In :
1.0 * 10**350

---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-26-48bdbd7d799e> in <module>()
----> 1 1.0 * 10**350

OverflowError: int too large to convert to float

## Strings¶

Strings are another type that we saw. Expressions like "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 :
'abc'

Out:
'abc'
In :
"abc"

Out:
'abc'
In :
"ab
c"

  File "<ipython-input-29-b218b3cee1e4>", line 1
"ab
^
SyntaxError: EOL while scanning string literal


You can store strings that contain multiple lines:

In :
'''a
bcd
ef'''

Out:
'a\nbcd\nef'

(Recall that we can have values written in the text of Python programs that Python will simply ignore. We sometimes have multi-line strings in programs that basically act as comments.)

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

In :
print("ab\ncd")

ab
cd


Suppose we want quotes inside quotes:

In :
"Getting into medical school is "easy""

  File "<ipython-input-32-607959b05a1f>", line 1
"Getting into medical school is "easy""
^
SyntaxError: invalid syntax


That didn't work! The reason is that Python reads from left to right. "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 :
'Getting into medical school is "easy"'

Out:
'Getting into medical school is "easy"'
In :
"Getting into medical school is easy'"

Out:
"Getting into medical school is easy'"

Another is to use \" inside double quotes to let Python know that you don't mean to terminate the string

In :
"Getting into medical school is \"easy\""

Out:
'Getting into medical school is "easy"'

(Note that Python itself uses the mixed quotes notation)

(But how do you put forward slashes in quotes? Use "\\" for a single forward slash.)

## Converting values to different types¶

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 :
float("3.14")

Out:
3.14

(You can tell the value is a float because of the lack of quotes when the value is displayed in the shell. But note that if we use print(), both string and floats are printed without quotes)

In :
print(float("3.14"))
print("3.14")

3.14
3.14


Recall that converting from a string to a float is useful if we want to perform arithmetic operations on a value that is stored as text (for example, because we got the value as input from a user)

In :
print("42" * 2)
print(float("42") * 2)

4242
84.0


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

In :
float("forty-two")

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-48-f991ea6ec533> in <module>()
----> 1 float("forty-two")

ValueError: could not convert string to float: 'forty-two'

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

In :
"abc" + 123

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-1e18b1af5699> in <module>()
----> 1 "abc" + 123

TypeError: Can't convert 'int' object to str implicitly

But:

In :
"abc" + str(123)

Out:
'abc123'

Floats and strings can be converted to integers:

In :
int(3.14)

Out:
3
In :
int(3.75)

Out:
3

(Integers are truncated rather than rounded)

In :
int("42")

Out:
42
In :
int("42.3")

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-6076aa6f9770> in <module>()
----> 1 int("42.3")

ValueError: invalid literal for int() with base 10: '42.3'

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

In :
int(float("42.3"))

Out:
42

## Converting strings to lists and using strings as iterables¶

You can convert strings to lists:

In :
list("abc")

Out:
['a', 'b', 'c']

We got a list that consists of the individual characters of the string. This suggests a way to find out the length of the string, and get the individual character of a string (by going list("abc")). But in fact, there is an easier way: you can just treat the string the way we treated lists:

In :
s = "abc"
s

Out:
'b'

In fact, you can directly go

In :
"abc"

Out:
'c'

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

In :
len("hello")

Out:
5

## Booleans¶

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 :
2 == 3

Out:
False

In fact, 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 :
are_equal_2_3 = (2 == 3)
print(are_equal_2_3)

False

In :
x = are_equal_2_3
print(x == are_equal_2_3)

True


Generally, we would see Boolean values used in conditionals. A Boolean value is what goes after the if.

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

The world makes sense

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

The world makes sense


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

In :
type(2 != 3)

Out:
builtins.bool
In :
type(False)

Out:
builtins.bool

)