0% found this document useful (0 votes)
6 views102 pages

Lec 5-7

The document is a lecture on advanced programming in Python, focusing on tuples and various programming concepts such as functions, recursion, and data structures. It includes code examples demonstrating function definitions, tuple manipulations, and algorithms like Fibonacci and Towers of Hanoi. The content is structured for educational purposes in the field of robotics and intelligent machine engineering.

Uploaded by

Muhammad Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views102 pages

Lec 5-7

The document is a lecture on advanced programming in Python, focusing on tuples and various programming concepts such as functions, recursion, and data structures. It includes code examples demonstrating function definitions, tuple manipulations, and algorithms like Fibonacci and Towers of Hanoi. The content is structured for educational purposes in the field of robotics and intelligent machine engineering.

Uploaded by

Muhammad Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 102

Advanced Programming in Python

Lecture 5
Tuples

Dr. Muhammad Jawad Khan

Robotics and Intelligent Machine Engineering Department,


School of Mechanical and Manufacturing,
National University of Sciences and Technology,
Islamabad, Pakistan.
\
Robotics and Intelligent Machine Engineering, NUST 1 / 102
def func_a():
print('inside func_a')

def func_b(y):
print('inside func_b')
return y

def func_c(z):
print('inside func_c')
return z()

print(func_a())
print(5+func_b(2))
print(func_c(func_a))

Robotics and Intelligent Machine Engineering, NUST 2 / 102


Robotics and Intelligent Machine Engineering, NUST 3 / 102
Robotics and Intelligent Machine Engineering, NUST 4 / 102
Robotics and Intelligent Machine Engineering, NUST 5 / 102
Robotics and Intelligent Machine Engineering, NUST 6 / 102
def h(y):
def f(y): def g(y):
pass
x=1 print(x)
#x += 1 #leads to an error without
x += 1 print(x+1)
# line `global x` inside h
print(x) x=5
x=5
x=5 g(x)
h(x)
f(x) print(x)
print(x)
print(x)

Robotics and Intelligent Machine Engineering, NUST 7 / 102


Robotics and Intelligent Machine Engineering, NUST 8 / 102
Robotics and Intelligent Machine Engineering, NUST 9 / 102
Robotics and Intelligent Machine Engineering, NUST 10 / 102
Robotics and Intelligent Machine Engineering, NUST 11 / 102
Robotics and Intelligent Machine Engineering, NUST 12 / 102
Robotics and Intelligent Machine Engineering, NUST 13 / 102
Robotics and Intelligent Machine Engineering, NUST 14 / 102
Robotics and Intelligent Machine Engineering, NUST 15 / 102
Robotics and Intelligent Machine Engineering, NUST 16 / 102
Robotics and Intelligent Machine Engineering, NUST 17 / 102
Robotics and Intelligent Machine Engineering, NUST 18 / 102
def quotient_and_remainder(x, y):
q = x // y
r=x%y
return (q, r)

(quot, rem) = quotient_and_remainder(5,3)


print(quot)
print(rem)

Robotics and Intelligent Machine Engineering, NUST 19 / 102


Robotics and Intelligent Machine Engineering, NUST 20 / 102
def get_data(aTuple):
nums = () # empty tuple
words = ()
for t in aTuple:
# concatenating with a singleton tuple
nums = nums + (t[0],)
# only add words haven't added before
if t[1] not in words:
words = words + (t[1],)
min_n = min(nums)
max_n = max(nums)
unique_words = len(words)
return (min_n, max_n, unique_words)

Robotics and Intelligent Machine Engineering, NUST 21 / 102


Robotics and Intelligent Machine Engineering, NUST 22 / 102
Robotics and Intelligent Machine Engineering, NUST 23 / 102
Robotics and Intelligent Machine Engineering, NUST 24 / 102
Robotics and Intelligent Machine Engineering, NUST 25 / 102
def sum_elem_method1(L): def sum_elem_method2(L):
total = 0 total = 0
for i in range(len(L)): for i in L:
total += L[i] total += i
return total return total

print(sum_elem_method1([1,2,3,4]))
print(sum_elem_method2([1,2,3,4]))

Robotics and Intelligent Machine Engineering, NUST 26 / 102


Robotics and Intelligent Machine Engineering, NUST 27 / 102
L1 = [2,1,3]
L2 = [4,5,6]
L3 = L1 + L2

L1.extend([0,6])

Robotics and Intelligent Machine Engineering, NUST 28 / 102


L = [2,1,3,6,3,7,0] # Do below in order
L.remove(2)
L.remove(3)
del(L[1])
print(L.pop())

Robotics and Intelligent Machine Engineering, NUST 29 / 102


s = "I<3 cs"
print(list(s))
print(s.split('<'))
L = ['a', 'b', 'c']
print(''.join(L))
print('_'.join(L))

Robotics and Intelligent Machine Engineering, NUST 30 / 102


https://docs.python.org/3/tutorial/datastructures.html

L=[9,6,0,3]
print(sorted(L))
L.sort()
L.reverse()

Robotics and Intelligent Machine Engineering, NUST 31 / 102


