Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Python Lab 2024

lab programms

Uploaded by

velu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Python Lab 2024

lab programms

Uploaded by

velu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

PROGRAM USING VARIABLES, CONSTANTS, I/O STATEMENTS IN PYTHON

AIM:

The aim of the program is to calculate the area of a circle using a constant value for pi and user

input for the radius.

ALGORITHM:

Step 1: Initialize a constant variable PI with the value 3.14159.

Step 2: Prompt the user to enter the radius of the circle.

Step 3: Read the input value and store it in a variable radius.

Step 4: Calculate the area of the circle using the formula area = PI * radius * radius.

Step 5: Display the calculated area of the circle.

CODING:

PI = 3.14159

radius = float(input("Enter the radius of the circle:"))

area = PI * radius * radius

print("The area of the circle is:", area)

OUTPUT:

Enter the radius of the circle: 5

The area of the circle is: 78.53975


2. PROGRAM USING OPERATORS IN PYTHON

AIM:

The aim of the program is to perform basic arithmetic operations using operators in Python.

ALGORITHM:

Step 1: Prompt the user to enter two numbers.


Step 2: Read the input values and store them in variables num1 and num2.
Step 3:Perform the following operations:
Addition: result = num1 + num2
Subtraction: result = num1 - num2
Multiplication: result = num1 * num2
Division: result = num1 / num2
Modulo: result = num1 % num2
Exponentiation: result = num1 ** num2
Step 4: Display the results of each operation.
CODING:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = num1 + num2
print("Addition:", result)
result = num1 - num2
print("Subtraction:", result)
result = num1 * num2
print("Multiplication:",result)
result = num1 / num2
print("Division:", result)
result = num1 % num2
print("Modulo:", result)
result = num1 ** num2
print("Exponentiation:", result)

OUTPUT:
Enter the first number: 10
Enter the second number: 3
Addition: 13.0
Subtraction: 7.0
Multiplication: 30.0
Division: 3.3333333333333335
Modulo: 1.0
Exponentiation: 1000.0
3. PROGRAM USING CONDITIONAL STATEMENTS.

AIM:
The aim of the program is to determine whether a given number is positive, negative, or zero
using conditional statements in Python.

ALGORITHM:
Step 1: Prompt the user to enter a number.
Step 2: Read the input value and store it in a variable num.
Step 3: Use conditional statements to check the value of
num: Step 4: If num is greater than 0, display "Positive
number".
Step 5: If num is less than 0, display "Negative number".
Step 6: If num is equal to 0, display "Zero".
Step 7: End the program.

CODING:
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")

OUTPUT:
Enter a number: 5
Positive number
Enter a number: -2
Negative number
Enter a number: 0
4. PROGRAM USING LOOPS.

AIM:
The aim of the program is to display the numbers from 1 to 10 using a loop in Python.

ALGORITHM:
Step 1: Initialize a variable num with the value 1.
Step 2: Use a loop to iterate from num to 10 (inclusive).
Step 3: Print the value of num.
Step 4: Increment num by 1.
Step 5: End the loop.

CODING:
num = 1
while num <=10:
print(num)
num += 1

OUTPUT:
1
2
3
4
5
6
7
8
9
10
5. PROGRAM USING JUMP STATEMENTS.

AIM:
The aim of the program is to demonstrate the use of jumping statements (break and
continue) in Python.

ALGORITHM:
Step 1: Use a loop to iterate through the numbers from 1 to 10 (inclusive).
Step 2: Inside the loop, check if the current number is divisible by 3.
Step 3: If the number is divisible by 3, use the continue statement to skip the
remaining code in the loop and move to the next iteration.
Step 4: If the number is not divisible by 3, print the number.
Step 5: Check if the current number is equal to 7.
Step 6: If the number is equal to 7, use the break statement to exit the loop.
Step 7: End the loop.

CODING:
for num in range(1, 11):
if num % 3 == 0:
continue
print(num)
if num ==7:
break

OUTPUT:
1
2
4
5
6
6. PROGRAM USING FUNCTIONS.

AIM:
The aim of the program is to create a function that adds two numbers in Python.

ALGORITHM:
Step 1: Define a function named add_numbers that takes two parameters num1 and num2.
Step 2: Inside the add_numbers function, add num1 and num2 and store the result in a
variable sum.
Step 3: Return the value of sum.
Step 4: Prompt the user to enter two numbers.
Step 5: Read the input values and store them in variables n1 and n2.
Step 6: Call the add_numbers function, passing n1 and n2 as arguments, and store the
result in a variable result.
Step 7:Display the sum of the two numbers.

CODING:
def add_numbers(num1, num2):
sum = num1 + num2
return sum
n1 = float(input("Enter the first number: "))
n2 = float(input("Enter the second number: "))
result = add_numbers(n1, n2)
print("Sum:", result)

