How was lab? Was anything confusing?
We covered:
2 + 3 * 4 // 5 - 2 ** (3 % 10)
-4
1 +
File "/tmp/ipykernel_850159/1278102326.py", line 1 1 + ^ SyntaxError: invalid syntax
2 * * 5
File "/tmp/ipykernel_850159/2056278881.py", line 1 2 * * 5 ^ SyntaxError: invalid syntax
# Text cells vs code cells - getting out of the text cell
# (-4) ** 0.5
(-4) ** 0.5
(1.2246467991473532e-16+2j)
0.00000000000000000001
1e-20
(-3) % 5
2
-3 // 5
-1
-3 - (-1) * 5
2
342 // 12 + 23 ** 1 + 23 - 762
-688
32 - 762
-730
9 + 23
32
# Things that were confusing from earlier today
Beyond numbers (like 1
), there are also other kinds of "things" in Python. The first one we'll cover are strings.
Strings are sequences of individual characters. Roughly, a character is a single piece of text. Don't worry too much about the definition of characters for now.
In Python, you can define a string by enclosing a piece of text in "
. We'll see some examples
"Hello AddisCoder!"
'Hello AddisCoder!'
"hello addiscoder"
'hello addiscoder'
"678"
'678'
"True"
'True'
Aside from numbers and strings, there is another "thing" in Python called a boolean. A boolean is a True or False.
The syntax for defining a boolean is to use either True
or False
.
True
True
False
False
true
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_850159/592217714.py in <module> ----> 1 true NameError: name 'true' is not defined
Each of the "things" we've covered has a type. Every "thing" in Python has a type.
You can determine the type of something by doing the following:
type(1)
int
type(1.0)
float
type("hello")
str
type(True)
bool
You can convert between types using int
, float
, str
, and bool
type(1.2)
float
int(1.2)
1
int(1.9)
1
int(-1.9)
-1
type("1")
str
int("1")
1
int("1a")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_850159/4249014070.py in <module> ----> 1 int("1a") ValueError: invalid literal for int() with base 10: '1a'
int("1.2")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_850159/3809317863.py in <module> ----> 1 int("1.2") ValueError: invalid literal for int() with base 10: '1.2'
float(1)
1.0
float("1.2")
1.2
str(1)
'1'
bool(1)
True
bool(2)
True
bool(0)
False
bool(1.0)
True
bool("true")
True
bool("false")
True
bool("")
False
bool(" ")
True
bool("0")
True
Beyond the arithemtic operations we've seen, there are also other kinds of operations. We'll start with equals and not equals. These operations can change the types of the things they operate on
1 == 1
True
1 == 2
False
True == True
True
True == False
False
"hello" == "hello"
True
"hello" == "Hello"
False
"hello" == "hello "
False
1 != 1
False
1 != 2
True
True != True
False
True != False
True
1 = 1
File "/tmp/ipykernel_850159/814398681.py", line 1 1 = 1 ^ SyntaxError: cannot assign to literal
1 == True
True
2 == True
False
bool(2)
True
0 == False
True
"" == False
False
int(True)
1
"" == False
# Strings are not compatible with bools
# Automatically evaluates to false
False
1 == True
True
True == "a"
False
True == "True"
False
1 > 1
False
1 >= 1
True
2 > 1
True
2 >= 1
True
2 < 1
False
2 <= 1
False
You can also compare things like strings and booleans. Explore what happens in lab!
Python also allows for arithmetic operations for non-number types. This can be very confusing, so let's walk through it slowly.
"Hello" + "AddisCoder!"
'HelloAddisCoder!'
"Hello" + " AddisCoder!"
'Hello AddisCoder!'
"Hello" + 1
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_850159/2964887830.py in <module> ----> 1 "Hello" + 1 TypeError: can only concatenate str (not "int") to str
"hello " * 5
'hello hello hello hello hello '
"hello " * 0
''
"hello" // 3
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_850159/3874120328.py in <module> ----> 1 "hello" // 3 TypeError: unsupported operand type(s) for //: 'str' and 'int'
1 + True
2
1 + False
1
1 * True
1
1 * False
0
"hello " ** 2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_850159/435919030.py in <module> ----> 1 "hello " ** 2 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
We will briefly cover an advanced topic called variables. Don't worry if this is confusing - you do not need understand them today. We will cover it in more detail tomorrow.
You've seen variables in math, like in $f(x) = x^2$, $x$ is a variable.
A PROGRAMMING VARIABLE IS NOT A MATH VARIABLE!
A PROGRAMMING VARIABLE IS NOT A MATH VARIABLE!
A programming variable "holds" some value. This value can be any of the types we've seen so far (they can also be other things, but we'll see this later).
The value of a programming variable can change! This is NOT TRUE for math variables!
x = 1
x = 1
x
1
x = 1
x = 2
x
2
x = 1
x = x + 1
x
2
x = "hello"
x
'hello'
x = True
x
True
x = 1
y = 2
x + y
3
x = 1
y = 2
z = x + y
z
3
x = 1
y = x
y
1