Types & Programs¶
Two somewhat unrelated topics, but important nonetheless are Types and Programs.
Types¶
We'll learn a lot of types throughout this quarter, but at this point there are four that we know about:
- Integer (whole numbers): int
- Floating point (approximate): float
- Boolean (yes/no): bool
- String (arbitrary text): str
You can inspect the type of a given value using the type
function:
type(4.0)
float
type("Hello")
str
type(42)
int
type(3 < 5)
bool
x = 3
type(x)
type(3)
int
x = 4.0
type(x)
float
x = "Hello"
type(x)
str
Using different types in an expression usually doesn't work. But some data types "kind of look the same from a distance." For example, floats and ints are both numeric, and so can be used together. However, change happens!
3 + 4.0
7.0
3 < 4.0
True
3 / 4.0
0.75
0.1 + 0.1 + 0.1
0.30000000000000004
Strings are especially hard to combine with other data types as they're quite different. (Consider: what would it mean to do "Hello" - 3
?).
"Hello" - 3
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[20], line 1 ----> 1 "Hello" - 3 TypeError: unsupported operand type(s) for -: 'str' and 'int'
3 + "a"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[21], line 1 ----> 1 3 + "a" TypeError: unsupported operand type(s) for +: 'int' and 'str'
"aaa" - 3
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[22], line 1 ----> 1 "aaa" - 3 TypeError: unsupported operand type(s) for -: 'str' and 'int'
"aaa" / 3
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[23], line 1 ----> 1 "aaa" / 3 TypeError: unsupported operand type(s) for /: 'str' and 'int'
"a" * 3
'aaa'
Some combinations and operations give potentially unexpected results:
3 / 4
0.75
3 // 4
0
3 + True
4
3 + False
3
False + True
1
3 / 0
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[31], line 1 ----> 1 3 / 0 ZeroDivisionError: division by zero
3 / (2 < 5)
3.0
3 // 1
3
Programs¶
All of the homework assignments in CSE 160 deal with "programs." A program is like a recipe, allowing you to save the instructions and when you want to use the program, runs the instructions from top to bottom.
(NB: Notebooks deal with "cells" which exist somewhere between single lines of code and programs. Refer to this lecture's recording for discussion.)
Programs do not automatically print anything out, and so we must make use of the print
function anywhere we need to see the results.
x = 2 * 3
y = x / 5
# nothing printed out
x = 2 * 3
y = x / 5
print(x, y)
6 1.2
Print¶
The print
function comes in a few forms, allowing for combining expressions, strings, and variables in different ways for output. print
's arguments will always be some kind of expression -- something evaluating to a value.
print("Hello World")
Hello World
print("42")
42
print(3 + 6)
9
x = 3
y = 5
print("Value of x is", x, "and the value of y is", y)
Value of x is 3 and the value of y is 5
x + "a"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[42], line 1 ----> 1 x + "a" TypeError: unsupported operand type(s) for +: 'int' and 'str'
str(x)
'3'
str(x) + "a"
'3a'
x = 3
y = 5
print("Value of x is " + str(x) + " and the value of y is " + str(y))
Value of x is 3 and the value of y is 5
x = 3
y = 5
print(f"Value of x is {x} and the value of y is {y}")
Value of x is 3 and the value of y is 5
print("What if I wrote a haiku about a print function.")
What if I wrote a haiku about a print function.
print("What if I\nwrote a haiku about a\nprint function.")
What if I wrote a haiku about a print function.
print("\\n")
\n
print("Hello", end="")
print("World")
HelloWorld
print("")
print("Hello")
print()
print("World")
Hello World