Chapter1-IntroToPython
April 26, 2022
1 Chapter 1: Introduction to Python
1.1 Data Types
[1]: a = 3 + 5
[2]: a
[2]: 8
[3]: type(a)
[3]: int
[4]: b = a / 3
[5]: b
[5]: 2.6666666666666665
[6]: type(b)
[6]: float
[7]: c = 3.2e6
[8]: c
[8]: 3200000.0
[9]: d = 1e1000
[10]: d
[10]: inf
[11]: 0.1 + 0.2 == 0.3
[11]: False
1
[12]: 0.1 + 0.2 - 0.3
[12]: 5.551115123125783e-17
[13]: abs(0.1 + 0.2 - 0.3) < 1e-15
[13]: True
[14]: ans = abs(0.1 + 0.2 - 0.3) < 1e-15
[15]: ans
[15]: True
[16]: type(ans)
[16]: bool
[17]: e = 2 + 3j
[18]: type(e)
[18]: complex
[19]: f = input("Enter a number:")
Enter a number: 2
[20]: f + 4
---------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-789e59ac22f9> in <module>
----> 1 f + 4
TypeError: can only concatenate str (not "int") to str
[21]: type(f)
[21]: str
[22]: f
[22]: '2'
[23]: g = float(f)
[24]: type(g)
2
[24]: float
[25]: g
[25]: 2.0
[26]: h = 'Hello, World!'
[27]: h
[27]: 'Hello, World!'
[28]: h[0]
[28]: 'H'
[29]: h[0:5]
[29]: 'Hello'
[30]: h[-1]
[30]: '!'
[31]: h[0:-1] + ', here we are!'
[31]: 'Hello, World, here we are!'
[32]: l = [2, 5, 3]
[33]: l
[33]: [2, 5, 3]
[34]: type(l)
[34]: list
[35]: len(l)
[35]: 3
[36]: l[0]
[36]: 2
[37]: l[1:3]
[37]: [5, 3]
3
[38]: m = l
[39]: m
[39]: [2, 5, 3]
[40]: m[0] = 1
[41]: m
[41]: [1, 5, 3]
[42]: l
[42]: [1, 5, 3]
[43]: n = l.copy()
[44]: n
[44]: [1, 5, 3]
[45]: l[0] = 8
[46]: l
[46]: [8, 5, 3]
[47]: m
[47]: [8, 5, 3]
[48]: n
[48]: [1, 5, 3]
[49]: o = (8, 3, 5)
[50]: o
[50]: (8, 3, 5)
[51]: type(o)
[51]: tuple
[52]: o[0] = 9
4
---------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-52-d5b580f52665> in <module>
----> 1 o[0] = 9
TypeError: 'tuple' object does not support item assignment
[53]: p = o + (1, 2)
[54]: p
[54]: (8, 3, 5, 1, 2)
[55]: type(p)
[55]: tuple
[56]: q = {'tea':'tee', 'I':'ich', 'like':'mag'}
[57]: type(q)
[57]: dict
[58]: q['tea']
[58]: 'tee'
[59]: q['coffee'] = 'Kaffee'
[60]: q
[60]: {'tea': 'tee', 'I': 'ich', 'like': 'mag', 'coffee': 'Kaffee'}
[61]: r = {1, 1, 1, 3, 2}
[62]: r
[62]: {1, 2, 3}
[63]: type(r)
[63]: set
5
1.2 Control structures
[64]: x = int(input('Enter a number: '))
if x > 12:
print('Bigger')
elif x == 12:
print('Equal')
else:
print('Smaller')
print('Really!')
Enter a number: 11
Smaller
Really!
[65]: x = int(input('Enter a number: '))
if x > 12:
print('Bigger')
elif x == 12:
print('Equal')
else:
print('Smaller')
print('Really!')
Enter a number: 13
Bigger
Really!
[66]: l = [3, 4, 8, 9]
s = 0
for i in l:
s = s + i
s
[66]: 24
[67]: for i in range(10):
print(i)
0
1
2
3
4
5
6
7
8
6
9
[68]: list1 = [2, 3, 5]
list2 = [1, 3, 6, 3]
for i in list1:
for j in list2:
if i == j:
print(i, 'appears in both lists.')
break
3 appears in both lists.
[69]: dividend = int(input('Dividend:'))
divisor = int(input('Divisor:'))
result = 0
remainder = dividend
while remainder > divisor:
remainder = remainder - divisor
result = result + 1
print(dividend, 'divided by', divisor, 'equals', result, 'remainder', remainder)
Dividend: 32
Divisor: 3
32 divided by 3 equals 10 remainder 2
1.3 Operators
[70]: 7 + 2
[70]: 9
[71]: 7 - 2
[71]: 5
[72]: 7 * 2
[72]: 14
[73]: 7 / 2
[73]: 3.5
[74]: 7 // 2
[74]: 3
[75]: 7 % 2
7
[75]: 1
[76]: 7 ** 2
[76]: 49
[77]: x = 5
[78]: x == 6
[78]: False
[79]: x < 6
[79]: True
[80]: x >= 5
[80]: True
[81]: x != 6
[81]: True
[82]: y = True
[83]: z = False
[84]: y and z
[84]: False
[85]: y or z
[85]: True
[86]: not y
[86]: False
1.4 Functions
[87]: def divideRemainder(a, b):
result = a // b
remainder = a % b
return (result, remainder)
[88]: a = divideRemainder(5, 2)
8
[89]: a
[89]: (2, 1)
[90]: res, rem = divideRemainder(5, 2)
[91]: res
[91]: 2
[92]: def setToFive(x):
x = 5
print('Within function:', x)
x = 2
print('Before function', x)
setToFive(x)
print('After function', x)
Before function 2
Within function: 5
After function 2
[93]: def intersection(list1, list2):
res = []
for e in list1:
if e in list2:
res.append(e)
return res
[94]: intersection([1, 2, 3], [2, 3, 4])
[94]: [2, 3]
[95]: list1 = [[1], [2], [3]]
def unnest_list(l):
res = []
for idx, i in enumerate(l):
res.append(i[0])
return res
unnest_list(list1)
[95]: [1, 2, 3]
[96]: list1 = [[1], [2], [3]]
def unnest_list(l):
for idx, i in enumerate(l):
9
l[idx] = i[0]
unnest_list(list1)
[97]: list1
[97]: [1, 2, 3]
[98]: def intersection(list1, list2):
assert type(list1) == list and type(list2) == list
res = []
for e in list1:
if e in list2:
res.append(e)
return res
[99]: intersection((1,2,3),(2,3,4))
---------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-99-b592929ea8dc> in <module>
----> 1 intersection((1,2,3),(2,3,4))
<ipython-input-98-62a1dc0662b5> in intersection(list1, list2)
1 def intersection(list1, list2):
----> 2 assert type(list1) == list and type(list2) == list
3 res = []
4 for e in list1:
5 if e in list2:
AssertionError:
[100]: intersection('Hello', 'Hallo')
---------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-100-98d600e260d5> in <module>
----> 1 intersection('Hello', 'Hallo')
<ipython-input-98-62a1dc0662b5> in intersection(list1, list2)
1 def intersection(list1, list2):
----> 2 assert type(list1) == list and type(list2) == list
3 res = []
4 for e in list1:
5 if e in list2:
10
AssertionError:
[101]: import math
[102]: math.factorial(19)
[102]: 121645100408832000
[103]: from math import factorial
factorial(19)
[103]: 121645100408832000
1.5 Errors
[104]: list((3,4,5))
[104]: [3, 4, 5]
[105]: list = [3, 6, 7]
[106]: list((3,4,5))
---------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-106-26074db4bb56> in <module>
----> 1 list((3,4,5))
TypeError: 'list' object is not callable
[107]: dividend = int(input('Dividend:'))
divisor = int(input('Divisor'))
print(dividend/divisor)
Dividend: 3
Divisor 0
---------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-107-ff26790e76e8> in <module>
1 dividend = int(input('Dividend:'))
2 divisor = int(input('Divisor'))
----> 3 print(dividend/divisor)
ZeroDivisionError: division by zero
11
[108]: dividend = int(input('Dividend:'))
divisor = int(input('Divisor'))
try:
print(dividend/divisor)
except:
print('Some exception, fix me!')
Dividend: 3
Divisor 0
Some exception, fix me!
[ ]:
12