- functions
- recursion
05/04/2023
te = () # an empty tuple t = (2, "mit", 3) # a tuple with three elements t
## (2, 'mit', 3)
t = (1, 2, 3, 4, "burkay", "genc") t[3]
## 4
t[2:4]
## (3, 4)
t[3:4]
## (4,)
t[3]
and t[3:4]
are not the samet1 = (1, 2, 3) t2 = ("a", "b", "c") t3 = t1 + t2 t3
## (1, 2, 3, 'a', 'b', 'c')
3 * t1
## (1, 2, 3, 1, 2, 3, 1, 2, 3)
t1 + 4
## TypeError: can only concatenate tuple (not "int") to tuple
t1 = ("burkay", "genc", 41) t2 = (t1, "married", 2) t2
## (('burkay', 'genc', 41), 'married', 2)
t2[0][1]
## 'genc'
t = ("dr", "burkay", "genc") t[0] = "prof."
## TypeError: 'tuple' object does not support item assignment
t = ("dr", "burkay", "genc") t2 = ("prof",) + t[1:3] t2
## ('prof', 'burkay', 'genc')
def foo(t): return 2*t foo((1,2,3))
## (1, 2, 3, 1, 2, 3)
def max(t): maximum = t[0] for item in t: if item > maximum: maximum = item return maximum tup = (3, -2, 5, 7, 8, -4, 1, 3, 1, -5) max(tup)
## 8
def sum(t): curSum = 0 for item in t: curSum = curSum + item return curSum tup = (3, -2, 5, 7, 8, -4, 1, 3, 1, -5) sum(tup)
## 17
def mean(t): return sum(t) / len(t) # Reusing sum() from previous exercise tup = (3, -2, 5, 7, 8, -4, 1, 3, 1, -5) mean(tup)
## 1.7
def sumRational(r1, r2): .... sumRational( (3, 5), (2, 4) )
## (22,20)
[]
a_list = [] L = [2, 'a', 4, [1,2]] len(L) # evaluates to 4
## 4
L[0] # evaluates to 2
## 2
L[2]+1 # evaluates to 5
## 5
L[3] # evaluates to [1,2], another list!
## [1, 2]
i = 2 L[i-1] # evaluates to ‘a’ since L[1]='a' above
## 'a'
li1 = [1,2,3] li2 = [4,5,6] li1 + li2
## [1, 2, 3, 4, 5, 6]
3 * li1
## [1, 2, 3, 1, 2, 3, 1, 2, 3]
L = [2, 1, 3] L[1] = 5 L
## [2, 5, 3]
def max(li): maximum = li[0] for item in li: if item > maximum: maximum = item return maximum li = [3, -2, 5, 7, 8, -4, 1, 3, 1, -5] max(li)
## 8
def replaceMax(li): maxVal = max(li) # reusing the function from the previous slide for i in range(len(li)): if li[i] == maxVal: li[i] = -1 # because lists are mutable, we can do this return li myList = [4, 9, 3, 2, 0, 5, 4, 9, 8, 5, 1, 7, 9] replaceMax(myList)
## [4, -1, 3, 2, 0, 5, 4, -1, 8, 5, 1, 7, -1]
def foo(li): result = [] for num in li: result = result + [3 * num ** 2 - 2 * num + 1] return result foo([1,2,3,4,5,6,7,8,9,10])
## [2, 9, 22, 41, 66, 97, 134, 177, 226, 281]
object.function()
append
an object to the end of the listli1 = [1, 2, 3] li2 = [4, 5, 6] li1.append(li2) li1
## [1, 2, 3, [4, 5, 6]]
extend
a list with elements from another listli1 = [1, 2, 3] li2 = [4, 5, 6] li1.extend(li2) li1
## [1, 2, 3, 4, 5, 6]
count
an element in a listli1 = [1, 2, 3, 4, 2, 4, 1, 3, 2, 5] item = 2 li1.count(item)
## 3
insert
an object into a list at a specific positionli1 = [1, 2, 3] item = 4 li1.insert(1, item) li1
## [1, 4, 2, 3]
remove
the first occurrence of an itemli1 = [1, 2, 3, 2, 3, 1] item = 3 li1.remove(item) li1
## [1, 2, 2, 3, 1]
index
of the first occurence of an itemli1 = [1, 2, 3, 4, 2, 4, 1, 3, 2, 5] item = 4 li1.index(item)
## 3
pop
the item at a specific position
li1 = [1, 2, 3, 4, 5] li1.pop(3)
## 4
li1.pop()
## 5
li1
## [1, 2, 3]
sort
the listli1 = [4, 2, 3, 1, 3, 5] li1.sort() li1
## [1, 2, 3, 3, 4, 5]
sorted
to not mutate the list and return a new listli1 = [4, 2, 3, 1, 3, 5] sorted(li1)
## [1, 2, 3, 3, 4, 5]
li1
## [4, 2, 3, 1, 3, 5]
reverse
the listli1 = [1, 2, 3, 4, 2, 4, 1, 3, 2, 5] li1.reverse() li1
## [5, 2, 3, 1, 4, 2, 4, 3, 2, 1]
l1
and l2
, return a list of their common itemsGiven lists l1
and l2
, return a list of their common items
strategy
l1
and l2
, return a list of their common itemsdef intersect(l1, l2): l3 = [] for item in l1: if l2.count(item) > 0: l3.extend(item) return l3
l1
and l2
, return a list of their common itemsdef intersect(l1, l2): l3 = [] for item in l1: if l2.count(item) > 0: l3.append(item) return l3 l1 = [1,2,3,4,5,6,1,2,3,4,1,2,3] l2 = [2,3,4] intersect(l1, l2)
## [2, 3, 4, 2, 3, 4, 2, 3]
l1
and l2
, return a list of their common itemsdef intersect(l1, l2): l3 = [] for item in l1: if l2.count(item) > 0 and l3.count(item) == 0: l3.append(item) return l3 l1 = [1,2,3,4,5,6,1,2,3,4,1,2,3] l2 = [2,3,4] intersect(l1, l2)
## [2, 3, 4]
(name, surname, phonenumber)
addContact(pb, contact)
: adds contact
tuple into pb
phonebookremoveContact(pb, contact)
: finds and removes contact
from pb
checkNumber(pb, (name, surname))
: checks the provided (name,surname)
in the pb
and outputs the corresponding phone number if found in pb
. If the (name, surname)
tuple is not found in the phone book, then the application outputs name surname not found.
These slides are a direct adaptation of the slides used for MIT 6.0001 course present (as of February 2020) on MIT OCW web site.
Original work by:
Ana Bell, Eric Grimson, and John Guttag. 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Massachusetts Institute of Technology: MIT OpenCourseWare. License: Creative Commons BY-NC-SA.
Adapted by and for:
Assoc. Prof. Dr. Burkay Genç. MUH101 Introduction to Programming, Spring 2020. Hacettepe University, Computer Engineering Department.