0% found this document useful (0 votes)
42 views19 pages

Python Prms Print (I Bcom CA)

The document contains multiple Python programs for various tasks, including temperature conversion, pattern construction, calculating student grades, area calculations for different shapes, finding prime numbers, calculating factorials, counting even and odd numbers, reversing strings, copying odd lines from a file, and creating a turtle graphics window. Each section provides code snippets along with example outputs demonstrating the functionality of the programs. The programs cover fundamental programming concepts and user interactions in Python.

Uploaded by

amaravathi9566
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)
42 views19 pages

Python Prms Print (I Bcom CA)

The document contains multiple Python programs for various tasks, including temperature conversion, pattern construction, calculating student grades, area calculations for different shapes, finding prime numbers, calculating factorials, counting even and odd numbers, reversing strings, copying odd lines from a file, and creating a turtle graphics window. Each section provides code snippets along with example outputs demonstrating the functionality of the programs. The programs cover fundamental programming concepts and user interactions in Python.

Uploaded by

amaravathi9566
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
You are on page 1/ 19

1.

TEMPERATURE CONVERSION PROGRAM

print("Temperature Conversion Menu")


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"Temperature in Celsius:", celsius)
elif choice == 2:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9 / 5) + 32
print(f"Temperature in Fahrenheit: ", fahrenheit)
else:
print("Invalid choice")
OUTPUT 1

Temperature Conversion Menu

1. Fahrenheit to Celsius

2. Celsius to Fahrenheit

Enter your choice (1 or 2): 1

Enter temperature in Fahrenheit: 100

Temperature in Celsius: 37.77777777777778

OUTPUT 2

Temperature Conversion Menu

1. Fahrenheit to Celsius

2. Celsius to Fahrenheit

Enter your choice (1 or 2): 2

Enter temperature in Celsius: 0

Temperature in Fahrenheit: 32.0

OUTPUT 3

Temperature Conversion Menu

1. Fahrenheit to Celsius

2. Celsius to Fahrenheit

Enter your choice (1 or 2): 3

Invalid choice
2. PATTERN CONSTRUCTION
def print_up_down_pyramid(rows):

# Upward pyramid

for i in range(1, rows + 1):

# Print leading spaces

for j in range(rows - i):

print(" ", end="")

# Print stars

for k in range(2 * i - 1):

print("*", end="")

print()

# Downward pyramid (inverted)

for i in range(rows - 1, 0, -1):

# Print leading spaces

for j in range(rows - i):

print(" ", end="")

# Print stars

for k in range(2 * i - 1):

print("*", end="")

print()

# Example usage:

num_rows = 5

print_up_down_pyramid(num_rows)
OUTPUT

***

*****

*******

*********

*******

*****

***

*
3. CALCULATE TOTAL MARKS, PERCENTAGE AND GRADE OF A
STUDENT
# Python program to calculate total marks, percentage, and grade

# Taking marks input for 5 subjects

print("Enter marks for 5 subjects (out of 100):")

name = (input("Enter your name : "))

tamil = float(input("Tamil: "))

english = float(input("English: "))

accounts = float(input("Accounts: "))

computer = float(input("Computer: "))

history = float(input("History : "))

# Calculate total and percentage

total_marks = tamil + english+ accounts + computer + history

percentage = (total_marks / 5)

# Determine the grade

if percentage >= 80:

grade = "A"

elif percentage >= 70:

grade = "B"

elif percentage >= 60:

grade = "C"
elif percentage >= 40:

grade = "D"

else:

grade = "E"

# Display the result

print("\n----- Result -----")

print("\n Student Name:",name)

print("\n Total Marks = ", total_marks)

