You are on page 1of 15

Assignment 1 – Term Work

1) Pass statement
This pass statement is a null operation meaning it does nothing. However, it can be used
when a statement is required by the syntax but contains no code that must be executed.
For instance, the term pass might be useful as a temporary fix for an imaginary function
definition which will actually be programmed in the future.

Example:

for i in range(1, 11):


if i == 5:
pass
else:
print(i)

2) Continue and break statement


Break: A break statement in Python alters the flow of a loop by terminating it once a
specified condition is met.

Continue: The continue statement in Python is used to skip the remaining code inside a
loop for the current iteration only.

Example of a for loop with a break statement

for i in range(10):
if i == 5:
break
print(i)

Output:
0
1
2
3
4

Example of a for loop with a continue statement

for i in range(10):

if i % 2 == 0:

continue
3) Range function
The range() function in Python is used to generate a sequence of numbers. It takes up to
three arguments: start, stop, and step.

• The start argument is the first number in the sequence.

• The stop argument is the last number in the sequence, but it is not included in the
sequence itself.

• The step argument is the increment between each number in the sequence.

EXAMPLE:

# Generate a sequence of numbers from 0 to 9

range(10)

# Generate a sequence of numbers from 1 to 10, incrementing by 2

range(1, 11, 2)

# Generate a sequence of numbers from 10 to 0, decrementing by 1

range(10, 0, -1)

for i in range(10):
print(i)

my_list = list(range(10))

4) Enumerate function:
The enumerate function takes an iterable object as an argument and returns an
enumerate object. The enumerate object is an iterator that yields a tuple
containing a counter and the value of the iterable at that position. The counter
starts at 0 and increments by 1 for each iteration.

example:

my_list = ['apple', 'banana', 'cherry']

for index, value in enumerate(my_list):

print(index, value)
5) Stack using List:
1) STACK:

A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or


First-In/Last Out (FILO) manner.

In stack, a new element is added on the top of the stack and an element is
removed from the top end only. The insert and delete operations are often called
push and pop.

Implementation on using List:

To add/ PUSH an item/element to the top of the stack

→ list.append(x) To retrieve an item/ POP from the top of the stack

(perform pop operation) → list.pop()

Example:

list1=[3,2,1,4]

list1.append(25)

print(list1)

list1.pop()

list1.append(88)

print(list1)

num = int(input("enter number:"))

list1.append(num)

print(list1)

list1.pop()

print(list1)

list1.pop()

print(list1)
6) Queue using List:
Queue is a linear data structure that stores items in a First In First Out (FIFO)
manner. In queue, a new element is added at one end and an element is
removed from the other end.

The insert operation is called Enqueue and deletion is called Dequeue.

Implementation using List •

Lists are not efficient for this purpose, as doing inserts or pops from the
beginning of a list i

slow since all the other elements have to be shifted by one.

To implement a queue, Python provides a module called collections in which a


method

called deque is designed to have fast appends and pops from both its ends.

list = deque([list_elements])

To add an item/element to the top of the stack -5 list. append(x)

To retrieve an item from the top of the stack ( perform pop operation)
list.popleft()

Example:

from collections import deque

list2=[3,2,1,4]

list1=deque([3,2,1,4])

num = int(input("enter number:"))

list1.append(num)

print("first print:",list1)

num = int(input("enter number:"))

list1.append(num)

print("second print",list1)

list1.popleft()

print("third print:",list1)

num = int(input("enter number:"))


Output

enter number:3

first print: deque([3, 2, 1, 4, 3])

enter number:2

second print deque([3, 2, 1, 4, 3, 2])

third print: deque([2, 1, 4, 3, 2])

enter number:1

fourth print deque([1, 4, 3, 2, 1])

8) Nested List (Matrix Addition)


In the matrix addition python program, the program is used to iterate through
every row and every column. The compiler adds the corresponding elements in
the two matrices at each position and stores the result. A matrix can be
implemented as a hierarchical list in Python (list inside a list).

Example:

matrix=[[1,2,3],

[4,5,6],

[7,8,9]]

