Python Programming
Python Programming
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 4: Calculate the area of the circle using the formula area = PI * radius * radius.
CODING:
PI = 3.14159
OUTPUT:
AIM:
The aim of the program is to perform basic arithmetic operations using operators in
Python.
ALGORITHM:
Step 2: Read the input values and store them in variables num1 and num2.
CODING:
print("Addition:", result)
print("Subtraction:", result)
print("Multiplication:",
print("Division:", result)
print("Modulo:", result)
print("Exponentiation:", result)
lOMoARcPSD|27175953
OUTPUT:
Addition: 13.0
Subtraction: 7.0
Multiplication: 30.0
Division: 3.3333333333333335
Modulo: 1.0
Exponentiation: 1000.0
lOMoARcPSD|27175953
AIM:
The aim of the program is to determine whether a given number is positive, negative, or
zero using conditional statements in Python.
ALGORITHM:
number".
CODING:
if num > 0:
print("Positive number")
print("Negative number")
else:
print("Zero")
OUTPUT:
Enter a number: 5
Positive number
Enter a number: -2
Negative number
Enter a number: 0
Zero
lOMoARcPSD|27175953
AIM:
The aim of the program is to display the numbers from 1 to 10 using a loop in Python.
ALGORITHM:
CODING:
num = 1
10: print(num)
num += 1
lOMoARcPSD|27175953
OUTPUT:
10
lOMoARcPSD|27175953
AIM:
The aim of the program is to demonstrate the use of jumping statements (break and
continue) in Python.
ALGORITHM:
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 6: If the number is equal to 7, use the break statement to exit the loop.
CODING:
if num % 3 == 0:
continue
print(num)
if num ==
7:
break
OUTPUT:
6
lOMoARcPSD|27175953
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 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.
CODING:
return sum
print("Sum:", result)
OUTPUT:
Sum: 8.0
lOMoARcPSD|27175953
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 8: Call the factorial function, passing n as an argument, and store the result in a variable
fact.
CODING:
def factorial(num):
result = 1
for i in range(1,
num+1): result *= i
return result
fact = factorial(n)
OUTPUT:
Enter a number: 5
Factorial of 5 is
120
lOMoARcPSD|27175953
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 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 10: Divide the sum by the size to calculate the average.
Step 11: Display the array, sum, and average of the numbers.
CODING:
")) numbers = []
lOMoARcPSD|27175953
for i in range(size):
numbers.append(num)
sum = 0
sum += num
print("Array:", numbers)
print("Sum:", sum)
print("Average:", average)
OUTPUT:
4 Enter a number: 5
Enter a number: 2
Enter a number: 8
Enter a number: 3
Sum: 18.0
Average: 4.5
lOMoARcPSD|27175953
AIM:
The aim of the program is to demonstrate various operations and manipulations on strings in
Python.
ALGORITHM:
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 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
Step 9: Display the length of the sentence, the number of words, and the modified sentence.
lOMoARcPSD|27175953
CODING:
length = len(sentence)
words = sentence.split()
word_count = len(words)
print("Modified sentence:",
modified_sentence)
OUTPUT:
Number of words: 4
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 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.
CODING:
import math
sqrt = math.sqrt(num1)
OUTPUT:
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 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 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.
lOMoARcPSD|27175953
CODING:
")) numbers = []
for i in range(size):
numbers.append(num)
sum = sum(numbers)
maximum = max(numbers)
minimum = min(numbers)
print("List:", numbers)
print("Sum:", sum)
print("Average:", average)
print("Maximum:", maximum)
print("Minimum:", minimum)
lOMoARcPSD|27175953
OUTPUT:
Enter a number: 7
Enter a number: 4
Enter a number: 9
Enter a number: 2
Enter a number: 5
Sum: 27.0
Average: 5.4
Maximum: 9.0
Minimum: 2.0
lOMoARcPSD|27175953
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 5: Calculate the length of the tuple using the len() function.
Step 9: Display the length of the tuple, individual elements, sliced elements, and the
concatenated tuple.
lOMoARcPSD|27175953
CODING:
length = len(my_tuple)
element1 = my_tuple[0]
sliced_tuple = my_tuple[1:3]
OUTPUT:
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 2: Prompt the user to enter the name, age, and city of a person.
Step 5: Access the information from the dictionary using the keys.
CODING:
person = {}
print("Name:", person['name'])
print("Age:", person['age'])
print("City:", person['city'])
lOMoARcPSD|27175953
OUTPUT:
Age: 25
City: London
lOMoARcPSD|27175953
AIM:
The aim of the program is to demonstrate file handling in Python by reading and writing
data to a file.
ALGORITHM:
Step 2: Open the file in write mode and create a file object.
file. Step 4: Write the data to the file using the file object.
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.
CODING:
") file.write(data)
file.close()
contents = file.read()
file.close()
print("Contents of the
file:") print(contents)
OUTPUT:
Enter data to be written to the file: Hello, this is a file handling program.