OUTPUT:

Enter the first number: 5


Enter the second number: 3
Sum: 8.0
7. PROGRAM USING RECURSION.

AIM:
The aim of the program is to create a function that calculates the factorial of a given
number in Python.

ALGORITHM:
Step 1: Define a function named factorial that takes a single parameter num.
Step 2: Inside the factorial function, initialize a variable result with the value 1.
Step 3: Use a for loop to iterate from 1 to num (inclusive).
Step 4: Multiply result by the current value of the loop variable.
Step 5: Return the value of result.
Step 6: Prompt the user to enter a number.
Step 7: Read the input value and store it in a variable n.
Step 8: Call the factorial function, passing n as an argument, and store the result in a
variable fact.
Step 9: Display the calculated factorial value.

CODING:
def factorial(num):
result = 1
for i in range(1,num+1):
result *= i
return result
n = int(input("Enter a number: "))
fact = factorial(n)
print("Factorial of", n, "is", fact)

OUTPUT:

Enter a number: 5
Factorial of 5 is 120
8. PROGRAM USING ARRAYS.

AIM:
The aim of the program is to demonstrate the use of arrays in Python by storing and
manipulating a list of numbers.

ALGORITHM:
Step 1: Create an array/list to store numbers.
Step 2: Prompt the user to enter the size of the array.
Step 3: Read the input value and store it in a variable size.
Step 4: Use a loop to iterate size number of times.
Step 5: Inside the loop, prompt the user to enter a number and append it to the array.
Step 6: Calculate the sum and average of the numbers in the array.
Step 7: Initialize variables sum and average with the value 0.
Step 8: Use a loop to iterate through each element of the array.
Step 9: Add each element to the sum variable.
Step 10: Divide the sum by the size to calculate the average.
Step 11: Display the array, sum, and average of the numbers.

CODING:

size = int(input("Enter the size of the array: "))


numbers = []
for i in range(size):
num = float(input("Enter a number: "))
numbers.append(num)
sum = 0
for num in numbers:
sum += num
average = sum / size
print("Array:", numbers)
print("Sum:", sum)
print("Average:", average)
OUTPUT:

Enter the size of the array:


4 Enter a number: 5
Enter a number: 2
Enter a number: 8
Enter a number: 3
Array: [5.0, 2.0, 8.0, 3.0]
Sum: 18.0
Average: 4.5
9. PROGRAM USING STRINGS.

AIM:
The aim of the program is to demonstrate various operations and manipulations on
strings in Python.
ALGORITHM:
Step 1: Prompt the user to enter a sentence.
Step 2: Read the input value and store it in a variable sentence.
Step 3: Calculate the length of the sentence using the len() function and store it in a variable
length.
Step 4: Split the sentence into words using the split() method and store the resulting list of
words in a variable words.
Step 5: Count the number of words in the sentence by calculating the length of the words
list.
Step 6: Replace a specific word in the sentence with a new word.
Step 7: Prompt the user to enter the word to be replaced and the new word.
Step 8: Use the replace() method to replace the word in the sentence and store the modified
sentence in a variable modified_sentence.
Step 9: Display the length of the sentence, the number of words, and the modified sentence.

CODING:
sentence = input("Enter a sentence: ")
length = len(sentence)
words = sentence.split()
word_count = len(words)
word_to_replace = input("Enter the word to replace:")
new_word = input("Enter the new word: ")
modified_sentence = sentence.replace(word_to_replace, new_word)
print("Length of the sentence:", length)
print("Number of words:", word_count)
print("Modified sentence:", modified_sentence)

OUTPUT:
Enter a sentence: Hello, how are you?
Enter the word to replace: how
Enter the new word: is
Length of the sentence: 19
Number of words: 4
Modified sentence: Hello, is are you?
10. PROGRAM USING MODULES.

AIM:
The aim of the program is to demonstrate the usage of modules in Python by importing
and utilizing functions from a separate module.
ALGORITHM:

Step 1: Import the required module that contains the desired functions.
Step 2: Prompt the user to enter two numbers.
Step 3: Read the input values and store them in variables num1 and num2.
Step 4: Call the imported function(s) from the module to perform desired operations on
the input numbers.
Step 5: Display the output/result of the function(s).

CODING:
import math
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
sqrt = math.sqrt(num1)
power = math.pow(num1, num2)
print("Square root of", num1, "is", sqrt)
print(num1, "raised to the power of", num2, "is", power)

OUTPUT:

Enter the first number: 9


Enter the second number: 2
Square root of 9.0 is 3.0
9.0 raised to the power of 2.0 is 81.0
11. ROGRAM USING LISTS.

