Specifies:
We shall use PEP8 throughout this course.
In [1]:
i = 2
type(i), id(i)
Out[1]:
In [2]:
i = "foo"
type(i), id(i)
Out[2]:
In [3]:
i = 2
f = 1.2
s = i + f
print(type(i), type(f))
print(type(i + f))
print(s)
In [4]:
# print("I am " + 20 + " years old")
we must explicitely cast it:
In [5]:
print("I am " + str(20) + " years old")
Note that many other languages like Javascript allow implicit casting:
In [6]:
%%javascript
element.text("I am " + 20 + " years old")
In [7]:
%%javascript
element.text("1" + 1)
In [8]:
%%javascript
element.text(1 + "1")
In [9]:
%%perl
print "1" + 1
In [10]:
x = 2
x < 2 or x >= 2
Out[10]:
In [11]:
x > 0 and x < 10
Out[11]:
In [12]:
not x < 0
Out[12]:
In [13]:
x = True
type(x)
Out[13]:
In [14]:
True and False
Out[14]:
In [15]:
True or False
Out[15]:
In [16]:
not True
Out[16]:
In [17]:
i = 2
f = 1.2
c = 1+2j
type(i), type(f), type(c)
Out[17]:
In [18]:
c2 = i + c
print(c2, type(c2))
In [19]:
%%python2
import sys
import math
max_i = sys.maxint
print(max_i, math.log(max_i, 2))
long
type when they exceed maxint
In [20]:
%%python2
import sys
max_i = sys.maxint
print(type(max_i), type(max_i+1))
In [21]:
type(2**63 + 1)
Out[21]:
In [22]:
import sys
sys.float_info
Out[22]:
In [23]:
sys.int_info
Out[23]:
In [24]:
i = 2
f = 4.2
c = 4.1-3j
s1 = i + f
s2 = f - c
s3 = i * c
print(s1, type(s1))
print(s2, type(s2))
print(s3, type(s3))
In [25]:
-3 / 2
Out[25]:
In [26]:
%%python2
q = -3 / 2
print(q, type(q))
In [27]:
%%python2
q = -3.0 / 2
print(q, type(q))
In [28]:
-3.0 // 2
Out[28]:
In [29]:
x = 23
x < 24
Out[29]:
In [30]:
x >= 22
Out[30]:
In [31]:
23 < x < 100
Out[31]:
In [32]:
23 <= x < 100
Out[32]:
In [33]:
5 % 3
Out[33]:
In [34]:
2 ** 3
Out[34]:
In [35]:
2 ** 0.5
Out[35]:
In [36]:
abs(-2 - 1j)
Out[36]:
In [37]:
round(2.3456), round(2.3456, 2)
Out[37]:
In [38]:
float(2)
Out[38]:
In [39]:
int(2.5)
Out[39]:
In [40]:
import math
math.log(16), math.log(16, 2), math.exp(2), \
math.exp(math.log(10))
Out[40]:
In [41]:
x = 2
old_id = id(x)
x += 1
print(id(x) == old_id)
How about booleans?
In [42]:
x = True
y = False
print(id(x) == id(y))
x = False
print(id(x) == id(y))
There is only one True
and one False
object.
How about lists?
In [43]:
l1 = [0, 1]
old_id = id(l1)
l1.append(2)
old_id == id(l1)
Out[43]:
Lists are mutable.
operation | behaviour |
---|---|
x in s |
True if an item of s is equal to x, else False |
x not in s |
False if an item of s is equal to x, else True |
s + t |
the concatenation of s and t |
s * n or n * s |
equivalent to adding s to itself n times |
s[i] |
ith item of s, origin 0 |
s[i:j] |
slice of s from i to j |
s[i:j:k] |
slice of s from i to j with step k |
len(s) |
length of s |
min(s) |
smallest item of s |
max(s) |
largest item of s |
s.index(x[, i[, j]]) |
index of the first occurrence of x in s (at or after index i and before index j) |
s.count(x) |
total number of occurrences of x in s |
In [44]:
l = [1, 2, 2, 3]
l
Out[44]:
In [45]:
l[1]
Out[45]:
In [46]:
# l[4] # raises IndexError
In [47]:
l[-1]
Out[47]:
In [48]:
l = []
for i in range(20):
l.append(2*i + 1)
l[:10]
Out[48]:
In [49]:
l[-4:]
Out[49]:
In [50]:
i = 2
l[i:i+3]
Out[50]:
In [51]:
l[2:10:3]
Out[51]:
In [52]:
l[::-1]
Out[52]:
Lists are mutable, elements can be added or changed:
In [53]:
l = []
l.append(1)
l.append(2)
l.append(2)
l
Out[53]:
In [54]:
l[1] = 12
In [55]:
l.extend([3, 4, 5])
len(l), l
Out[55]:
In [56]:
l = [1, -1, "foo", 2, "bar"]
l
Out[56]:
In [57]:
for element in l:
print(element)
In [58]:
for i, element in enumerate(l):
print(i, element)
In [59]:
l = [3, -1, 2, 11]
for e in sorted(l):
print(e)
the sorting key can be specified using the key
argument
In [60]:
shopping_list = [
["apple", 5],
["pear", 2],
["milk", 1],
["bread", 3],
]
for product in sorted(shopping_list, key=lambda x: -x[1]):
print(product)
In [61]:
t = ()
print(type(t), len(t))
t = ([1, 2, 3], "foo")
type(t), len(t)
Out[61]:
In [62]:
t
Out[62]:
tuples can be indexed the same way as lists
In [63]:
t[1], t[-1]
Out[63]:
tuples contain immutable references, however, the objects may be mutable
In [64]:
t = ([1, 2, 3], "foo")
# t[0]= "bar" # this raises a TypeError
In [65]:
for e in t:
print(id(e))
print("\nChanging an element of t[0]\n")
t[0][1] = 11
for e in t:
print(id(e))
print("\n", t)
In [66]:
d = {} # empty dictionary same as d = dict()
d["apple"] = 12
d["plum"] = 2
d
Out[66]:
equivalent to
In [67]:
d = {"apple": 12, "plum": 2}
d
Out[67]:
In [68]:
del d["apple"]
d
Out[68]:
In [69]:
d = {"apple": 12, "plum": 2}
for key in d.keys():
print(key, d[key])
In [70]:
for value in d.values():
print(value)
In [71]:
for key, value in d.items():
print(key, value)
In [72]:
d = {}
d[1] = "a" # numeric types are immutable
d[3+2j] = "b"
d["c"] = 1.0
d
Out[72]:
In [73]:
d[("apple", 1)] = -2
d
Out[73]:
In [74]:
# d[["apple", 1]] = 12 # raises TypeError
In [75]:
key1 = (2, (3, 4))
key2 = (2, [], (3, 4))
# d = {}
# d[key1] = 1
# d[key2] = 2
In [76]:
s = set()
s.add(2)
s.add(3)
s.add(2)
s
Out[76]:
In [77]:
s = {2, 3, 2}
type(s), s
Out[77]:
In [78]:
s.add(2)
s.remove(2)
# s.remove(2) # raises KeyError, since we already removed this element
s.discard(2) # removes if present, does not raise exception
In [79]:
fs = frozenset([1, 2])
# fs.add(2) # raises AttributeError
In [80]:
fs = frozenset([1, 2])
s = {1, 2}
d = dict()
d[fs] = 1
# d[s] = 2 # raises TypeError
d
Out[80]:
In [81]:
s1 = {1, 2, 3, 4, 5}
s2 = {2, 5, 6, 7}
s1 & s2 # s1.intersection(s2) or s2.intersection(s1)
Out[81]:
In [82]:
s1 | s2 # s1.union(s2) OR s2.union(s1)
Out[82]:
In [83]:
s1 - s2, s2 - s1 # s1.difference(s2), s2.difference(s1)
Out[83]:
In [84]:
s3 = s1 & s2
type(s3), id(s3) == id(s1), id(s3) == id(s2)
Out[84]:
In [85]:
s1 < s2, s1.issubset(s2)
Out[85]:
In [86]:
{1, 2} < s1
Out[86]:
In [87]:
s1.issuperset({1, 6})
Out[87]:
In [88]:
l = [1, 2, 3, -1, 1, 2, 1, 0]
uniq = set(l)
uniq
Out[88]:
In [143]:
import random
n = 10000
l = list(range(n))
random.shuffle(l)
s = set(l)
len(s), len(l)
Out[143]:
In [150]:
{2, -1, 12, "aa", "bb"}
Out[150]:
In [152]:
{"aa", "cc", 2, "bb"}
Out[152]:
In [90]:
%%timeit
2 in l
In [91]:
%%timeit
2 in s
In [92]:
def foo(arg1, arg2, arg3):
print(arg1, arg2, arg3)
foo(1, 2, 3)
foo(1, arg3=2, arg2=3)
In [93]:
def foo(arg1, arg2, arg3=12):
print(arg1, arg2, arg3)
foo(-1, -4)
In [94]:
def arbitrary_positional_f(*args):
print(type(args))
for arg in args:
print(arg)
arbitrary_positional_f(1, 2, -1)
# arbitrary_positional_f(1, 2, arg=-1) # raises TypeError
In [95]:
def arbitrary_keyword_f(**kwargs):
print(type(kwargs))
for argname, value in kwargs.items():
print(argname, value)
arbitrary_keyword_f(arg1=1, arg2=12)
# arbitrary_keyword_f(12, arg=12) # TypeError
In [96]:
def arbitrary_arg_f(*args, **kwargs):
if args:
print("Positional arguments")
for arg in args:
print(arg)
else:
print("No positional arguments")
if kwargs:
print("Keyword arguments")
for argname, value in kwargs.items():
print(argname, value)
else:
print("No keyword arguments")
arbitrary_arg_f()
arbitrary_arg_f(12, -2, param1="foo")
In [97]:
def insert_value(value, l=[]):
l.append(value)
print(l)
l1 = []
insert_value(12, l1)
l2 = []
insert_value(14, l2)
In [98]:
insert_value(-1)
In [99]:
insert_value(-3)
One solution is to create a new list inside a function:
In [100]:
def insert_value(value, l=None):
if l is None:
l = []
l.append(value)
return l
l = insert_value(2)
l
Out[100]:
In [101]:
insert_value(12)
Out[101]:
In [102]:
l = [-1, 0, -10, 2, 3]
Let's sort this list by absolute value. The built-in sorted
's default behavior is not enough right now:
In [103]:
for e in sorted(l):
print(e)
but we can specify the key
to use
In [104]:
for e in sorted(l, key=lambda x : abs(x)):
print(e)
we can use any callable as the key
In [105]:
for e in sorted(l, key=abs):
print(e)
In [106]:
single = 'abc'
double = "abc"
single == double
Out[106]:
std::string
)
In [107]:
s = "abc"
# s[1] = "c" # TypeError
In [108]:
print(id(s))
s += "def"
id(s)
Out[108]:
In [109]:
s = "abcdefghijkl"
s[::2]
Out[109]:
character | Unicode code point | UTF-8 byte sequence |
---|---|---|
a | U+0061 | 61 |
ő | U+0151 | C5 91 |
ش | U+0634 | D8 B4 |
گ | U+06AF | DA AF |
¿ | U+00BF | C2 BF |
ư | U+01B0 | C6 B0 |
Ң | U+04A2 | D2 A2 |
⛵ | U+26F5 | E2 9B B5 |
Python3 automatically encodes Unicode strings when:
In [110]:
s = "ábc"
print(type(s))
with open("file.txt", "w") as f:
f.write(s)
f.write("\n")
and automatically decodes byte sequnces when reading from file:
In [111]:
with open("file.txt") as f:
text = f.read().strip()
print(text)
type(text)
Out[111]:
In [112]:
%%python2
with open("file.txt") as f:
text = f.read().strip()
print(text)
print(text[0], len(text), type(text))
str
type is a byte string, different from Python 3's str
which is a Unicode stringunicode
type for Unicode strings
In [113]:
%%python2
with open("file.txt") as f:
text = f.read().decode('utf8').strip()
print(type(text), len(text))
print(text[0].encode('utf8'))
In [114]:
%%python2
with open("file.txt") as f:
text = f.read().decode('utf8').strip()
print(text[0].encode('utf8'))
# print(text) # raise UnicodeEncodeError - can you interpret the error message?
print(text.encode('utf8'))
In [115]:
unicode_string = "ábc"
utf8_string = unicode_string.encode("utf8")
latin2_string = unicode_string.encode("latin2")
type(unicode_string), type(utf8_string), type(latin2_string)
Out[115]:
In [116]:
len(unicode_string), len(utf8_string), len(latin2_string)
Out[116]:
In [117]:
"abC".upper(), "ABC".lower(), "abc".title()
Out[117]:
In [118]:
s = "\tabc \n"
print("<START>" + s + "<STOP>")
In [119]:
s.strip()
Out[119]:
In [120]:
s.rstrip()
Out[120]:
In [121]:
s.lstrip()
Out[121]:
In [122]:
"abca".strip("a")
Out[122]:
since each function returns a new string, they can be chained after another
In [123]:
" abcd abc".strip().rstrip("c").lstrip("ab")
Out[123]:
In [124]:
"abc".startswith("ab"), "abc".endswith("cd")
Out[124]:
In [125]:
"abc".istitle(), "Abc".istitle()
Out[125]:
In [126]:
" \t\n".isspace()
Out[126]:
In [127]:
"989".isdigit()
Out[127]:
In [128]:
s = "the quick brown fox jumps over the lazy dog"
words = s.split()
words
Out[128]:
In [129]:
s = "R.E.M."
s.split(".")
Out[129]:
In [130]:
"-".join(words)
Out[130]:
use explicit token separators
In [131]:
" <W> ".join(words)
Out[131]:
In [132]:
import urllib.request
wp_url = "https://en.wikipedia.org/wiki/Budapest"
text = urllib.request.urlopen(wp_url).read()
text = text.decode('utf8')
In [133]:
words = text.split()
len(words), len(set(words))
Out[133]:
In [134]:
word_freq = {}
for word in words:
if word not in word_freq:
word_freq[word] = 1
else:
word_freq[word] += 1
In [ ]:
In [135]:
for word, freq in sorted(word_freq.items(), key=lambda x: -x[1])[:20]:
print(word, freq)
various other ways for counting word frequencies here
In [2]:
name = "John"
age = 25
print("My name is {0} and I'm {1} years old. I turned {1} last December".format(name, age))
print("My name is {} and I'm {} years old.".format(name, age))
# print("My name is {} and I'm {} years old. I turned {} last December".format(name, age)) # raises IndexError
print("My name is {name} and I'm {age} years old. I turned {age} last December".format(
name=name, age=age))
In [3]:
print("My name is %s and I'm %d years old" % (name, age))
In [7]:
import sys
age2 = 12
if sys.version_info >= (3, 6):
print(f"My name is {name} and I'm {age} years old {age2}")
In [ ]: