Uploaded by ksachin141120

03.02.23

advertisement
03.02.23
March 19, 2023
1. Which keyword is used to create a function? Create a function to return a list of odd numbers
in the range of 1 to 25.
Ans :
#def keyword is used to create a function.
[53]: def get_odd_num () :
a = range(1,25)
odd_num = []
for i in a :
if i % 2 != 0 :
odd_num.append(i)
return odd_num
get_odd_num()
[53]: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]
2. Why *args and **kwargs is used in some functions? Create a function each for *args and
**kwargs to demonstrate their use.
Ans :
*args are used by the user to pass multiple values to the function which returns the data as tuple
format.
[54]: def fn(*args) :
return args
fn(5, [i for i in range(4)])
[54]: (5, [0, 1, 2, 3])
**kwargs are used to pass key-value pairs to functions that return data as a dictionary format.
[38]: def fruits(**fruits) :
total = 0
for i in fruits.values() :
total += i
1
return total
fruits(banana = 5, mango = 6, apple = 7)
[38]: 18
3. What is an iterator in python? Name the method used to initialise the iterator object and
the method used for iteration. Use these methods to print the first five elements of the given
list
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20].
Ans :
In Python, Iterator works to extract the data stored in variables one by one as the loop is executed.
Iter() method is used to initialize the iterator object and next() method used for iteration.
[18]: # For Example,
l = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
convert_iter = iter(l)
print(next(convert_iter) , next(convert_iter) , next(convert_iter) ,␣
↪next(convert_iter) , next(convert_iter))
2 4 6 8 10
4. What is a generator function in python? Why yield keyword is used? Give an example of a
generator function.
Ans :
A generator function allows data to be extracted from the object one by one. The yield keyword is
used to generate a large sequence of values as it gives an immediate output by iterating over the
data.
[41]: # Generator Function Example,
def abc () :
l = [1,2,3, 'sanoj', 'prakash', 5.6, 7.89]
for i in l :
yield i
list(abc())
[41]: [1, 2, 3, 'sanoj', 'prakash', 5.6, 7.89]
5. Create a generator function for prime numbers less than 1000. Use the next() method to
print the first 20 prime numbers.
[47]: def isprime(num) :
for i in range(2,num) :
2
if num%i == 0 :
return False
return True
def primeGenerator(n) :
num = 2
while n > 0 :
if isprime(num) :
yield num
n-= 1
num+= 1
return
a = primeGenerator(20)
print(next(a), next(a), next(a), next(a), next(a), next(a), next(a), next(a),␣
↪next(a), next(a), next(a), next(a), next(a), next(a), next(a), next(a),␣
↪next(a), next(a), next(a), next(a))
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
6. Write a python program to print the first 10 Fibonacci numbers using a while loop
[47]: first_num = 0
second_num = 1
counter = 0
while counter < 10 :
if counter <= 1 :
next_num = counter
else :
next_num = first_num + second_num
first_num = second_num
second_num = next_num
print(next_num)
counter = counter + 1
0
1
1
2
3
5
8
13
21
34
7. Write a List Comprehension to iterate through the given string: ‘pwskills’.
3
Expected output: [‘p’, ‘w’, ‘s’, ‘k’, ‘i’, ‘l’, ‘l’, ‘s’]
[48]: s = 'pwskills'
[i for i in s]
[48]: ['p', 'w', 's', 'k', 'i', 'l', 'l', 's']
8. Write a python program to check whether a given number is Palindrome or not using a while
loop.
[49]: num = int(input("enter your number"))
temp_num = num
rev_num = 0
while num > 0 :
last_digit = num % 10
rev_num = rev_num*10 + last_digit
num = num//10
if temp_num == rev_num :
print('It is a palindrome')
else :
print('it is not a palindrome')
enter your number 525
It is a palindrome
9. Write a code to print odd numbers from 1 to 100 using list comprehension.
[33]: l = range(1,100)
[i for i in l if i % 2 != 0]
[33]: [1,
3,
5,
7,
9,
11,
13,
15,
17,
19,
21,
23,
4
25,
27,
29,
31,
33,
35,
37,
39,
41,
43,
45,
47,
49,
51,
53,
55,
57,
59,
61,
63,
65,
67,
69,
71,
73,
75,
77,
79,
81,
83,
85,
87,
89,
91,
93,
95,
97,
99]
[ ]:
5
Download