0% found this document useful (0 votes)
8 views24 pages

Sem I Program (Python)

The document contains a series of Python programming exercises, each with a specific task such as converting temperatures, constructing patterns, calculating student grades, and manipulating strings and files. Each exercise includes a program code snippet and a brief description of its functionality. The document serves as a practical guide for learning Python programming through hands-on examples.

Uploaded by

sivaishu2008
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)
8 views24 pages

Sem I Program (Python)

The document contains a series of Python programming exercises, each with a specific task such as converting temperatures, constructing patterns, calculating student grades, and manipulating strings and files. Each exercise includes a program code snippet and a brief description of its functionality. The document serves as a practical guide for learning Python programming through hands-on examples.

Uploaded by

sivaishu2008
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

CONTENT

[Link] DATE TITLE [Link] SIGN

PROGRAM TO CONVERT THE GIVEN


1 TEMPERATURE FROM FAHRENHEIT TO
CELSIUS AND VICE VERSA

WRITE A PYTHON PROGRAM TO


2 CONSTRUCT THE DIAMOND PATTERN,
USING A NESTED LOOP

PROGRAM TO CALCULATE TOTAL MARKS,


3
PERCENTAGE AND GRADE OF A STUDENT

PROGRAM, TO FIND THE AREA OF


4 RECTANGLE, SQUARE, CIRCLE AND
TRIANGLE

WRITE A PYTHON SCRIPT THAT PRINTS


5
PRIME NUMBERS LESS THAN 20

PROGRAM TO FIND FACTORIAL OF THE


6 GIVEN NUMBER USING RECURSIVE
FUNCTION.

WRITE A PYTHON PROGRAM TO COUNT


7 THE NUMBER OF EVEN AND ODD NUMBERS
FROM ARRAY OF N NUMBERS

WRITE A PYTHON CLASS TO REVERSE A


8
STRING WORD BY WORD

READ A FILE CONTENT AND COPY ONLY


THE CONTENTS AT ODD LINES INTO A NEW
9 FILE.

CREATE A TURTLE GRAPHICS WINDOW


10 WITH SPECIFIC SIZE.
1. PROGRAM TO CONVERT THE GIVEN TEMPERATURE
FROM FAHRENHEIT TO CELSIUS AND VICE VERSA

PROGRAM:
print('1. Celsius to Fahrenheit.')

print('2. Fahrenheit to Celsius.')

choice = int(input('Enter your choice: '))

if choice == 1:

c = float(input('Enter temperature in celsius: '))

f = 9 * c / 5 + 32

print('Temperature in fahrenheit scale:', f)

elif choice == 2:

f = float(input('Enter temperature in fahrenheit: '))

c = 5 * (f - 32) / 9

print('Temperature in celsius scale:', c)


\\\
else:

print('Invalid choice.')
FLOWCHART:
OUTPUT
2. WRITE A PYTHON PROGRAM TO CONSTRUCT THE
DIAMOND PATTERN, USING A NESTED LOOP

PROGRAM:
rows = int(input("Enter the number of rows: "))

k = 2 * rows - 2

for i in range(0, rows):

for j in range(0, k):

print(end=" ")

k=k-1

for j in range(0, i + 1):

print("* ", end="")

print("")

k = rows - 2

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

for j in range(k, 0, -1):

print(end=" ")

k=k+1

for j in range(0, i + 1):

print("* ", end="")

print("")
OUTPUT :
3. PROGRAM TO CALCULATE TOTAL MARKS,
PERCENTAGE AND GRADE OF A STUDENT
PROGRAM:
marks1 = int(input("Enter marks in subject 1: "))

marks2 = int(input("Enter marks in subject 2: "))

marks3 = int(input("Enter marks in subject 3: "))

marks4 = int(input("Enter marks in subject 4: "))

marks5 = int(input("Enter marks in subject 5: "))

total_marks = marks1 + marks2 + marks3 + marks4 + marks5

percentage = (total_marks / 500) * 100

if percentage >= 80:

grade = "A"

elif percentage >= 70 and percentage < 80:

grade = "B"

elif percentage >= 60 and percentage < 70:

grade = "C"

elif percentage >= 40 and percentage < 60:

grade = "D"

else:

grade = "E"

print("Total marks:", total_marks)


print("Percentage:", percentage)
print("Grade:", grade)
OUTPUT :
4. PROGRAM, TO FIND THE AREA OF RECTANGLE,
SQUARE, CIRCLE AND TRIANGLE

PROGRAM:

import math

# Functions to calculate area


def area_rectangle(length, width):
return length * width

def area_square(side):
return side * side

def area_circle(radius):
return [Link] * radius * radius

def area_triangle(base, height):