Robotics and Intelligent Machine Engineering, NUST 32 / 102
Robotics and Intelligent Machine Engineering, NUST 33 / 102
Robotics and Intelligent Machine Engineering, NUST 34 / 102
a=1
b=a
print(a)
print(b)

warm = ['red', 'yellow', 'orange']


hot = warm
hot.append('pink')
print(hot)
print(warm)

Robotics and Intelligent Machine Engineering, NUST 35 / 102


cool = ['blue', 'green', 'grey']
chill = cool[:]
chill.append('black')
print(chill)
print(cool)

Robotics and Intelligent Machine Engineering, NUST 36 / 102


warm = ['red', 'yellow', 'orange']
sortedwarm = warm.sort()
print(warm)
print(sortedwarm)

cool = ['grey', 'green', 'blue']


sortedcool = sorted(cool)
print(cool)
print(sortedcool)

Robotics and Intelligent Machine Engineering, NUST 37 / 102


warm = ['yellow', 'orange']
hot = ['red']
brightcolors = [warm]
brightcolors.append(hot)
print(brightcolors)
hot.append('pink')
print(hot)
print(brightcolors)

Robotics and Intelligent Machine Engineering, NUST 38 / 102


Robotics and Intelligent Machine Engineering, NUST 39 / 102
def remove_dups(L1, L2): def remove_dups_new(L1,
for e in L1: L2):
if e in L2: L1_copy = L1[:]
L1.remove(e) for e in L1_copy:
if e in L2:
L1.remove(e)

L1 = [1, 2, 3, 4] L1 = [1, 2, 3, 4]
L2 = [1, 2, 5, 6] L2 = [1, 2, 5, 6]
remove_dups(L1, L2) remove_dups_new(L1, L2)
print(L1, L2) print(L1, L2)

Robotics and Intelligent Machine Engineering, NUST 40 / 102


def remove_dups(L1, L2): def remove_dups_new(L1, L2):
for e in L1: L1_copy = L1[:]
if e in L2: for e in L1_copy:
L1.remove(e) if e in L2:
L1.remove(e)

L1 = [1, 2, 3, 4]
L1 = [1, 2, 3, 4] L2 = [1, 2, 5, 6]
L2 = [1, 2, 5, 6] remove_dups_new(L1, L2)
remove_dups(L1, L2) print(L1, L2)
print(L1, L2)

Robotics and Intelligent Machine Engineering, NUST 41 / 102


Previously

Robotics and Intelligent Machine Engineering, NUST 42 / 102


Now

Robotics and Intelligent Machine Engineering, NUST 43 / 102


Recursion is the process of repeating items in a
self similar way
• For example, we can define the operation "find
your way home" as:
– If you are at home, stop moving.
– Take one step toward home.
– "find your way home".

Robotics and Intelligent Machine Engineering, NUST 44 / 102


Robotics and Intelligent Machine Engineering, NUST 45 / 102
Robotics and Intelligent Machine Engineering, NUST 46 / 102
Robotics and Intelligent Machine Engineering, NUST 47 / 102
Robotics and Intelligent Machine Engineering, NUST 48 / 102
Robotics and Intelligent Machine Engineering, NUST 49 / 102
Robotics and Intelligent Machine Engineering, NUST 50 / 102
Robotics and Intelligent Machine Engineering, NUST 51 / 102
Robotics and Intelligent Machine Engineering, NUST 52 / 102
Robotics and Intelligent Machine Engineering, NUST 53 / 102
Robotics and Intelligent Machine Engineering, NUST 54 / 102
Robotics and Intelligent Machine Engineering, NUST 55 / 102
Robotics and Intelligent Machine Engineering, NUST 56 / 102
Robotics and Intelligent Machine Engineering, NUST 57 / 102
Robotics and Intelligent Machine Engineering, NUST 58 / 102
def printMove(fr, to):
print('move from ' + str(fr) + ' to ' + str(to))

def Towers(n, fr, to, spare):


if n == 1:
printMove(fr, to)
else:
Towers(n-1, fr, spare, to)
Towers(1, fr, to, spare)
Towers(n-1, spare, to, fr)

print(Towers(3, 'P1', 'P3', 'P2'))

Robotics and Intelligent Machine Engineering, NUST 59 / 102


def TowerOfHanoi(n , s_pole, d_pole, i_pole):
if n == 1:
print("Move disc 1 from pole",s_pole,"to pole",d_pole)
return
TowerOfHanoi(n-1, s_pole, i_pole, d_pole)
print("Move disc",n,"from pole",s_pole,"to pole",d_pole)
TowerOfHanoi(n-1, i_pole, d_pole, s_pole)

n=3
TowerOfHanoi(n, 'A', 'C', 'B')
# A, C, B are the name of poles

Robotics and Intelligent Machine Engineering, NUST 60 / 102


