CSV Project3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

1)Write a Python code using func on to find maximum of three numbers.

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

# Test the func on

base = 5

height = 8

triangle_area=calculate_area(base,height)

print("The area of the triangle is:", triangle_area)

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)

print("Result of add func on is{}".format(res))

res= is_true(2<5)

print("\nResult of is_true func on is {}".format(res))

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

# Test the func on

string = "Hello, World!"

reversed = reverse_string(string)

print("Reversed string:", reversed)

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):

for number in numbers:

if number % 2 == 0:

print(number)

# Test the func on

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

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)

average = total / len(numbers)

return average

# Test the func on

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

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 = open("output.txt", "w")

# Write text to the file

file.write("Hello,World!\n")

file.write("This is a sample text.\n")

file.write("Wri ng data to a file.")

# Close the file

file.close()

OUTPUT :
9) Write a Python code using writelines()

CODE:

f = open("demofile3.txt", "a")

f.writelines(["\nSee you soon!", "\nOver and out."])


f.close()

#open and read the file a er the appending:


f = open("demofile3.txt", "r")
print(f.read())

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"]

# \n is placed to indicate EOL (End of Line)


file1.write("Hello \n")
file1.writelines(L)
file1.close() #to change file access modes

file1 = open("myfile.txt","r+")

print("Output of Read func on is ")


print(file1.read())
print()

# seek(n) takes the file handle to the nth


# byte from the beginning.
file1.seek(0)

print( "Output of Readline func on is ")


print(file1.readline())
print()

file1.seek(0)

# To show difference between read and readline


print("Output of Read(9) func on is ")
print(file1.read(9))
print()
file1.seek(0)
print("Output of Readline(9) func on is ")
print(file1.readline(9))
file1.seek(0)

print("Output of Readlines func on is ")


print(file1.readlines())
print()
file1.close()

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

# python object(dic onary) to be dumped

dict1 ={ ('addresss', 'street'):'Brigade road', }

# the json file where the output must be stored

out_file = open("myfile.json", "w")

json.dump(dict1, out_file, indent = 6)

out_file.close()

OUTPUT :
13)Write a Python code using load()

CODE:
# Python program to read

# json file

import json

# Opening JSON file

f = open('data.json',)

# returns JSON object as

# a dic onary

data=json.load(f)

# Itera ng through the json

#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

rows = [ ['Nikhil', 'COE', '2', '9.0'],

['Sanchit', 'COE', '2', '9.1'],


['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]

# name of csv file

filename = "university_records.csv"

# wri ng to csv file with open(filename, 'w') as csvfile:

# crea ng a csv writer object

csvwriter = csv.writer (csvfile) OUTPUT:


#wri ng the fields

csvwriter.writerow(fields)

# wri ng the data rows

csvwriter.writerows(rows)
15)Write a Python code using reader()

CODE:
import csv

with open('people.csv', 'r') as file:

reader = csv.reader(file) for row in reader:

print(row)

OUTPUT :

You might also like