Week 1 class demo
This is a copy of what I demonstrated in class when this was first run.
import this
The 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 = 1
print(x * x)
1
print(type(x))
x = x * 100
print(x)
100
phrase = "no semicolons needed!"
print("You said " + phrase)
You said no semicolons needed!
# strings
print('hello', "hello")
hello hello
print("""hello there
there are newlines here
this is valid""")
hello there
there are newlines here
this is valid
text = """hello there
there are newlines here
this is valid"""
text
'hello there\nthere are newlines here\nthis is valid'
3
3
type(3)
int
type(3.0)
float
print(3 * 6)
18
print(3 * 6.0)
18.0
print(234567897654345678987654567897654 * 34656789876)
8129370340661731474943692757700569751350904
text = "3.0"
float(text)
3.0
text = "3"
int(text)
3
int(3.9)
3
int("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.99
print("$" + str(39.99))
$39.99
print(3, 9, 9)
3 9 9
isinstance("hello", str)
True
isinstance("hello", int)
False
isinstance(3.99, (int, float))
True
type("hello") == str
True
type("hello") == int
False
type(3.99) == str
False
try:
print("hello")
x = int("4.9")
except:
print("failure")
hello
failure
num = "3.9"
try:
num = float(num)
except:
print("oops")
num
3.9
text = "009939"
print(text.isdigit())
print(text.isnumeric())
True
True
print('二百三'.isdigit())
False
print('二百三'.isnumeric())
True
print('௩௰௭'.isnumeric())
True
l = []
type(l)
list
lista = [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)
139878782947272
id(listb)
139878782946952
id(listc)
139878782978504
listd = lista
lista
[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)
139878782947272
id(listd)
139878782947272
liste = lista[:]
id(lista)
139878782947272
id(liste)
139878782977992
lista
[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)
5
lista.index(1)
0
lista.remove(1)
lista
[2, 3, 4, 5, 6, 1, 1, 1, 1]
lista.index(1)
5
lista.index(6)
4
lista[6]
1
type(lista[6])
int
lista
[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
1
for aoeuidht in "hello world":
print(aoeuidht)
h
e
l
l
o
w
o
r
l
d
for 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 iterable
print(aoeuidht)
d
data = "haouh"
for data in data:
print(data)
h
a
o
u
h
data
'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
ds
listc
['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)
5
count = 0
for item in listc:
# do stuff
#count = count + 1
count += 1
print(count)
5
lista
[2, 3, 4, 5, 6, 1, 1, 1, 1]
sum(lista)
24
sum(lista) / len(lista)
2.6666666666666665
total = 0
for item in lista:
# do stuff
total += item
print(total)
24
base = ""
for item in listc:
text = str(item)
base += text
print(base)
d1[]923.99
lista
[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 + listc
base = []
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)
True
True
True
False
False
if bool_expression:
# do this
else:
# do that
if "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 lowercase
if "c".isdigit():
print("this is a digit")
lista
[2, 3, 4, 5, 6, 1, 1, 1, 1]
assert len(lista) == 9
lista.pop(2)
4
assert len(lista) == 9
AssertionError Traceback (most recent call last)
<ipython-input-173-37678435df7c> in <module>
----> 1 assert len(lista) == 9
AssertionError:
Last updated
Was this helpful?