matrix2=[[9, 8, 7],

[6, 5, 4],

[3, 2, 1]]

for i in range (len(matrix)):

for j in range (len(matrix[0])):

print(matrix[i][j]+matrix2[i][j],end=" ")

print()
OUTPUT:

Matrix A:

[1, 2, 3]

[4, 5, 6]

[7, 8, 9]

Matrix B:

[9, 8, 7]

[6, 5, 4]

[3, 2, 1]

Resultant Matrix (A + B):

[10, 10, 10]

[10, 10, 10]

[10, 10, 10]

8) List Comprehension:
Python, list comprehension is a method or construct that can be used to define
and create a list from a string or another existing list. Besides creating lists, we
can filter and transform data using list comprehension, which has a more human-
readable and concise syntax.

Example:

numbers = [1, 2, 3, 4, 5]

squared = [x**2 for x in numbers]

print(squared)

Output: [1, 4, 9, 16, 25]


9) Dictionary Comprehension:
Like List Comprehension, Python allows dictionary comprehensions. We can
create dictionaries using simple expressions. A dictionary comprehension takes
the form {key: value for (key, value) in iterable}

Example:

keys = ['a','b','c','d','e']
values = [1,2,3,4,5]

Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

10) Decision making statements ( if, if..else, if..elif..else, nested


if):
Decision making statements are used to control the flow of a program. They allow you to
execute different blocks of code depending on the value of a condition.

There are four main types of decision-making statements in Python:

• if statement:

The if statement is used to execute a block of code if a condition is true.

• if..else statement:

The if..else statement is used to execute a block of code if a condition is true, and another
block of code if the condition is false.

• if..elif..else statement:

The if..elif..else statement is used to execute one of several blocks of code depending on
the value of a condition.

• nested if statement:

A nested if statement is an if statement that contains another if statement.

Example:
# if statement
if x > 0:
print("x is positive")

# if..else statement
if x > 0:
print("x is positive")
else:
print("x is not positive")

# if..elif..else statement
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is zero")

# nested if statement
if x > 0:
if y > 0:
print("x and y are both positive")
else:
print("x is positive, but y is not")
else:
print("x is not positive")

11)Filter Function
The filter() function in Python is a built-in function that takes an iterable and a
function as arguments and returns an iterator that yields the elements of the
iterable for which the function returns True.

Syntax:

filter(function, iterable)

Example:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

even_numbers = list(filter(lambda x: x % 2 == 0, numbers))

print(even_numbers)

Output: [2, 4, 6, 8, 10]


12) Reduce function:
The reduce() function in Python is a part of the functools module. It's
used to apply a function of two arguments cumulatively to the items of
an iterable, from left to right, so as to reduce the iterable to a single
value.

Syntax:
from functools import reduce
result = reduce(function, iterable, initializer)
• function: The function to be applied cumulatively to the items of
the iterable.
• iterable: The iterable to be reduced.
• initializer (optional): An initial value (default is None). If the
initializer is not provided, the first item in the iterable will be
used as the initial value.

Example:

from functools import reduce

def add(x, y):

return x + y

numbers = [1, 2, 3, 4, 5]

result = reduce(add, numbers)

print(result)

Output: 15 (1 + 2 + 3 + 4 + 5)
b) Write the following Python Programs:
1) Generate Fibonacci series of N terms:

OUTPUT:

2) Find factorial of a number

OUTPUT:
3) Find numbers which are divisible by 5 between 2000-5000 and also print the
sum of these numbers.:

OUTPUT:

4) To accept a user’s name and print it in reverse

OUTPUT:

5) Print the count of no. of vowels, consonants, digits and special characters.

OUTPUT:
6) Write a python program to demonstrate the use of a recursive function

OUTPUT:

7) Display Fibonacci series using recursion.

OUTPUT:
8) Write a python program to demonstrate the use of a Lambda (Anonymous)

function.

OUTPUT:

9) Write a function for the following:

a) Take 2 lists, and return true if they have at least one common member.

b) Concatenate 2 strings.

c) function to return true if the 2 integer values are equal OR sum is equal to 5.

OUTPUT

You might also like