Read these:
Complete reverse_string and primes challenges in challenge folder.
In [8]:
def t(num):
if 10<= num < 15:
print("hot")
elif num > 15:
print("hotter")
else:
print("cold")
t(5)
In [12]:
i in range(4):
for j in range(10):
if j % 3 == 0:
continue
if j > 7 and j % 2 == 0:
break
else:
print('i equals', i)
print('j equals', j)
print('-----------')
In [27]:
a = 1
assert a == 1
Try evaluating each of the lines below in your python REPL. What's going on?
a = [1, 2, 3]
b = a
b is a
b == a
b = a[:]
b is a
b == a
If you have a hypothesis about the difference between is and ==, devise some tests you could try out to disprove your hypothesis. Implement them. Were you correct?
comparison priority
not has the highest priority and or the lowest, so that A and not B or C is equivalent to (A and (not B)) or C.
Short circuit operators
In [ ]:
l = [1, 2, 3]
b = [4, 5, 6]
l+b
l[:]+b[:2]
l[::-1]+b[1:]
l.extend(b)
l
In [51]:
l = [1, 2, 3]
b = [4, 5, 6]
b[2] = 3
b[::-1] = l
b[2]=l[1]
b
Out[51]:
In [8]:
l = [1, 2, 3]
str2 = 'World!'
str2+str(l[2])
str2 = list(str2)
str2.insert(3, 'HI')
str2 = ''.join(str2)
str2
str1 = 'Hello World!'
str1 = str1[:3] + 'lllll' + str1[4:]
str1
Out[8]:
In [12]:
l = [1, 2, 3]
l.remove(2)
l
Out[12]:
In [13]:
l = [1, 2, 3]
l.pop(0)
l
Out[13]:
In [20]:
l = [1, 2, 3]
del l[:]
l.clear()
l
Out[20]:
In [23]:
l = [1, 2, 3]
l.index(2)
Out[23]:
In [25]:
l.count(1)
Out[25]:
In [35]:
str1 = 'hello'
str1.capitalize()
Out[35]:
In [41]:
str1.upper()
Out[41]:
In [62]:
str1 = 'Hello World ! '
str1.strip().strip('H').strip('')
Out[62]:
In [82]:
a = 'Split a sentence into a list'
a.split()
a = a.replace(' ', ':')
a.split(':')
Out[82]: