my_var = None
type(my_var)NoneTypeNone and PrintA special data type, and a valuable function
None typeWe have covered a few data types so far. There are infinitely many integers, floating point numbers, and strings. (Actually finite, because of how computers store information. Take CS61C to learn more.)
However, for the NoneType data type, there is only one value: None.
my_var = None
type(my_var)NoneTypeNone is strange: Cells will not output expressions that evaluate to None.
None is also referred to as the null value.
print functionThe print function displays values to the screen (in this case, our Jupyter notebook). Each call to print displays information on a new line.
print(2)
print("Hello, world!")2
Hello, world!print lets us to see information about executed statements that are not just the last line of the cell. Could you see why this would be a useful feature for debugging?
print is ultra-convenient because it can take as many arguments as you want.
x = 3
y = 4
print(x, "+", y, "is equal to", x + y)3 + 4 is equal to 7Trace through the above program. Do you see how expressions are evaluated before printing?
printCell output and print output seem very closely related:
However, they have subtle differences. We won’t expect you to know all of them. Instead, the purpose of this section is to get you used to tracing programs, where you closely consider Python’s order of execution of each statement, expression, function call, and so on.
Because the print output is intended for human view, it displays strings without quotes. Contrast this with the cell output, which includes the quotes.
print(2)
"Hello, world!"2'Hello, world!'This is now super pedantic, but consider the following cells. Can you explain what is happening?
my_varprint(my_var)NoneWhen print is evaluated as the last line in a cell, it displays the value of the evaluated argument (here, None). print returns None, so the cell does not additionally output anything,
Try this challenge on for size.
print(my_var)
print(3)
45None
345Each line explained:
print call expression. This prints the value of my_var to the screen, which evaluates to None. print returns None, but this second None is not output to the sceen because it is not the last line.print call expression. This prints the value of 3 to the screen. print returns None, but this second None is not output to the sceen because it is not the last line.45; because it is the last line, output to screen.You’re thinking like a computer scientist now!