Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Q1. 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.
The def keyword is used to create or define a function.

In [8]: # function to return a list of odd numbers from 1 to 25

def odd():
l = []
for i in range(1,26):
if i%2 != 0:
l.append(i)

return l

odd()

Out[8]: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]

Q2. Why args and kwargs is used in some functions? Create a


function each for args and **kwargs to demonstrate their use.

*args

args allows us to pass a variable number of non-keyword arguments to a Python function.


In the function, we should use an asterisk () before the parameter name to pass a variable
number of arguments.

In [9]: # Function to find sum of numbers using *args

def add(*numbers):
Sum = 0
for i in numbers:
Sum += i
return Sum

add(1,2,3,4,5)

Out[9]: 15

**kwargs
**kwargs allows us to pass a variable number of keyword arguments to a Python
function. In the function, we use the double-asterisk ( **) before the parameter name to
denote this type of argument.

In [10]: # Function to find total marks using **kwargs

def total_marks(**subject):
total = 0
for marks in subject.values():
total += marks
return total
total_marks(maths=80, science=90, english=80)

Out[10]: 250

Q3. 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].
Iterator in Python is an object that is used to iterate over iterable objects like lists, tuples,
dicts, and sets.

The iterator object is initialized using the iter() method. It uses the next() method for
iteration.

In [77]: l = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

l1 = iter(l)

print(next(l1))
print(next(l1))
print(next(l1))
print(next(l1))
print(next(l1))

2
4
6
8
10

Q4. What is a generator function in python? Why yield keyword


is used? Give an example of a generator function.
Python Generator functions allows us to declare a function that behaves likes an iterator,
allowing programmers to make an iterator in a fast, easy, and clean way. An iterator is an
object that can be iterated or looped upon. It is used to abstract a container of data to make
it behave like an iterable object. Examples of iterable objects that are used more commonly
include lists, dictionaries, and strings.

Yield keyword is used to create a generator function. A type of function that is memory
efficient and can be used like an iterator object.

In [20]: # generator to print even numbers


def print_even(test_list):
for i in test_list:
if i % 2 == 0:
yield i

# initializing list
test_list = [1, 4, 5, 6, 7]

# printing initial list


print("The original list is : " + str(test_list))
# printing even numbers
print("The even numbers in list are : ")
for j in print_even(test_list):
print(j)

The original list is : [1, 4, 5, 6, 7]


The even numbers in list are :
4
6

Q5. Create a generator function for prime numbers less than


1000. Use the next() method to print the first 20 prime numbers.
In [55]: def prime_generator():
for j in range(1,1001):
number = j
i = 2
factor = 0
while i < number:
if number%i== 0:
factor +=1
i+=1
if factor == 0 and number > 1:
yield number

prime_no = prime_generator()

print("1st 20 prime no = ", end=" ")


for j in range(1,21):
print(next(prime_no), end=' ')

1st 20 prime no = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71

Q6. Write a python program to print the first 10 Fibonacci


numbers using a while loop.
In [65]: i = 1
n1 = 0
n2 = 1
while i <= 10:
fab_no = n1+n2
print(fab_no)
n1,n2 = n2,fab_no
i+=1

1
2
3
5
8
13
21
34
55
89
Q7. Write a List Comprehension to iterate through the given
string: ‘pwskills’.
Expected output: ['p', 'w', 's', 'k', 'i', 'l', 'l', 's']

In [69]: s = 'pwskills'
[i for i in s]

Out[69]: ['p', 'w', 's', 'k', 'i', 'l', 'l', 's']

Q8. Write a python program to check whether a given number


is Palindrome or not using a while loop.

In [71]: num=int(input("Enter a number = "))


temp=num
rev=0
while(num>0):
dig=num%10
rev=rev*10+dig
num=num//10
if(temp==rev):
print("The number is palindrome!")
else:
print("Not a palindrome!")

Enter a number = 22
The number is palindrome!

Q9. Write a code to print odd numbers from 1 to 100 using list
comprehension.
In [75]: [i for i in range(1,101) if i%2!=0]
Out[75]: [1,
3,
5,
7,
9,
11,
13,
15,
17,
19,
21,
23,
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]

You might also like