Week 1 class demo
This is a copy of what I demonstrated in class when this was first run.
import thisThe Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!Variables
things about variables
x = 1print(x * x)1print(type(x))x = x * 100
print(x)100phrase = "no semicolons needed!"
print("You said " + phrase)You said no semicolons needed!# strings
print('hello', "hello")hello helloprint("""hello there
there are newlines here
this is valid""")hello there
there are newlines here
this is validtext = """hello there
there are newlines here
this is valid"""text'hello there\nthere are newlines here\nthis is valid'33type(3)inttype(3.0)floatprint(3 * 6)18print(3 * 6.0)18.0print(234567897654345678987654567897654 * 34656789876)8129370340661731474943692757700569751350904text = "3.0"float(text)3.0text = "3"
int(text)3int(3.9)3int("3.9")ValueError Traceback (most recent call last)
<ipython-input-31-05f4ebb5ab90> in <module>
----> 1 int("3.9")
ValueError: invalid literal for int() with base 10: '3.9'str(3.99999999)'3.99999999'print()$39.99print("$" + str(39.99))$39.99print(3, 9, 9)3 9 9isinstance("hello", str)Trueisinstance("hello", int)Falseisinstance(3.99, (int, float))Truetype("hello") == strTruetype("hello") == intFalsetype(3.99) == strFalsetry:
print("hello")
x = int("4.9")
except:
print("failure")hello
failurenum = "3.9"
try:
num = float(num)
except:
print("oops")num3.9text = "009939"
print(text.isdigit())
print(text.isnumeric())True
Trueprint('二百三'.isdigit())Falseprint('二百三'.isnumeric())Trueprint('௩௰௭'.isnumeric())Truel = []type(l)listlista = [1, 2, 3, 4]
listb = ['c', 'p', 'w']
listc = ['d', 1, [], 9, 23.99]lista.append(5)lista[1, 2, 3, 4, 5]id(lista)139878782947272id(listb)139878782946952id(listc)139878782978504listd = listalista[1, 2, 3, 4, 5]listd[1, 2, 3, 4, 5]listd.append(6)listd[1, 2, 3, 4, 5, 6]lista[1, 2, 3, 4, 5, 6]id(lista)139878782947272id(listd)139878782947272liste = lista[:]id(lista)139878782947272id(liste)139878782977992lista[1, 2, 3, 4, 5, 6]lista.append(1)
lista.append(1)
lista.append(1)
lista.append(1)lista[1, 2, 3, 4, 5, 6, 1, 1, 1, 1]lista.count(1)5lista.index(1)0lista.remove(1)lista[2, 3, 4, 5, 6, 1, 1, 1, 1]lista.index(1)5lista.index(6)4lista[6]1type(lista[6])intlista[2, 3, 4, 5, 6, 1, 1, 1, 1]listb #start:stop:step['c', 'p', 'w']listb.append('z')
listb.append('i')
listb.append('e')listb['c', 'p', 'w', 'z', 'i', 'e']listb[2]'w'listb[2:5]['w', 'z', 'i']"hello"[2:4]'ll'listb['c', 'p', 'w', 'z', 'i', 'e']listb[-5:-3]['p', 'w']listb[-1]'e'[listb[-1]]['e']listb['c']for iterable_variable in sequence: do stuff...
listb['c', 'p', 'w', 'z', 'i', 'e']for item in lista:
print(item)2
3
4
5
6
1
1
1
1for aoeuidht in "hello world":
print(aoeuidht)h
e
l
l
o
w
o
r
l
dfor i in 30:
print(i)TypeError Traceback (most recent call last)
<ipython-input-130-b9ed12485d49> in <module>
----> 1 for i in 30:
2 print(i)
TypeError: 'int' object is not iterableprint(aoeuidht)ddata = "haouh"
for data in data:
print(data)h
a
o
u
hdata'h'text = "hello world"
another = "snake socks"
for i in range(len(text)):
a = text[i]
b = another[i]
print(a + b)hs
en
la
lk
oe
ws
oo
rc
lk
dslistc['d', 1, [], 9, 23.99]TypeError Traceback (most recent call last)
<ipython-input-141-aee044fbcb2d> in <module>
1 for item in listc:
----> 2 item + "d"
TypeError: unsupported operand type(s) for +: 'int' and 'str'listc['d', 1, [], 9, 23.99]len(listc)5count = 0
for item in listc:
# do stuff
#count = count + 1
count += 1
print(count)5lista[2, 3, 4, 5, 6, 1, 1, 1, 1]sum(lista)24sum(lista) / len(lista)2.6666666666666665total = 0
for item in lista:
# do stuff
total += item
print(total)24base = ""
for item in listc:
text = str(item)
base += text
print(base)d1[]923.99lista[2, 3, 4, 5, 6, 1, 1, 1, 1]listb['c', 'p', 'w', 'z', 'i', 'e']lista + listb[2, 3, 4, 5, 6, 1, 1, 1, 1, 'c', 'p', 'w', 'z', 'i', 'e']newlist = lista + listb + listcbase = []
for item in newlist[::2]:
# do stuff
base.append(item)print(base)[2, 4, 6, 1, 1, 'p', 'z', 'e', 1, 9]print(1 < 3)TrueTrueTrueFalseFalseif bool_expression:
# do this
else:
# do thatif "c".isdigit():
print("this is a digit")
elif "c".isnumeric():
print("this in numeric")
elif "c".isupper():
print("is uppcase")
elif "c".islower():
print("is lowercase")
else:
print("not a digit")is lowercaseif "c".isdigit():
print("this is a digit")lista[2, 3, 4, 5, 6, 1, 1, 1, 1]assert len(lista) == 9lista.pop(2)4assert len(lista) == 9AssertionError Traceback (most recent call last)
<ipython-input-173-37678435df7c> in <module>
----> 1 assert len(lista) == 9
AssertionError: Last updated
Was this helpful?