CSV Project3
CSV Project3
CSV Project3
CODE :
def find_maximum(num1, num2, num3):
# Compare the numbers and return the maximum
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >=num1 and num2 >= num3:
return num2
else:
return num3
# Test the func on
number1 = 10
number2 = 5
number3 = 8
maximum = find_maximum(number1, number2, number3)
print("The maximum number is:", maximum)
OUTPUT :
2)Write a Python code using func on calculate_area() that takes base and
height as input arguments and returns area of a triangle as an output. The
formula used is : Area= 1/2 * base * height.
CODE :
def calculate_area(base, height):
area=0.5*base*height
return area
base = 5
height = 8
triangle_area=calculate_area(base,height)
OUTPUT :
3)Write a Python code using func on returning a value.
CODE :
def add(a, b):
# returning sum of a and b
return a + b
def is_true(a):
# returning boolean of a
return bool(a)
# calling func on
res = add(2, 3)
res= is_true(2<5)
OUTPUT :
4)Write a Python code using func on to reverse a string.
CODE:
def reverse_string(input_string):
reversed_string = input_string[::-1]
return reversed_string
reversed = reverse_string(string)
OUTPUT :
5)Write a Python code using func on to print even numbers from a given
list[1,2,3,4,5,6,7,8,9,10].
CODE:
def print_even_numbers(numbers):
if number % 2 == 0:
print(number)
print("Even numbers:")
print_even_numbers(number_list)
OUTPUT :
6)Write a Python code using func on to print average of the numbers from
a given list.
CODE :
def calculate_average(numbers):
total = sum(numbers)
return average
average_result = calculate_average(number_list)
print("Average:", average_result)
OUTPUT :
7) Write a Python code using func on to print followig pa ern:
*
**
***
CODE:
def print_pa ern(row):
for i in range(1, rows + 1):
for j in range(i):
print("*", end="")
print()
# Test the func on
num_rows = 3
print_pa ern(num_row)
OUTPUT :
8)Write a Python code using write()
CODE :
# Open the file in write mode
file.write("Hello,World!\n")
file.close()
OUTPUT :
9) Write a Python code using writelines()
CODE:
f = open("demofile3.txt", "a")
OUTPUT :
10)Write a Python code using read()
CODE:
file1 = open("myfile.txt","w")
L = ["This is Delhi \n","This is Paris \n","This is London \n"]
file1 = open("myfile.txt","r+")
file1.seek(0)
OUTPUT :
11)Write a Python code using readline()
CODE:
f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
OUTPUT :
12)Write a Python code using dump()
CODE:
import json
out_file.close()
OUTPUT :
13)Write a Python code using load()
CODE:
# Python program to read
# json file
import json
f = open('data.json',)
# a dic onary
data=json.load(f)
#list
for i in data['emp_details']:
print(i)
#Closingfile
f.close()
OUTPUT :
14)Write a Python code using writerow()
CODE:
# Python program to demonstrate
# wri ng to CSV
import csv
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']
# data rows of csv file
filename = "university_records.csv"
csvwriter.writerow(fields)
csvwriter.writerows(rows)
15)Write a Python code using reader()
CODE:
import csv
print(row)
OUTPUT :