Assignment 1 Python
Assignment 1 Python
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:
Continue: The continue statement in Python is used to skip the remaining code inside a
loop for the current iteration only.
for i in range(10):
if i == 5:
break
print(i)
Output:
0
1
2
3
4
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 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:
range(10)
range(1, 11, 2)
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:
print(index, value)
5) Stack using List:
1) STACK:
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.
Example:
list1=[3,2,1,4]
list1.append(25)
print(list1)
list1.pop()
list1.append(88)
print(list1)
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.
Lists are not efficient for this purpose, as doing inserts or pops from the
beginning of a list i
called deque is designed to have fast appends and pops from both its ends.
list = deque([list_elements])
To retrieve an item from the top of the stack ( perform pop operation)
list.popleft()
Example:
list2=[3,2,1,4]
list1=deque([3,2,1,4])
list1.append(num)
print("first print:",list1)
list1.append(num)
print("second print",list1)
list1.popleft()
print("third print:",list1)
enter number:3
enter number:2
enter number:1
Example:
matrix=[[1,2,3],
[4,5,6],
[7,8,9]]
matrix2=[[9, 8, 7],
[6, 5, 4],
[3, 2, 1]]
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]
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]
print(squared)
Example:
keys = ['a','b','c','d','e']
values = [1,2,3,4,5]
• if statement:
• 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:
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:
print(even_numbers)
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:
return x + y
numbers = [1, 2, 3, 4, 5]
print(result)
Output: 15 (1 + 2 + 3 + 4 + 5)
b) Write the following Python Programs:
1) Generate Fibonacci series of N terms:
OUTPUT:
OUTPUT:
3) Find numbers which are divisible by 5 between 2000-5000 and also print the
sum of these numbers.:
OUTPUT:
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:
OUTPUT:
8) Write a python program to demonstrate the use of a Lambda (Anonymous)
function.
OUTPUT:
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