0% found this document useful (0 votes)
13 views15 pages

Python Revathi 1

The document outlines multiple Python programs with their aims, algorithms, and code implementations. These programs include temperature conversion, star pattern printing, student marks calculation, area calculation for various shapes, prime number printing, factorial calculation, even and odd number counting, string reversal, file line copying, and Turtle graphics window creation. Each program is designed to demonstrate specific programming concepts and techniques.

Uploaded by

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

Python Revathi 1

The document outlines multiple Python programs with their aims, algorithms, and code implementations. These programs include temperature conversion, star pattern printing, student marks calculation, area calculation for various shapes, prime number printing, factorial calculation, even and odd number counting, string reversal, file line copying, and Turtle graphics window creation. Each program is designed to demonstrate specific programming concepts and techniques.

Uploaded by

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

1.

Program to convert temperature between Fahrenheit and Celsius


Aim
To write a Python program to convert the given temperature from Fahrenheit to Celsius and
vice versa depending upon the user’s choice.

Algorithm
1. Start the program.
2. Display the menu:
o Press 1 for Fahrenheit to Celsius
o Press 2 for Celsius to Fahrenheit
3. Read the user’s choice.
4. If the choice is 1:
o Accept temperature in Fahrenheit.
o Convert it to Celsius using the formula:
C=(F−32)×59C = \frac{(F - 32) \times 5}{9}C=9(F−32)×5
o Display the Celsius value.
5. Else if the choice is 2:
o Accept temperature in Celsius.
o Convert it to Fahrenheit using the formula:
F=(C×95)+32F = (C \times \frac{9}{5}) + 32F=(C×59)+32
o Display the Fahrenheit value.
6. Else display "Invalid Choice".
7. Stop the program.
Coding
print("Temperature Conversion Program")
print("1. Fahrenheit to Celsius")
print("2. Celsius to Fahrenheit")
choice = int(input("Enter your choice (1 or 2): "))
if choice == 1:
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5 / 9
print(f"{fahrenheit}°F is equal to {celsius:.2f}°C")
elif choice == 2:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9 / 5) + 32
print(f"{celsius}°C is equal to {fahrenheit:.2f}°F")
else:
print("Invalid choice! Please enter 1 or 2.")
2. Program to print a star pattern using nested loops
Aim
To write a Python program to construct a diamond-shaped star pattern using nested loops.

Algorithm
1. Start the program.
2. Read/set the number of rows (say n).
3. For the upper half of the diamond:
o Loop i from 1 to n.
o Print (n-i) spaces.
o Print (2*i-1) stars.
4. For the lower half of the diamond:
o Loop i from n-1 down to 1.
o Print (n-i) spaces.
o Print (2*i-1) stars.
5. Stop the program.

Python Code
rows = 5 # number of rows in the upper half
# Upper part
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
# Lower part
for i in range(rows - 1, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))
3. Program to calculate total marks, percentage, and grade of a student. Marks
obtained in each of the five subjects are to be input by user. Assign grades
according to the given criteria."
Aim
To write a Python program to calculate the total marks, percentage, and grade of a student
based on marks in five subjects.

Algorithm
1. Start the program.
2. Input marks for five subjects.
3. Calculate total marks = sum of marks in all five subjects.
4. Calculate percentage = (total marks / maximum marks) × 100.
o Assume each subject is out of 100, so maximum marks = 500.
5. Determine the grade using conditions:
o If percentage ≥ 80 → Grade A
o Else if percentage ≥ 70 → Grade B
o Else if percentage ≥ 60 → Grade C
o Else if percentage ≥ 40 → Grade D
o Else → Fail
6. Display total marks, percentage, and grade.
7. Stop the program.

Python Code
marks = []
for i in range(1, 6):
mark = float(input(f"Enter marks for subject {i}: "))
[Link](mark)
# Calculate total and percentage
total = sum(marks)
percentage = (total / 500) * 100
# Determine grade
if percentage >= 80:
grade = "A"
elif percentage >= 70:
grade = "B"
elif percentage >= 60:
grade = "C"
elif percentage >= 40:
grade = "D"
else:
grade = "Fail"
# Display results
print("\n--- Result ---")
print(f"Total Marks = {total}")
print(f"Percentage = {percentage:.2f}%")
print(f"Grade = {grade}")
4. Program to find the area of rectangle, square, circle, and triangle by accepting suitable
input parameters from user.

Aim
To write a Python program to calculate the area of a rectangle, square, circle, and triangle by
accepting input from the user.

Algorithm
1. Start the program.
2. Display a menu with choices:
o 1 → Rectangle
o 2 → Square
o 3 → Circle
o 4 → Triangle
3. Read the user’s choice.
4. If the choice is Rectangle:
o Input length and breadth.
o Compute area = length × breadth.
5. If the choice is Square:
o Input side.
o Compute area = side × side.
6. If the choice is Circle:
o Input radius.
o Compute area = 3.14 × radius × radius.
7. If the choice is Triangle:
o Input base and height.
o Compute area = 0.5 × base × height.
8. Display the result.
9. Stop the program.
Python Code
# Program to calculate area of rectangle, square, circle and triangle

print("Area Calculation Program")


print("1. Rectangle")
print("2. Square")
print("3. Circle")
print("4. Triangle")

choice = int(input("Enter your choice (1-4): "))

if choice == 1:
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
area = length * breadth
print(f"Area of Rectangle = {area}")

elif choice == 2:
side = float(input("Enter side: "))
area = side * side
print(f"Area of Square = {area}")

elif choice == 3:
radius = float(input("Enter radius: "))
area = 3.14 * radius * radius
print(f"Area of Circle = {area}")

