Unit 1 - Lab Programs
Unit 1 - Lab Programs
Algorithm:
This code illustrates the implementation of two common built-in functions, sum() and max(), along
with tracing the data types of the values involved.
def my_sum(iterable):
"""
Return the sum of elements in the iterable.
"""
result = 0
for item in iterable:
result += item
return result
def my_max(iterable):
"""
Return the maximum element in the iterable.
"""
if not iterable:
raise ValueError("max() arg is an empty sequence")
max_value = iterable[0]
for item in iterable[1:]:
if item > max_value:
max_value = item
return max_value
Aim:
To implement conditional and iterative statements in Python to demonstrate basic
control flow mechanisms.
Algorithm:
1. Conditional Statements:
if statement: Check if a condition is true, and execute a block of code if
it is.
if-else statement: Check if a condition is true; if it is, execute one block
of code; otherwise, execute another block.
if-elif-else statement: Check multiple conditions sequentially, and
execute different blocks of code based on the first true condition
encountered.
2. Iterative Statements:
while loop: Repeat a block of code as long as a specified condition is
true.
for loop: Iterate over a sequence of elements (e.g., a list or a range of
numbers) and execute a block of code for each element.
# Conditional Statements
x = 10
y=5
# If statement
if x > y:
print("Aim: x is greater than y")
# If-else statement
if x < y:
print("Aim: x is less than y")
else:
print("Aim: x is not less than y")
# If-elif-else statement
if x < y:
print("Aim: x is less than y")
elif x == y:
print("Aim: x is equal to y")
else:
print("Aim: x is greater than y")
# Iterative Statements
# While loop
count = 0
while count < 5:
print("Aim: Count:", count)
count += 1
# For loop
for i in range(5):
print("Aim: i:", i)
# Nested loops
for i in range(3):
for j in range(2):
print("Aim: i:", i, "j:", j)
output:
Aim: x is greater than y
Aim: x is not less than y
Aim: x is greater than y
Aim: Count: 0
Aim: Count: 1
Aim: Count: 2
Aim: Count: 3
Aim: Count: 4
Aim: i: 0
Aim: i: 1
Aim: i: 2
Aim: i: 3
Aim: i: 4
Aim: i: 0 j: 0
Aim: i: 0 j: 1
Aim: i: 1 j: 0
Aim: i: 1 j: 1
Aim: i: 2 j: 0
Aim: i: 2 j: 1
EX.No.1.3
Aim:
Write a Python program to read and write from a CSV file by using built-in CSV module.
Algorithm
Step 1: A CSV (Comma Separated Values) format is one of the most simple and common ways to
store tabular data. To represent a CSV file, it must be saved with the .csv file extens
Step 3: Use the built-in open() function to work with CSV files in Python
Step 4: Before use the methods to the csv module, need to import the module first using import
command
Step 6: To write to a CSV file in Python, use the csv.writer() function.The csv.writer() function returns
a writer object that converts the user's data into a delimited string. This string can later be
open the people.CSV file using a text editor and store below data:
2, Jack, California
Program
Read from a CSV file
import csv
reader = csv.reader(file)
print(row)
Output
['SN', ' Name', ' City']
[]
writer = csv.writer(file)
Output
SN,Movie,Protagonist
print(ser)
Output:
B. numpy
import numpy as np
a = np.array([[1, 4, 2],
[3, 4, 6],
[0, -1, 5]])
# sorted array
print ("Array elements in sorted order:\n",
np.sort(a, axis = None))
# sort array row-wise
print ("Row-wise sorted array:\n",
np.sort(a, axis = 1))
Output:
C. Chart
D. Graph
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
OUTPUT