return 0.5 * base * height

# Main program
def main():
print "==== Area Calculator ===="
print "1. Rectangle"
print "2. Square"
print "3. Circle"
print "4. Triangle"
choice = raw_input("Enter your choice (1-4): ")

if choice == '1':
length = float(raw_input("Enter the length of the rectangle: "))
width = float(raw_input("Enter the width of the rectangle: "))
area = area_rectangle(length, width)
print "Area of Rectangle = %.2f" % area

elif choice == '2':


side = float(raw_input("Enter the side of the square: "))
area = area_square(side)
print "Area of Square = %.2f" % area

elif choice == '3':


radius = float(raw_input("Enter the radius of the circle: "))
area = area_circle(radius)
print "Area of Circle = %.2f" % area

elif choice == '4':


base = float(raw_input("Enter the base of the triangle: "))
height = float(raw_input("Enter the height of the triangle: "))
area = area_triangle(base, height)
print "Area of Triangle = %.2f" % area

else:
print "Invalid choice! Please enter a number between 1 and 4."
# Run the program
main()

OUTPUT :

5. WRITE A PYTHON SCRIPT THAT PRINTS PRIME


NUMBERS LESS THAN 20
PROGRAM:

# Function to check if a number is prime


def is_prime(n):
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True

# Main program
print "Prime numbers less than 20 are:"
for num in range(2, 20):
if is_prime(num):
print num

OUTPUT:
[Link] TO FIND FACTORIAL OF THE GIVEN
NUMBER USING RECURSIVE FUNCTION

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(raw_input("Enter a number to find its factorial: "))

if num < 0:
print "Factorial is not defined for negative numbers."
else:
result = factorial(num)
print "Factorial of %d is %d" % (num, result)
OUTPUT:
7. WRITE A PYTHON PROGRAM TO COUNT THE NUMBER
OF EVEN AND ODD NUMBERS FROM ARRAY
OF N NUMBERS

PROGRAM:
# Program to count even and odd numbers in a list

# Read number of elements


n = int(raw_input("Enter how many numbers: "))

# Initialize empty list


numbers = []

# Read numbers into the list


for i in range(n):
num = int(raw_input("Enter number %d: " % (i + 1)))
[Link](num)

# Initialize counters
even_count = 0
odd_count = 0

# Count even and odd numbers


for num in numbers:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
# Output results
print "Total Even numbers: %d" % even_count
print "Total Odd numbers: %d" % odd_count
OUTPUT :
8. WRITE A PYTHON CLASS TO REVERSE A STRING
WORD BY WORD

PROGRAM:

# Class to reverse a string word by word


class StringReverser:
def __init__(self, text):
[Link] = text

def reverse_words(self):
words = [Link]()
reversed_words = words[::-1]
return ' '.join(reversed_words)

# Main program
input_str = raw_input("Enter a sentence: ")
reverser = StringReverser(input_str)
reversed_str = reverser.reverse_words()
print "Reversed (word by word):", reversed_str
OUTPUT :
9. READ A FILE CONTENT AND COPY ONLY THE
CONTENTS AT ODD LINES INTO A NEW FILE

PROGRAM:

# Program to copy odd lines from one file to another

class FileOddLineCopier:
def __init__(self, source_file, destination_file):
self.source_file = source_file
self.destination_file = destination_file

def copy_odd_lines(self):
with open(self.source_file, "r") as infile, open(self.destination_file, "w") as
outfile:
for line_number, line in enumerate(infile, start=1):
if line_number % 2 != 0: # odd lines only
[Link](line)

# Example usage
# First, create a sample input file
with open("[Link]", "w") as f:
[Link]("Line 1: Hello World\n")
[Link]("Line 2: Python Programming\n")
[Link]("Line 3: File Handling Example\n")
[Link]("Line 4: Copy Odd Lines\n")
[Link]("Line 5: End of File\n")
# Use the class
copier = FileOddLineCopier("[Link]", "[Link]")
copier.copy_odd_lines()

# Display the result


print("Contents of input file:")
with open("[Link]", "r") as f:
print([Link]())

print("Contents of output file (odd lines only):")


with open("[Link]", "r") as f:
print([Link]())
OUTPUT :
10. CREATE A TURTLE GRAPHICS WINDOW WITH
SPECIFIC SIZE

PROGRAM:

import turtle

# Create the screen (window)

screen = [Link]()

[Link]("Simple Turtle Window")

[Link](width=600, height=400) # Set specific size (600x400 pixels)

# Create a turtle

t = [Link]()

# Draw a square

for _ in range(4):

[Link](100)

[Link](90)

# Keep window open until user clicks inside it

[Link]()
OUTPUT :

You might also like