elif choice == 4:
base = float(input("Enter base: "))
height = float(input("Enter height: "))
area = 0.5 * base * height
print(f"Area of Triangle = {area}")

else:
print("Invalid choice! Please enter 1-4.")
[Link] a Python script that prints prime numbers less than 20.
Aim
To write a Python program that prints all prime numbers less than 20.

Algorithm
1. Start the program.
2. Loop through numbers from 2 to 19 (since primes start from 2).
3. For each number, check if it is divisible by any number from 2 to (n-1).
o If divisible → not prime.
o If not divisible → prime.
4. Print the prime numbers.
5. Stop the program.

Python Code
# Program to print prime numbers less than 20

print("Prime numbers less than 20 are:")

for num in range(2, 20): # numbers from 2 to 19


is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, end=" ")
6. Program to find factorial of the given number using recursive function.

Aim
To write a Python program to find the factorial of a given number using recursion.

Algorithm
1. Start the program.
2. Define a recursive function factorial(n):
o If n == 0 or n == 1, return 1 (base case).
o Else return n * factorial(n-1).
3. Read the number n from the user.
4. Call the recursive function factorial(n).
5. Print the result.
6. Stop the program.

Python Code

def factorial(n):
if n == 0 or n == 1: # base case
return 1
else:
return n * factorial(n - 1) # recursive call

# Input from user


num = int(input("Enter a number: "))

# Check for negative numbers


if num < 0:
print("Factorial is not defined for negative numbers.")
else:
print(f"Factorial of {num} = {factorial(num)}")
7. Program to count even and odd numbers in an array
Aim
To write a Python program to count the number of even and odd numbers from an array of N
numbers.
Algorithm

1. Start the program.


2. Input the number of elements (N).
3. Read N elements and store them in an array.
4. Initialize two counters: even_count = 0 and odd_count = 0.
5. Traverse each element in the array:
o If the element is divisible by 2, increment even_count.
o Else, increment odd_count.
6. Display the values of even_count and odd_count.
7. Stop the program.

Python Code

# Program to count even and odd numbers in an array


# Step 1: Input the number of elements
N = int(input("Enter the number of elements in the array: "))

# Step 2: Input the array elements


arr = [ ]
print("Enter the elements:")
for i in range(N):
[Link](int(input( )))

# Step 3: Initialize counters


even_count = 0
odd_count = 0

# Step 4: Traverse the array and count


for num in arr:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1

# Step 5: Display the result


print("Number of even numbers:", even_count)
print("Number of odd numbers:", odd_count)
Output
Enter the number of elements in the array: 6
Enter the elements:
10
21
32
43
54
65
Number of even numbers: 3
Number of odd numbers: 3
8. Python class to reverse a string word by word
Aim
To write a Python class to reverse a string word by word.
Algorithm

1. Start the program.


2. Define a class StringReverse with:
o A method reverse_words(self, text) that:
 Splits the string into words using split().
 Reverses the list of words.
 Joins them back into a string with spaces using join().
 Returns the reversed string.
3. Create an object of the class.
4. Take input from the user as a string.
5. Call the method to reverse the string word by word.
6. Display the result.
7. End the program.

Python Code
# Python class to reverse a string word by word

class StringReverse:
def reverse_words(self, text):
# Split the string into words, reverse the list, and join back
words = [Link]()
reversed_words = words[::-1]
return " ".join(reversed_words)

# Main program
string_input = input("Enter a string: ")
obj = StringReverse()
result = obj.reverse_words(string_input)

print("Reversed string word by word:", result)


Output
Enter a string: Python is very easy
Reversed string word by word: easy very is Python
9. Program to copy odd lines of one file into another

Aim
To write a Python program to read a file content and copy only the contents at odd lines into a
new file.
Algorithm

1. Start the program.


2. Open the input file in read mode.
3. Open another file (output file) in write mode.
4. Read all lines from the input file using readlines().
5. Traverse the list of lines with their line numbers.
o If the line number is odd, write it into the new file.
6. Close both input and output files.
7. Display a success message.
8. End the program.

Python Code

# Program tos copy odd lines of one file into another


# Step 1: Open input file in read mode
with open("[Link]", "r") as infile:
lines = [Link]()
# Step 2: Open output file in write mode
with open("[Link]", "w") as outfile:
# Step 3: Write only odd-numbered lines (1, 3, 5, ...)
for i in range(len(lines)):
if (i + 1) % 2 != 0: # odd line number
[Link](lines[i])
Output
Line 1: Python
Line 2: Java
Line 3: C++
Line 4: HTML
Line 5: JavaScript
10. Program to create a Turtle graphics window with specific size
Aim
To write a Python program to create a Turtle graphics window with a specific size.
Algorithm

1. Start the program.


2. Import the turtle module.
3. Create a screen object using [Link]().
4. Set the title of the window using [Link]().
5. Set the size of the window using [Link](width, height).
6. (Optional) Choose background color using [Link]().
7. Keep the window open using [Link]().
8. End the program.

Python Program
# Program to create a Turtle graphics window with specific size

import turtle

# Step 1: Create screen object


screen = [Link]()

# Step 2: Set window title


[Link]("My Turtle Graphics Window")

# Step 3: Set window size (width=600, height=400)


[Link](width=600, height=400)

# Step 4: Set background color (optional)


[Link]("lightblue")

# Step 5: Finish
[Link]()
Output
🔹 A Turtle graphics window will open with:

 Title → "My Turtle Graphics Window"


 Width → 600 pixels
 Height → 400 pixels
 Background color → Light Blue

You might also like