ASSIGNMENTpython
ASSIGNMENTpython
SUBMITTED BY:
VISHNU S
2K22BCA347
III-BCA- ‘E’
REFERENCE:
https://www.simplilearn.com/tutorials/python-tutorial/python-functions
https://www.geeksforgeeks.org/python-lists/#adding-elements-into-list
https://www.geeksforgeeks.org/searching-algorithms-in-python/
FUNCTIONS IN PYTHON:
A function in Python is a block of code that performs a specific task, and can be
reused. Functions are a fundamental building block of programming.
Syntax:
def function_name(parameters):
# Function body
return value # (optional)
1. Built-in Functions:
These functions are pre-defined in Python and can be used directly without any
further declaration.
Example:
# Using the built-in len() function
my_list = [1, 2, 3, 4, 5]
print(len(my_list))
Output:
5.
In the above example, len() function is used to find the length of the list. In the print
statement my_list variable is declared within the len().
2. User-defined Functions:
These are functions that users create to perform specific tasks.
Example:
def add_numbers(a, b):
return a + b
result = add_numbers(3, 5)
print(result)
Output:
8.
In the above example,(a,b) is a user defined which define a values to the variable
within the function(3,5).
3. Anonymous Functions (Lambda Functions):
These are small, unnamed functions defined using the lambda keyword. They are
typically used for short, simple operations.
Example:
x = lamba a, b: a + b
print(x(3,4))
x = lamba a, b: a * b
print(x(3,4))
Output:
7
12.
In the above example, the variable value is declared in the print(x(3,4)) by means
(3,4) is an value of a, b which add(a + b) and multiple(a * b).The lamba function are similar
to the user-defined function but without a name.
LIST IN PYTHON
In Python, lists allow us to store multiple items in a single variable. For example, if
you need to store the ages of all the students in a class, you can do this task using a list.
Example:
a = []
# Adding
a.append(10)
print("After append(10):", a)
# Inserting
a.insert(0, 5)
print("After insert(0, 5):", a)
# Extend
a.extend([15, 20, 25])
print("After extend([15, 20, 25]):", a)
Output:
After append(10): [10]
After insert(0, 5): [5, 10]
After extend([15, 20, 25]): [5, 10, 15, 20, 25]
The above example show the some list function like append, insert, extend which
append used to add the element(10) in the list, insert function used to insert the element(5) in
the 0th position in the list and extend function used to add more element in the list in sequence
order.
Removing Elements from List
We can remove elements from a list using:
remove(): Removes the first occurrence of an element.
pop(): Removes the element at a specific index or the last element if no index is
specified.
del function: Deletes an element at a specified index.
Example:
a = [10, 20, 30, 40, 50]
# Removes
a.remove(30)
print("After remove(30):", a)
#Pop
popped_val = a.pop(1)
print("Popped element:", popped_val)
print("After pop(1):", a)
# Deletes
del a[0]
print("After del a[0]:", a)
Output:
After remove(30): [10, 20, 40, 50]
Popped element: 20
After pop(1): [10, 40, 50]
After del a[0]: [40, 50]
The above example show that some remove function from the list like remove, pop
and del. remove function completely remove the particular element(30) from the list, pop
function used to delete the element by their index position and del statement also like pop
which delete the element by their index position.
SEARCH IN PYTHON
Search is a fundamental techniques used to find an element or a value within a
collection of data.
Types of Search
1.Linear Search
2.Binary Search
1.Linear Search:
Linear search is the simplest searching technique. It sequentially checks each element
of the list until it finds the target value.
Example:
List1 = [1,2,3,4,5,6]
Search = int(input(“Enter a number:”))
For i in range (0, len(list1)):
if search == list[i]:
print(i)
Output:
Enter a number:4
3.
In the example, search the element one by one in the given array which resulted the
index position of element.
2. Binary Search:
Binary search is a more efficient searching technique suitable for sorted lists. It
repeatedly divides the search interval in half until the target value is found.
Example:
def binary(array, x, low, high):
while low <= high:
mid = low+(high-low)//2
if x == array[mid]:
return mid
elif x > array [mid]:
low = mid + 1
else:
high = mid – 1
return -1
array = [3,4,5,6,7,8,9]
x=4
result = binary(array, x, 0, len(array)-1)
if result! =-1:
print(“Element is present at index”+str(result))
else:
print(“Not found”)
Output:
Element is present at index:6.
In the above example, first it will divide the given array into two half to find the mid
element, after the element are compared equal to the mid if, that element is equal it result the
output otherwise it compare the element to high or low then repeat the process of finding the
middle element.