print("\n Percentage =” , percentage)

print("\n Grade =”, grade)

OUTPUT
Enter marks for 5 subjects (out of 100):

Enter your name: Kavitha

Tamil: 85

English: 78

Accounts: 90

Computer: 88

History: 80

----- Result -----

Student Name: Kavitha

Total Marks = 421.0

Percentage = 84.2

Grade = A
4. AREA OF RECTANGLE , SQUARE , CIRCLE AND TRIANGLE

import math

def calculate_area():

"""Calculates the area of a rectangle, square, circle, or triangle based


on user input."""

print("Choose a shape to calculate the area:")

print("1. Rectangle")

print("2. Square")

print("3. Circle")

print("4. Triangle")

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

if choice == '1':

length = float(input("Enter the length of the rectangle: "))

width = float(input("Enter the width of the rectangle: "))

area = length * width

print(f"The area of the rectangle is: {area}")

elif choice == '2':

side = float(input("Enter the side length of the square: "))

area = side * side

print(f"The area of the square is: {area}")

elif choice == '3':

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


area = math.pi * radius**2

print(f"The area of the circle is: {area}")

elif choice == '4':

base = float(input("Enter the base of the triangle: "))

height = float(input("Enter the height of the triangle: "))

area = 0.5 * base * height

print(f"The area of the triangle is: {area}")

else:

print("Invalid choice. Please enter a number between 1 and 4.")

calculate_area()

OUTPUT 1

Choose a shape to calculate the area:

1. Rectangle

2. Square

3. Circle

4. Triangle

Enter your choice (1-4): 1

Enter the length of the rectangle: 10

Enter the width of the rectangle: 5

The area of the rectangle is: 50.0


OUTPUT 2

Choose a shape to calculate the area:

1. Rectangle

2. Square

3. Circle

4. Triangle

Enter your choice (1-4): 2

Enter the side length of the square: 6

The area of the square is: 36.0

OUTPUT 3

Choose a shape to calculate the area:

1. Rectangle

2. Square

3. Circle

4. Triangle

Enter your choice (1-4): 3

Enter the radius of the circle: 7

The area of the circle is: 153.93804002589985


5. PRIME NUMBERS BETWEEN 1 AND 20
print("Prime numbers between 1 and 20 are:")

ulmt = 20

for num in range(ulmt + 1): # include 20 also

# prime numbers are greater than 1

if num > 1:

for i in range(2, num):

if (num % i) == 0:

break

else:

print(num)

OUTPUT

Prime numbers between 1 and 20 are:

11

13

17
6. FACTORIAL OF A NUMBER

# Factorial of a number using recursion

def recur_factorial(n):

if n == 1:

return n

else:

return n*recur_factorial(n-1)

num= int(input("Enter the Number to find factorial ="))

# check if the number is negative

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

print("The factorial of", num, "is", recur_factorial(num))


OUTPUT 1

Enter the Number to find factorial = 5

The factorial of 5 is 120

OUTPUT 2

Enter the Number to find factorial = 0

The factorial of 0 is 1

OUTPUT 3

Enter the Number to find factorial = -5

Sorry, factorial does not exist for negative numbers


7. COUNT THE NUMBER OF EVEN AND ODD NUMBERS

# Program to count even and odd numbers in an array

# Step 1: Input size of array

N = int(input("Enter number of elements: "))

# Step 2: Input array elements

arr = [ ]

print("Enter the numbers:")

for i in range(N):

num = int(input())

arr.append(num)

# Step 3: Initialize counters

even_count = 0

odd_count = 0

# Step 4: Count even and odd

for num in arr:

if num % 2 == 0:

even_count += 1

else:

odd_count += 1

# Step 5: Display results

print("Number of even numbers:", even_count)

print("Number of odd numbers:", odd_count)


OUTPUT

Enter number of elements: 6

Enter the numbers:

10

23

45

66

78

91

Number of even numbers: 3

Number of odd numbers: 3


8. REVERSE A STRING WORD BY WORD

# Program to reverse a string word by word using class

class ReverseString:

def reverse_words(self, sentence):

# Split the sentence into words

words = sentence.split()

# Reverse the list of words

reversed_words = words[::-1]

# Join them back into a string

return ' '.join(reversed_words)

# Create object of class

obj = ReverseString()

# Input string

sentence = input("Enter a string: ")

# Call method and display result

result = obj.reverse_words(sentence)

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

Output
Enter a string: Python is very powerful

Reversed string word by word: powerful very is Python


9. READ A FILE CONTENT AND COPY ODD LINES INTO A NEW
FILE

# Read a file and copy only odd lines into a new file

def copy_odd_lines(input_file, output_file):

with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:

for line_number, line in enumerate(infile, start=1):

if line_number % 2 != 0: # odd lines

outfile.write(line)

# Example usage

input_file = "D:\input.txt"

output_file = "D:\odd_lines.txt"

copy_odd_lines(input_file, output_file)

print("Odd lines copied from {input_file} to {output_file}")

OUTPUT

Odd lines copied from D:\input.txt to D:\odd_lines.txt


INPUT.TXT FILE
Hellow

how are you

I Am fine.

what about you

now i am working

studying Trinity college

I am I Bcom(CA)

Odd_lines.txt file
Hellow

I Am fine.

now i am working

I am I Bcom(CA)
10. TURTLE GRAPHICS WINDOW WITH SPECIFIC SIZE

import turtle

# Create screen

screen = turtle.Screen()

# Set window size (width=600, height=400) and position (startx=200,


starty=100)

screen.setup(width=400, height=400, startx=50, starty=50)

# Set window title

screen.title("Turtle Window with Position")

# Create a turtle and draw

pen = turtle.Turtle()

pen.forward(50) # draw a line

# Keep window open until click

screen.exitonclick()
OUTPUT

You might also like