KEMBAR78
Tut10 Solution | PDF | Programming Paradigms | Computing
0% found this document useful (0 votes)
26 views5 pages

Tut10 Solution

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

Tut10 Solution

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

Course- BTech Type- Core

Course Code- CSET101 Course Name- Computational Thinking and


Programming
Year- 2023 Semester- odd
Date- Batch- ALL

Tutorial Assignment: 10 Solutions

Q1. Output - ['bay', 'boy']

words = ["bay", "cat", "boy", "fan"]


b_words = list(filter(lambda word: word.startswith("b"),
words))
print(b_words)

Q2. Output - little

from functools import reduce

words = ["mary", "had", "a", "little", "lamb"]


biggest_word = reduce(lambda x,y: x if len(x) > len(y) else y,
words)
print(biggest_word)

Q3. Output - [1, -2, -3]

l = [1, -2, -3, 4, 5]

def f1(x):
return x<2

m1 = filter(f1, l)
print(list(m1))

Q4. Output -
index-0 element-Python
index-1 element-Exceptions
index-2 element-try and except
index out of range

a = ["Python", "Exceptions", "try and except"]


try:
for i in range(4):
print(f"index-{i} element-{a[i]}")
except:
print("index out of range")

Q5. Output - [5.0, 3.0, 9.0, 0.0]

import math

def is_positive(num):
return num >= 0

def sanitized_sqrt(numbers):
cleaned_iter = map(math.sqrt, filter(is_positive,
numbers))
return list(cleaned_iter)

a = sanitized_sqrt([25, 9, 81, -16, 0])


print(a)

Q6. Output - current year is 2022

def print_year():
year = 1990
def print_current_year():
nonlocal year
year += 32
print("current year is",year)

print_current_year()
print_year()

Q7. Output -
ZeroDivisionError: division by zero
The code gives a ZeroDivisionError

try:
True/False
except ZeroDivisionError:
print("The code gives a ZeroDivisionError")

print(True/False)

Q8. Output -
KeyError: 'euler_number'
This code gives us a KeyError.
dictionary = {'euler_constant': 0.577, 'golden_ratio':1.618}
try:
dictionary = dictionary['euler_number']
except KeyError:
print("This code gives us a KeyError.")

dictionary = dictionary['euler_number']
print(dictionary)

Q9. Output - 89
import functools

lis = [23,56,78,89,12]
print(functools.reduce(lambda a,b: a if a>b else b, lis))

Q10. Output -
value is b
Oops! <class 'ValueError'> occured
value is 0
Oops! <class 'ZeroDivisionError'> occured
value is 1

randomList = ['b', 0, 1]
for entry in randomList:
try:
print("value is", entry)
r = 1/ int(entry)
break
except Exception as e:
print("Oops!", e.__class__, "occured")

Q11. Output -
If there is no locals(), it returns an empty dictionary {}
If there is no locals(), it returns a dictionary {'program': 'Python'}

def dict1():
return locals()

def dict2():
program = 'Python'
return locals()

print("If there is no locals(), it returns an empty


dictionary", dict1())
print("If there is no locals(), it returns a dictionary",
dict2())

Q12. d)
Output - NameError: name 'math' is not defined

from math import factorial


print(math.factorial(5))

Q13. b)

Q14.
a) Output – TypeError: object of type 'map' has no len()
x = [34, 56]
print(len(map(str, x)))

b) Output – [['a'], ['b'], ['c'], ['d']]


x = 'abcd'
print(list(map(list, x)))

c) Output – NameError: name 'abcd' is not defined


x = abcd
print(list(map(list, x)))

d) Output - TypeError: 'int' object is not iterable

x = 1234
(list(map(list, x)))

Q15. Output –
Takes in a number n, returns the square of n

def square(n):
#This is a comment
'''Takes in a number n, returns the square of n'''
return n**2

print(square.__doc__)
Q16. Output –
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__':
<_frozen_importlib_external.SourceFileLoader object at
0x00000208837B6D00>, '__spec__': None, '__annotations__': {},
'__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\shiva\\
PycharmProjects\\pythonProject\\chk.py', '__cached__': None, 'fun': <function
fun at 0x00000208837FE160>, 'a': 20, 'b': 3.14, 's': 'python is programming'}

{'a': 45}

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__':


<_frozen_importlib_external.SourceFileLoader object at
0x00000208837B6D00>, '__spec__': None, '__annotations__': {},
'__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\shiva\\
PycharmProjects\\pythonProject\\chk.py', '__cached__': None, 'fun': <function
fun at 0x00000208837FE160>, 'a': 20, 'b': 6.28, 's': 'python is programming'}

def fun():
a = 45
global b
b = 6.28
print(locals())
print(globals())

a = 20
b = 3.14
s = "python is programming"
print(locals())
fun()

You might also like