PYTHON PROGRAMMING - LAB EXERCISES (23UCSCCP01)
1.
Program using variables, constants, I/O statements in Python
Aim:
To write a Python program using variables, constants, input, and output statements.
Algorithm:
1. Start the program
2. Define a constant PI
3. Get the radius of the circle
4. Calculate the area using the formula:
area = PI *radius * radius
5. Display the calculated area value
6. End the program
#Program using variables, constants,
I/O statements in Python
# Constant: Pi value (usually written in uppercase)
PI = 3.14159
# Input: Read radius from user
radius = float(input("Enter the radius of the circle (in meters): "))
# Variable: Calculate area using formula area = PI * radius^2
area = PI * radius * radius
# Output: Display the result
print("The area of the circle with radius", radius, "meters is:", area, "square meters")
2.
Program using Operators in Python
Aim:
To write a Python program that performs arithmetic, relational, and logical operations on two numbers and displays the results.
Algorithm:
1. Start the program
2. Read the first number (num1)
3. Read the second number (num2)
4. Perform arithmetic operations:
Addition: num1 + num2
Subtraction: num1 - num2
Multiplication: num1 * num2
Division: num1 / num2
5. Perform relational operation:
Check if the two numbers are equal: (num1 == num2)
6. Perform logical operation:
Check if both numbers are positive: (num1 > 0) and (num2 > 0)
7. Display the results of all operations
8. End the program
# Program using Operators in
Python.
# Input two numbers from user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Arithmetic Operations
add = num1 + num2
sub = num1 - num2
mul = num1 * num2
div = num1 / num2
# Relational Operation
is_equal = (num1 == num2)
# Logical Operation
is_positive = (num1 > 0) and (num2 > 0)
# Output the results
print("Addition:", add)
print("Subtraction:", sub)
print("Multiplication:", mul)
print("Division:", div)
print("Are both numbers equal?", is_equal)
print("Are both numbers positive?", is_ positive)
3.
Program using Conditional Statements
Aim:
To write a Python program that checks whether the number is positive, negative, or zero using conditional statements.
Algorithm:
1. Start the program
2. Read the number (num) from the user
3. Use conditional statements to check:
If the num > 0, print "The number is positive."
Else if the num < 0, print "The number is negative."
Else, print "The number is zero."
4. End the program
# Program using Conditional
Statements.
# Program to check whether a number is positive, negative, or zero
# Input: Read number from user
num= int(input("Enter a number: "))
# Conditional Statements
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
4.
Program using Loops
Aim:
To write a Python program that calculates the sum and average of n numbers using a loop.
Algorithm:
1. Start the program
2. Read the value of n
3. Initialize sum as 0
4. for loop from 1 to n:
read a number from the user.
Add the number to sum.
5. Calculate : average = sum / n
6. Display the sum and average
7. End the program
# Program using Loops.
# Program to calculate sum and average of n numbers using a loop
n = int(input("Enter how many numbers you want to add: "))
sum = 0
for i in range(n):
number = float(input(f"Enter number {i+1}: "))
sum += number
average = sum / n
print("Sum of the numbers is:", sum)
print("Average of the numbers is:", average)
5.
Program using Jump Statements
Aim:
To write a Python program that prints numbers from 1 to 10 but skips number 5 using continue and stops the loop completely if the number becomes 8 using break.
Algorithm:
1. Start the program.
2. for loop to iterate numbers from 1 to 10.
3. In each iteration:
If the number is 5, use continue to skip printing number 5.
If the number is 8, use break to exit the loop completely.
Otherwise, print the number.
4. End the program.
# Program using Jump Statements
(break and continue)
for i in range(1, 11):
if i == 5:
continue # Skip number 5
if i == 8:
break # Stop the loop when i is 8
print(i)
6.
Program using Functions
Aim:
To write a Python program that uses a function to calculate the factorial of a number .
Algorithm:
1. Start the program
2. Define a function factorial(n):
Initialize fact = 1.
Use a loop from 1 to n to multiply fact by each number
Return the value of fact
3. Get a number (n) from the user
4. Call the function factorial(n)
5. Print the result (fact).
6. End the program
# Function to calculate
factorial
def factorial(n):
fact = 1
for i in range(1, n + 1):
fact *= i
return fact
# Main program
num = int(input("Enter a number to find its factorial: "))
result = factorial(num)
print(f"The factorial of {num} is: {result}")
7.
Program using Recursion
Aim:
To write a Python program that calculates the factorial of a number using recursion.
Algorithm:
1. Start the program
2. Define a recursive function factorial(n):
Base case: If n == 0 or n == 1, return 1.
Recursive case: Return n*factorial(n - 1)
3. Read an integer number from the user
4. Call the recursive function factorial(n)
5. Print the result (factorial of the number)
6. End the program
# Recursive function to
calculate factorial
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Main program
num = int(input("Enter a number to find its factorial using recursion: "))
result = factorial(num)
print(f"The factorial of {num} is: {result}")
8.
Program using Arrays (Lists)
Aim:
To write a Python program that stores n numbers in an array and finds the sum and average of those numbers.
Algorithm:
1. Start the program.
2. Create an empty list numbers = [].
3. Read the value of n
4. for loop from 1 to n:
Get a number
Append the number to the numbers list.
5. Calculate the sum using the built-in sum() function.
6. Calculate the average as sum / n.
7. Display the array, sum, and average.
8. End the program.
# Program to store n numbers in
an array and calculate sum and average
# Step 1: Create an empty list
numbers = []
# Step 2: Input how many numbers to store
n = int(input("Enter the number of elements: "))
# Step 3: Read numbers from user and store in the array
for i in range(n):
num = float(input(f"Enter number {i + 1}: "))
numbers.append(num)
# Step 4: Calculate sum and average
total = sum(numbers)
average = total / n
# Step 5: Display results
print("\nArray of numbers:", numbers)
print("Sum of numbers:", total)
print("Average of numbers:", average)
9.
Python Program using Strings
Aim:
To write a Python program that takes a string input from the user and performs the following string operations.
Algorithm:
1. Start the program.
2. Read a string input from the user.
3. Calculate and display the length of the string using len().
4. Convert and display the string in uppercase using .upper().
5. Reverse and display the string using slicing ([::-1]).
6. Initialize a vowel counter to 0.
7. Loop through each character of the string:
If the character is a vowel (a, e, i, o, u), increment the counter.
8. Display the total number of vowels.
9. End the program.
# Program to perform string
operations
# Step 1: Read string from user
text = input("Enter a string: ")
# Step 2: Length of string
len = len(text)
# Step 3: Convert to uppercase
caps = text.upper()
# Step 4: Reverse the string
rev = text[::-1]
# Step 5: Count vowels
vowels = 'aeiouAEIOU'
vc = 0
for char in text:
if char in vowels:
vc += 1
# Step 6: Display results
print("\nLength of the string:", len)
print("Uppercase string:", caps)
print("Reversed string:", rev)
print("Number of vowels:", vc)
10.
Program using Modules
Aim:
To write a Python program that uses the built-in math module to calculate and display the square root, power, and factorial of a number.
Algorithm:
1. Start the program.
2. Import the math module.
3. Read a number from the user.
4. Calculate the square root using math.sqrt().
5. Calculate the power of 2 using math.pow().
6. Calculate the factorial using math.factorial().
7. Display the results.
8. End the program.
# Program demonstrating the use
of math module
import math
# Step 1: Input number from user
num = int(input("Enter a positive integer: "))
# Step 2: Calculate square root
squ = math.sqrt(num)
# Step 3: Calculate power (num^2)
power = math.pow(num, 2)
# Step 4: Calculate factorial
fact = math.factorial(num)
# Step 5: Display results
print(f"\nSquare root of {num} is: {squ }")
print(f"{num} raised to power 2 is: {power}")
print(f"Factorial of {num} is: {factl}")
Comments
Post a Comment