Review of fundamentals

In this notebook, you will guess the output of simple Python statements. You should be able to correctly predict most (if not all) of them. If you get something wrong, figure out why!

Simple operations

3 + 10
Out[3]:
13
3 = 10
  File "<ipython-input-4-ac50e1f5290d>", line 1
    3 = 10
SyntaxError: can't assign to literal
3 == 10
Out[5]:
False
3 ** 10
Out[6]:
59049
'3 + 10'
Out[7]:
'3 + 10'
3 * '10'
Out[8]:
'101010'
a*10
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-ee62d7991d84> in <module>()
----> 1 a*10

NameError: name 'a' is not defined
'a' * 10
Out[10]:
'aaaaaaaaaa'
int('3') + int('10')
Out[11]:
13
int('hello world')
---------------------------------------------------------------------------
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
Out[13]:
2
10 / 4
Out[14]:
2
float(10 / 4)
Out[15]:
2.0
float(10)/4
Out[16]:
2.5
type("True")
Out[17]:
str
type(True)
Out[18]:
bool

Conditionals

a=3
if (a==3):
    print "it's a three!"
it's a three!
a=3
if a==3:
    print "it's a four!"
it's a four!
a=3
if a=4:
    print "it's a four!"
    
  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"
    
we're here
a=3
if a<10:
    print "we're here"
if a<100:
    print "and also here"
    
we're here
and also here
a = "True"
if a:
    print "we're in the 'if'"
else:
    print "we're in the else"
we're in the 'if'
a = "False"
if a:
    print "we're in the 'if'"
else:
    print "we're in the 'else'"
we're in the 'if'
If you were surprised by that, think about the difference between `False` and `"False"`
a = 5
b = 10
if a and b:
    print "a is", a
    print "b is", b
a is 5
b is 10
c = 20
if c or d:
    print "c is", c
    print "d is", d
c is 20
d is
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-27-a7e7c1242441> in <module>()
      2 if c or d:
      3     print "c is", c
----> 4     print "d is", d

NameError: name 'd' is not defined
c = 20
if c and d:
    print "c is", c
    print "d is", d
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-28-40dcd95f37d9> in <module>()
      1 c = 20
----> 2 if c and d:
      3     print "c is", c
      4     print "d is", d

NameError: name 'd' is not defined

Lists

animals= ['dog', 'cat', 'panda']
if panda in animals:
    print "a hit!"
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-29-23ce29a52d3a> in <module>()
      1 animals= ['dog', 'cat', 'panda']
----> 2 if panda in animals:
      3     print "a hit!"

NameError: name 'panda' is not defined
animals= ['dog', 'cat', 'panda']
if "panda" or "giraffe" in animals:
    print "a hit!"
 a hit!
if ["dog", "cat"] in animals:
    print "we're here"
some_nums = range(1,10)
print some_nums
print some_nums[0]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
1
animals= ['dog', 'cat', 'panda']
print animals[-1]
panda
animals= ['dog', 'cat', 'panda']
print animals.index('cat')
1
animals= ['dog', 'cat', 'panda']
more_animals = animals+['giraffe']
print more_animals
['dog', 'cat', 'panda', 'giraffe']
animals= ['dog', 'cat', 'panda']
more_animals = animals.append('giraffe')
print more_animals
None
The above is a tricky one! The issue is that append() does not return a value. It simply appends
animals= ['dog', 'cat', 'panda']
animals.append("giraffe")
print animals
['dog', 'cat', 'panda', 'giraffe']
animals= ['dog', 'cat', 'panda']
for num,animal in enumerate(animals):
    print "Number", num+1, "is", animals
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 "Number", num, "is", animal
print "\nWe have", len(animals), "animals in our list."
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 "\nWe have", len(animals), "animals in our list."
panda
cat
dog

We have 0 animals in our list.

top