
After this lesson, you will be able to…
Did you notice that until now, we’ve only used whole numbers? Whole numbers are integers or, in programming terms, int.
Where are all the decimal points?
3.3, 1.1, and 2.2 are all floats.
an_int = 3 # Int!
a_float = 3.0 # Float!
x = 2.5 # Float!
z = 3.5 + 2.5 # Adding floats - normal math.
y = x + z
print(y) # Prints 8.5.
sum = an_int + a_float # What if we add an int and a float?
print(sum) # Prints 6.0. Adding an int to a float will still make a float!A quotient is not necessarily a whole number! * 5 / 2 == 2.5 * 1 / 3 == 1.333...
Therefore, quotients are always floats - even when they look like ints. Python doesn’t distinguish!
6 / 2 == 3.08 / 4 == 2.0Protip: This is called implicit type conversion - Python changed our numbers from ints to floats automatically.
6 / 2 == 3.0: A float. What if you just want the int 3? (Pretty soon, having the right type will be important!). We need explicit type conversion.
int() converts something to an integer.float() converts to a float.str() converts to a stringLet’s try:
x and y, and assign each an int value.z and assign a float value.result, which stores x + y. What type is result? Let’s convert it to other types.-, *, /, or **? What about using x and z?In programming:
1, 0, -5.1.6, -28.2, 0.0.6 + 3.0 = 9.0.4 / 2 = 2.0You can use explicit type conversion to turn one variable type into another:
int() converts to an integer: int(6.0) # 6float() converts to a float: float(6) # 6.0str() converts to a string: str(6) # "6"Up next: Floor Division.
One intermediate variable down! Let’s move on past floats.
What if we want to find the middle index of a list?
# An odd numbered list (length of 5)
characters = ["Green Arrow", "Super Girl", "The Flash", "Wonder Woman", "Batman"]
index = len(characters) / 2 # Index is 2.5
print(characters[index]) # There's no element 2.5!We want 2. Any ideas? This is a very common use case - there must be a way!
Protip: Remember, indexes start at 0!
Python has a shortcut.
Floor division (a.k.a. integer division):
// instead of just /.2.8 will become 2, not 3.Correct the code by using floor division:
Floor division:
// instead of just /.Next up: Specialty Strings!
Our intermediate variables checklist: - Floats - Floor division
What about strings? We might want:
Discussion: How would you go about printing a new line between strings, like below?
Hello!
This is a line later.
| Name | Escape Character | Notes |
|---|---|---|
| Newline | Whitespace: Inserts another line | |
| Tab | Whitespace: Inserts a tab | |
| Quote | " | Print a double quote, don’t end the string |
| Backslash | \ | Prints \ |
This prints, including the quotation marks:
"These are not the droids you're looking for."
- Obi-Wan Kenobi
What else with strings?
String formatting uses index numbers, in {}, as placeholders for strings we later specify in format.
Indexes inside the braces refer to the arguments, in order!
## Indexes count from 0. ##
x = "{0}, {1}, {2}".format("man", "bear", "pig")
print(x) # prints "man, bear, pig"
## They don't need to be in order ##
x = "{1}, {0}, {2}".format("man", "bear", "pig")
print(x) # prints "bear, man, pig"
## We can repeat! ##
x = "{0} {1} {0} {1} {0}".format("Hello", "World")
print(x) # prints "Hello World Hello World Hello"Check it out:
Special strings:
\ escapes special characters: \" will print a quote and \\ prints a \.\n creates a New line; \t creates a Tab.String formatting:
{x}; x corresponds to the number of the argument.x = "{0}, {1}, {2}".format("man", "bear", "pig")
print(x) # prints "man, bear, pig"
x = "{1}, {0}, {2}".format("man", "bear", "pig")
print(x) # prints "bear, man, pig"
x = "{0} {1} {0} {1} {0}".format("Hello", "World")
print(x) # prints "Hello World Hello World Hello"What about number formatting?
x = format(1/3, '.2f')
print(x) # Technically, 1/3 is .333333333333. This prints "0.33"
x = format(2.0024292, '.3f')
print(x) # This prints "2.002"Note: Number formatting creates strings!
Open a new file and name it “solution.py”.
Make a dictionary called “sports” with at least 4 key / value pairs.
Use a loop to print out all the keys and values.
I like "tennis".
There are usually 2 players in tennis.
format to print out your string!BONUS: Every other sport, indent by another tab.
HINT: Use floor division for the bonus! number_of_tabs = loop_counter // 2
2.52)int_index = 5 // 2) - creates an int.\\, \n, \r, \t, \")x = "{0}{1}{0}".format("Hello", "World")
print(x) # prints "HelloWorldHello"
x = format(5200, ',d') # "5,200" -> A string!
x = format(1/3, '.2f') # 0.33int()float()str()