#!/usr/bin/env python # coding: utf-8 # ### Types and `type` # # Values in Python have different *types*. You can find the type of a value using `type` # In[1]: type(42) # In Python, the integer type is called `int` # In[2]: type(3.14) # In[3]: type("Hello") # In Python, the string type is called `str` # Note that an expression like `40 + 2` is evaluated to a value with a type # In[4]: type(40 + 2) # Something like this would also work: # In[5]: n = 5 type(n + 2) # In[6]: type(n) # ### Floats # If we perform division, we will get a type that's possibly different from the type of the values in the expression: # In[9]: type(5/2) # In[10]: type(4/2) # 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[12]: type(2 * 3.14) # We can go a little crazy: # In[13]: type(type(42)) # Functions are also objects and have a type # In[14]: type(print) # 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), `int`s 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[15]: 1.0 * 10**300 # In[16]: 1.0 * 10**350 # ### 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[17]: 'abc' # In[18]: "abc" # In[19]: "ab c" # You *can* store strings that contain multiple lines: # In[20]: '''a bcd ef''' # (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. You saw this with docstrings, although docstrings are treated in a special way in Python) # `\n` is a special character. It means "switch to a new line." # In[21]: print("ab\ncd") # Suppose we want quotes inside quotes: # In[22]: "Getting into medical school is "easy"" # 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[23]: 'Getting into medical school is "easy"' # In[24]: "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[25]: "Getting into medical school is \"easy\"" # (Note that Python 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[27]: float("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[28]: print(float("3.14")) print("3.14") # 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[29]: 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[30]: float("forty-two") # Converting floats to string makes sense if, for example, we want to use "string addition": # In[31]: "abc" + 123 # But: # In[32]: "abc" + str(123) # Floats and strings can be converted to integers: # In[33]: int(3.14) # In[34]: int(3.75) # (Integers are *truncated* rather than rounded) # In[35]: int("42") # In[36]: int("42.3") # Doesn't work: the string must contain an integer to be converted. But we have the tools to do this: # In[37]: int(float("42.3")) # ### Converting strings to lists and using strings as iterables # # You can convert strings to lists: # In[38]: list("abc") # 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")[1]`). But in fact, there is an easier way: you can just treat the string the way we treated lists: # In[39]: s = "abc" s[1] # In fact, you can directly go # In[40]: "abc"[2] # You can also find the length of a string (i.e., the number of characters the string has) like so: # In[41]: len("hello") # ### 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[42]: 2 == 3 # 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[43]: are_equal_2_3 = (2 == 3) print(are_equal_2_3) # In[44]: x = are_equal_2_3 print(x == are_equal_2_3) # Generally, we would see Boolean values used in conditionals. A Boolean value is what goes after the if. # In[45]: if x == are_equal_2_3: print("The world makes sense") else: print("The world doesn't make sense") # In[46]: 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[47]: type(2 != 3) # In[48]: type(False)