AIM:
The aim of the program is to demonstrate the usage of lists in Python by performing
various operations on a list of numbers.
ALGORITHM:
Step 1: Create an empty list to store numbers.
Step 2: Prompt the user to enter the size of the list.
Step 3: Read the input value and store it in a variable size.
Step 4: Use a loop to iterate size number of times.
Step 5: Inside the loop, prompt the user to enter a number and append it to the list.
Step 6: Calculate the sum and average of the numbers in the list.
Step 7: Initialize variables sum and average with the value 0.
Step 8: Use a loop to iterate through each element of the list.
Step 9: Add each element to the sum variable.
Step 10: Divide the sum by the size to calculate the average.
Step 11: Find the maximum and minimum values in the list using the max() and min()
functions.
Step 12: Display the list, sum, average, maximum, and minimum values.

CODING:
size = int(input("Enter the size of the list:"))
numbers = []
for i in range(size):
num = float(input("Enter a number: "))
numbers.append(num)
sum = sum(numbers)
average = sum / size
maximum = max(numbers)
minimum = min(numbers)
print("List:", numbers)
print("Sum:", sum)
print("Average:", average)
print("Maximum:", maximum)
print("Minimum:", minimum)
OUTPUT:
Enter the size of the list: 5
Enter a number: 7
Enter a number: 4
Enter a number: 9
Enter a number: 2
Enter a number: 5
List: [7.0, 4.0, 9.0, 2.0, 5.0]
Sum: 27.0
Average: 5.4
Maximum: 9.0
Minimum: 2.0
12. PROGRAM USING TUPLES.

AIM:
The aim of the program is to demonstrate the usage of tuples in Python by performing
operations and accessing elements of a tuple.
ALGORITHM:
Step 1: Create a tuple to store a collection of elements.
Step 2: Prompt the user to enter values for the tuple.
Step 3: Read the input values and store them in variables.
Step 4: Perform operations on the tuple:
Step 5: Calculate the length of the tuple using the len() function.
Step 6: Access individual elements of the tuple using indexing.
Step 7: Slice the tuple to extract a subset of elements.
Step 8: Concatenate two tuples using the + operator.
Step 9: Display the length of the tuple, individual elements, sliced elements, and the
concatenated tuple.

CODING:
value1 = input("Enter the first value: ")
value2 = input("Enter the second value: ")
value3 = input("Enter the third value: ")
my_tuple = (value1, value2, value3)
length = len(my_tuple)
element1 = my_tuple[0]
sliced_tuple = my_tuple[1:3]
concatenated_tuple = my_tuple + ('extra',)
print("Length of the tuple:", length)
print("First element:", element1)
print("Sliced tuple:", sliced_tuple)
print("Concatenated tuple:", concatenated_tuple)
OUTPUT:

Enter the first value: Apple


Enter the second value: Banana
Enter the third value: Orange
Length of the tuple: 3
First element: Apple
Sliced tuple: ('Banana', 'Orange')
Concatenated tuple: ('Apple', 'Banana', 'Orange', 'extra')
13. PROGRAM USING DICTIONARIES.

AIM:
The aim of the program is to demonstrate a simple usage of dictionaries in Python by
storing and accessing information about a person.

ALGORITHM:

Step 1: Create an empty dictionary to store information.


Step 2: Prompt the user to enter the name, age, and city of a person.
Step 3: Read the input values and store them in variables.
Step 4: Add the information to the dictionary using appropriate keys.
Step 5: Access the information from the dictionary using the keys.
Step 6: Display the stored information.

CODING:

person = {}
person['name'] = input("Enter the name: ")
person['age'] = input("Enter the age: ")
person['city'] = input("Enter the city: ")
print("Name:", person['name'])
print("Age:", person['age'])
print("City:", person['city'])

OUTPUT:

Enter the name: velu


Enter the age: 25
Enter the city: salem
Name: velu
Age: 25
City: salem
14. PROGRAM FOR FILE HANDLING.

AIM:
The aim of the program is to demonstrate file handling in Python by reading and writing
data to a file.
ALGORITHM:

Step 1: Prompt the user to enter a file name.


Step 2: Open the file in write mode and create a file object.
Step 3: Prompt the user to enter data to be written to the
file. Step 4: Write the data to the file using the file object.
Step 5: Close the file.
Step 6: Open the file in read mode and create a new file object.
Step 7: Read the contents of the file using the file object.
Step 8: Display the contents of the file.
Step 9: Close the file.

CODING:

filename = input("Enter the file name: ")


file = open(filename, 'w')
data = input("Enter data to be written to the file:")
file.write(data)
file.close()
file = open(filename, 'r')
contents = file.read()
file.close()
print("Contents of the file:")
print(contents)

OUTPUT:

Enter the file name: my_file.txt


Enter data to be written to the file: Hello, this is a file handling program.
Contents of the file:
Hello, this is a file handling program.

You might also like