0% found this document useful (0 votes)
2 views4 pages

Practical Solution

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Practical Solution

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 1. Write a Python program to print Reverse a String.

def reverse_string(text):
return text[::-1]

# Example usage
input_str = "Python"
reversed_str = reverse_string(input_str)
print(reversed_str)

Program 2. Write a Python program to print Sum of List Elements.


numbers = [10, 20, 30, 40, 50]
# Initialize a variable to store the sum
total_sum = 0
# Iterate through each element in the list
for num in numbers:
total_sum += num
print("The sum of the list elements is:", total_sum)

Program 3. Write a Python program to print Frequency of an Element.


# Define the list of elements
numbers = [1, 2, 8, 3, 2, 2, 2, 5, 1]

# Specify the element you want to count


target_element = 2

# Find the frequency


frequency = [Link](target_element)

# Print the result


print(f"The element {target_element} appears {frequency} times in the list.")
Program 4. Write a Python program to print read and write data into a file.
# Writing data to a file
with open("[Link]", "w") as file:
[Link]("Hello, world!\n")
[Link]("This is a simple Python program.")

# Reading data from the file


with open("[Link]", "r") as file:
content = [Link]()
print(content)

Program 5. Write a Python program to add two numbers using print user-defined
functions.
def add_numbers(x,y):
sum = x + y
return sum
num1 = 5
num2 = 6
print("The sum is", add_numbers(num1, num2))

Program 6. Write a Python program to print sum of two numbers using lambda
functions.
# Lambda function to add two numbers
add_numbers = lambda x, y: x + y

# Example usage
num1 = 5
num2 = 3
result = add_numbers(num1, num2)
print(f"The sum of ”, num1, “ and ”, num2, “is: ”, result )

Program 7. Write a Python program to print list comprehension.


numbers = [1, 2, 3, 4]
# list comprehension to create new list
doubled_numbers = [num * 2 for num in numbers]
print(doubled_numbers)
Program 8. Write a Python program to print exception handling using try and except.
try:
# Code that might cause an error
number = int(input("Enter a number: "))
result = 10 / number
print(f"Result is {result}")

except ZeroDivisionError:
# Handles division by zero
print("Error: You cannot divide by zero.")

except ValueError:
# Handles non-numeric inputs
print("Error: Please enter a valid whole number.")

Program 9. Write a Python program to use file handling for write data to a file and read
it.
#Reading a CSV File in Python

import csv
# Opening the CSV file
with open('[Link]', mode='r') as file:
# Reading the CSV file
csvFile = [Link](file)

# Skipping the header (uncomment if needed)


# next(csvFile)

# Displaying the contents of the CSV file


for line in csvFile:
print(line)
# Writing to a csv file in python
# Field names
f = ['Name', 'Branch', 'Year', 'CGPA']

# Data rows of CSV file


r=[
['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 the CSV file


fn = "university_records.csv"

# Writing to CSV file


with open(fn, 'w', newline='') as csvfile:
# Creating a CSV writer object
csvwriter = [Link](csvfile)
[Link](f) # Writing the field names
[Link](r) # Writing the data rows

Program 10. Write a Python program to create a Pandas Series using a list of numbers
and display it.

import pandas as pd
data_list = [10, 20, 30, 40, 50] # Create a list of numbers

# Create a Pandas Series from the list


number_series = [Link](data_list)[0] # Or simply: [Link](data_list)

print(number_series) # Display the Series

You might also like