Robotics and Intelligent Machine Engineering, NUST 61 / 102
Robotics and Intelligent Machine Engineering, NUST 62 / 102
Robotics and Intelligent Machine Engineering, NUST 63 / 102
Robotics and Intelligent Machine Engineering, NUST 64 / 102
Robotics and Intelligent Machine Engineering, NUST 65 / 102
Robotics and Intelligent Machine Engineering, NUST 66 / 102
Robotics and Intelligent Machine Engineering, NUST 67 / 102
Robotics and Intelligent Machine Engineering, NUST 68 / 102
Robotics and Intelligent Machine Engineering, NUST 69 / 102
Robotics and Intelligent Machine Engineering, NUST 70 / 102
Robotics and Intelligent Machine Engineering, NUST 71 / 102
Robotics and Intelligent Machine Engineering, NUST 72 / 102
Robotics and Intelligent Machine Engineering, NUST 73 / 102
def fib(x):

"""assumes x an int >= 0


returns Fibonacci of x"""

if x == 0 or x == 1:
return 1
else:
return fib(x-1) + fib(x-2)

Robotics and Intelligent Machine Engineering, NUST 74 / 102


def fib(n):
if n == 1:
return 1
elif n == 2:
return 2
else:
return fib(n-1) + fib(n-2)

Robotics and Intelligent Machine Engineering, NUST 75 / 102


Robotics and Intelligent Machine Engineering, NUST 76 / 102
Robotics and Intelligent Machine Engineering, NUST 77 / 102
Robotics and Intelligent Machine Engineering, NUST 78 / 102
Robotics and Intelligent Machine Engineering, NUST 79 / 102
def isPalindrome(s):

def toChars(s):
s = s.lower()
ans = ''
for c in s:
if c in 'abcdefghijklmnopqrstuvwxyz':
ans = ans + c
return ans

def isPal(s):
if len(s) <= 1:
return True
else:
return s[0] == s[-1] and isPal(s[1:-1])

return isPal(toChars(s))

#print(isPalindrome('eve'))
#print(isPalindrome('Able was I, ere I saw Elba'))
#print(isPalindrome('Is this a palindrome'))

Robotics and Intelligent Machine Engineering, NUST 80 / 102


Robotics and Intelligent Machine Engineering, NUST 81 / 102
Robotics and Intelligent Machine Engineering, NUST 82 / 102
Robotics and Intelligent Machine Engineering, NUST 83 / 102
Robotics and Intelligent Machine Engineering, NUST 84 / 102
Robotics and Intelligent Machine Engineering, NUST 85 / 102
Robotics and Intelligent Machine Engineering, NUST 86 / 102
Robotics and Intelligent Machine Engineering, NUST 87 / 102
Robotics and Intelligent Machine Engineering, NUST 88 / 102
Robotics and Intelligent Machine Engineering, NUST 89 / 102
Robotics and Intelligent Machine Engineering, NUST 90 / 102
Robotics and Intelligent Machine Engineering, NUST 91 / 102
Robotics and Intelligent Machine Engineering, NUST 92 / 102
Robotics and Intelligent Machine Engineering, NUST 93 / 102
def lyrics_to_frequencies(lyrics):
myDict = {}
for word in lyrics:
if word in myDict:
myDict[word] += 1
else:
myDict[word] = 1
return myDict

she_loves_you = ['she', 'loves', 'you', 'yeah', 'yeah',


'yeah','she', 'loves', 'you', 'yeah', 'yeah', 'yeah',
'she', 'loves', 'you', 'yeah', 'yeah', 'yeah',

'you', 'think', "you've", 'lost', 'your', 'love',


'well', 'i', 'saw', 'her', 'yesterday-yi-yay',
"it's", 'you', "she's", 'thinking', 'of',
'and', 'she', 'told', 'me', 'what', 'to', 'say-yi-yay']

beatles = lyrics_to_frequencies(she_loves_you)
print(beatles)

Robotics and Intelligent Machine Engineering, NUST 94 / 102


Robotics and Intelligent Machine Engineering, NUST 95 / 102
Robotics and Intelligent Machine Engineering, NUST 96 / 102
def most_common_words(freqs): def words_often(freqs, minTimes):
values = freqs.values() result = []
best = max(freqs.values()) done = False
words = [] while not done:
for k in freqs: temp = most_common_words(freqs)
if freqs[k] == best: if temp[1] >= minTimes:
words.append(k) result.append(temp)
return (words, best) for w in temp[0]:
del(freqs[w]) #remove word from dict
else:
done = True
return result

#print(words_often(beatles, 5))

Robotics and Intelligent Machine Engineering, NUST 97 / 102


def fib(n):
if n == 1:
return 1
elif n == 2:
return 2
else:
return fib(n-1) + fib(n-2)

Robotics and Intelligent Machine Engineering, NUST 98 / 102


Robotics and Intelligent Machine Engineering, NUST 99 / 102
Robotics and Intelligent Machine Engineering, NUST 100 / 102
def fib_efficient(n, d):
if n in d:
return d[n]
else:
ans = fib_efficient(n-1, d)+fib_efficient(n-2, d)
d[n] = ans
return ans

d = {1:1, 2:2}

argToUse = 34
#print("")
#print('using fib')
#print(fib(argToUse))
#print("")
#print('using fib_efficient')
#print(fib_efficient(argToUse, d))

Robotics and Intelligent Machine Engineering, NUST 101 / 102


Robotics and Intelligent Machine Engineering, NUST 102 / 102

You might also like