Review of fundamentals A#
In this notebook, you will guess the output of simple Python statements. You should be able to correctly predict almost all (ideally all) of them. If you get something wrong, make sure you understand why! Don’t be satisfied until you’ve understood why every snippet of code returns the value it does or produces the error it does.
Simple operations#
3 + 10
Show code cell output
13
3 = 10
Show code cell output
File "<ipython-input-4-ac50e1f5290d>", line 1
3 = 10
SyntaxError: can't assign to literal
3 == 10
Show code cell output
False
3 ** 10
Show code cell output
59049
'3 + 10'
Show code cell output
'3 + 10'
3 * '10'
Show code cell output
'101010'
a*10
Show code cell output
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-ee62d7991d84> in <module>()
----> 1 a*10
NameError: name 'a' is not defined
'a' * 10
Show code cell output
'aaaaaaaaaa'
int('3') + int('10')
Show code cell output
13
int('hello world')
Show code cell output
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-1e75bebcb0a1> in <module>()
----> 1 int('hello world')
ValueError: invalid literal for int() with base 10: 'hello world'
10 / 5
Show code cell output
2
10 / 4
Show code cell output
2
float(10 / 4)
Show code cell output
2.0
float(10)/4
Show code cell output
2.5
type("True")
Show code cell output
str
type(True)
Show code cell output
bool
Conditionals#
a=3
if (a==3):
print("it's a three!")
Show code cell output
it's a three!
a=3
if a==3:
print("it's a four!")
Show code cell output
it's a four!
a=3
if a=4:
print( "it's a four!")
Show code cell output
File "<ipython-input-21-5b9cab591576>", line 2
if a=4:
^
SyntaxError: invalid syntax
a=3
if a<10:
print ("we're here")
elif a<100:
print( "and also here")
Show code cell output
we're here
a=3
if a<10:
print("we're here")
if a<100:
print("and also here")
Show code cell output
we're here
and also here
a = "True"
if a:
print ("we're in the 'if'")
else:
print ("we're in the else")
Show code cell output
we're in the 'if'
a = "False"
if a:
print("we're in the 'if'")
else:
print("we're in the 'else'")
Show code cell output
we're in the 'if'
Tip
If you were surprised by that, think about the difference between the literal False
and the string "False"
a = 5
b = 10
if a and b:
print("a is", a)
print("b is", b)
Show code cell output
a is 5
b is 10
Lists#
animals= ['dog', 'cat', 'panda']
if panda in animals:
print("found it!")
Show code cell output
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/Users/glupyan/gitRepos/psych750.github.io/notebooks/review_of_fundamentals_a.ipynb Cell 30 in <cell line: 2>()
<a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/review_of_fundamentals_a.ipynb#X41sZmlsZQ%3D%3D?line=0'>1</a> animals= ['dog', 'cat', 'panda']
----> <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/review_of_fundamentals_a.ipynb#X41sZmlsZQ%3D%3D?line=1'>2</a> if panda in animals:
<a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/review_of_fundamentals_a.ipynb#X41sZmlsZQ%3D%3D?line=2'>3</a> print("found it!")
NameError: name 'panda' is not defined
animals= ['dog', 'cat', 'panda']
if "panda" or "giraffe" in animals:
print("found it!")
Show code cell output
found it!
animals= ['dog', 'cat', 'panda']
if "panda" and "giraffe" in animals:
print("found it!")
if ["dog", "cat"] in animals:
print ("we're here")
some_nums = range(1,10)
print(list(some_nums))
print(some_nums[0])
Show code cell output
[1, 2, 3, 4, 5, 6, 7, 8, 9]
1
animals= ['dog', 'cat', 'panda']
print(animals[-1])
Show code cell output
panda
animals= ['dog', 'cat', 'panda']
print(animals.index('cat'))
Show code cell output
1
animals= ['dog', 'cat', 'panda']
more_animals = animals+['giraffe']
print (more_animals)
Show code cell output
['dog', 'cat', 'panda', 'giraffe']
animals= ['dog', 'cat', 'panda']
more_animals = animals.append('giraffe')
print (more_animals)
Show code cell output
None
Note
The above is a tricky one! The issue is that append() does not return a value. It simply appends.
Compare the above with what happens in the code below
animals= ['dog', 'cat', 'panda']
animals.append("giraffe")
print(animals)
Show code cell output
['dog', 'cat', 'panda', 'giraffe']
animals= ['dog', 'cat', 'panda']
for num,animal in enumerate(animals):
print(f"Number {num+1} is {animals}")
Show code cell output
Number 1 is ['dog', 'cat', 'panda']
Number 2 is ['dog', 'cat', 'panda']
Number 3 is ['dog', 'cat', 'panda']
animals= ['dog', 'cat', 'panda']
for num,animal in enumerate(animals):
print(f"Number {num} is {animal}")
print(f"\nWe have {len(animals)} animals in our list.")
Show code cell output
Number 0 is dog
Number 1 is cat
Number 2 is panda
We have 3 animals in our list.
animals= ['dog', 'cat', 'panda']
while animals:
print (animals.pop())
print(f"\nWe have {len(animals)} animals in our list")
Show code cell output
panda
cat
dog
We have 0 animals in our list