Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views22 pages

Computer Project

The document is a practical file for a computer science course at White Leaf Public School, detailing various Python programming tasks. It includes code examples for generating patterns, calculating series sums, checking number properties, and manipulating lists and strings. The file is submitted by a student named Saksham Thakran and includes an index of the programs covered.

Uploaded by

studyclass12yt
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)
10 views22 pages

Computer Project

The document is a practical file for a computer science course at White Leaf Public School, detailing various Python programming tasks. It includes code examples for generating patterns, calculating series sums, checking number properties, and manipulating lists and strings. The file is submitted by a student named Saksham Thakran and includes an index of the programs covered.

Uploaded by

studyclass12yt
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
You are on page 1/ 22

WHITE LEAF PUBLIC SCHOOL

PRACTICAL FILE
COMPUTER FILE(PYTHON-083)
[SESSION:2024-25]

Submitted to:
Submitted by:
Mr. Upendra Prasad
Name: Saksham Thakran
PGT(Computer Science) Roll
no.: 06
Index
S.
N PROGRAM TEACHE
PAGE R SIGN
o NO.
1. Write the python code for the Given
patterns

2. Write a program to input the value of x


and n and print the sum of the following
series:
3. Determine whether a number is a perfect
number, an Armstrong number or a
palindrome.
4. Input a number and check if the number is
a prime or composite number.
5. Display the terms of a Fibonacci series.
6. Compute the greatest common divisor and
least
common multiple of two integers.
7. Count and display the number of vowels,
consonants, uppercase, lowercase
characters in string.
8. Input a string and determine whether it is
a palindrome or not; convert the case of
characters in a string.
9. Find the largest/smallest number in a
list/tuple.
10 Input a list of numbers and swap elements
at the even location with the elements at
the odd location.
11 Input a list/tuple of elements, search for a
. given element in the list/tuple.
12 Create a dictionary with the roll number,
. name and marks of n students in a class
and display the names of students who
have marks above 75.

1.Write the python code for the following patterns :-


(i) *
**
***
****
******
Code
for i in range(1, 6):

print("*" * i)

Output
(ii) 12345
1234
123
12
1

Code
for i in range(5, 0, -1):

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

print(j, end="")

print()

Output
(iii)A
AB
ABC
ABCD
ABCDE

Code
for i in range(1, 6):

for j in range(i):

print(chr(65 + j), end="")

print()

Output
2.Write a program to input the value of x and n and print the sum
of the following series:
(i)1+x+x²+x3+x4+….....+xn

Code
x = int(input("Enter the value of x: "))

n = int(input("Enter the value of n: "))

sum = 0

term = 1

for i in range(n + 1):

sum += term

term *= x

print("The sum of the series is:", sum)

Output
(ii)1-x+x2-x3+x4-.......xn

Code
x = int(input("Enter the value of x: "))

n = int(input("Enter the value of n: "))

sum = 1

sign = -1

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

sum += sign * (x ** i)

sign *= -1

print("The sum of the series is:", sum)

Output

(iii)x+x2/2+x3/3+x4/4......xn/n
Code
def calculate_series_sum(x, n): """Calculates the sum of the series
x + x^2/2 + x^3/3 + ... + x^n/n."""

sum = 0
for i in range(1, n + 1):
sum += x**i / i
return sum

x = int(input("Enter the value of x: "))

n = int(input("Enter the value of n: "))

result = calculate_series_sum(x, n) print("The sum of the series


is:", result)

Output

(iv)x+x2/2!+x3/3!+x4/4.....+xn/n
Code
def calculate_series_sum(x, n): """Calculates the sum of the series
x + x^2/2! + x^3/3! + ... + x^n/n!"""

sum = 0
factorial = 1
for i in range(1, n + 1):
factorial *= i
sum += (x ** i) / factorial
return sum
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

result = calculate_series_sum(x, n) print("The sum of the series


is:", result)

Output

3.Determine whether a number is a perfect number, an


Armstrong number or a palindrome.
Code
def is_perfect_number(n): """Checks if a number is a perfect
number.""" if n <= 1:

return False

divisors = [1]

for i in range(2, int(n**0.5) + 1):

if n % i == 0:
divisors.extend([i, n // i]) return sum(divisors) == n

def is_armstrong_number(n): """Checks if a number is an


Armstrong number."""

num_str = str(n)

length = len(num_str)return sum(int(digit)**length for digit in


num_str)=n

def is_palindrome(n): """Checks if a number is a palindrome."""

return str(n) == str(n)[::-1]

def check_number(n): """Checks if a number is perfect, Armstrong,


or a palindrome."""

if is_perfect_number(n):

print(f"{n} is a perfect number.")

if is_armstrong_number(n):

print(f"{n} is an Armstrong number.")


if is_palindrome(n):

print(f"{n} is a palindrome.")

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

check_number(number)

Output
4.Input a number and check if the number is a prime or composite
number.

Code
def is_prime(n): """Checks if a number is prime."""

if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for i in range(3, int(n**0.5) + 1, 2):
if n % i == 0:
return False
return True

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

if is_prime(num):

print(num, "is a prime number.")

else:

print(num, "is a composite number”)


Output
5. Display the terms of a Fibonacci series.

Code
def fibonacci(n):
a, b = 0, 1
if n <= 0:
return []
elif n == 1:
return [a]
elif n == 2:
return [a, b]
else:
series = [a, b]
for i in range(2, n):
c=a+b
series.append(c)
a, b = b, c
return series
n = int(input("Enter the number of terms: "))
# Display the Fibonacci series
print("Fibonacci series:")
print(fibonacci(n))
Output
6. Compute the greatest common divisor and least
common multiple of two integers.
Code

def gcd(a, b):


"""Computes the greatest common divisor of two integers."""
while b:
a, b = b, a % b
return a
def lcm(a, b):
"""Computes the least common multiple of two integers."""
return (a * b) // gcd(a, b)
num1 = int(input("Enter first integer: "))
num2 = int(input("Enter second integer: "))
print("GCD:", gcd(num1, num2))
print("LCM:", lcm(num1, num2))

Output
7. Count and display the number of vowels, consonants,
uppercase, lowercase characters in string.

Code
def count_characters(string):
"""Counts vowels, consonants, uppercase, and lowercase characters in a
string."""
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0
for char in string:
if char.isalpha():
if char in "aeiouAEIOU":
vowels += 1
else:
consonants += 1
if char.isupper():
uppercase += 1
else:
lowercase += 1
print("Vowels:", vowels)
print("Consonants:", consonants)
print("Uppercase:", uppercase)
print("Lowercase:", lowercase)
# Get input string from the user
string = input("Enter a string: ")
# Call the function to count characters
count_characters(string)
Output

8. Input a string and determine whether it is a palindrome


or not; convert the case of characters in a string.
Code
def is_palindrome(string): """Checks if a string is a palindrome using a
loop."""
string = string.lower() # Ignore case sensitivity length = len(string)
for i in range(length // 2): if string[i] != string[length - i - 1]: return False
return True
def convert_case(string): """Converts the case of characters in a string using
a loop."""
new_string = "" for char in string: if char.islower(): new_string +=
char.upper() else: new_string += char.lower()
return new_string
Get input from the user
string = input("Enter a string: ")
Check if it's a palindrome
if is_palindrome(string): print("The string is a palindrome.") else: print("The
string is not a palindrome.")
Convert the case of the string
converted_string = convert_case(string) print("Converted string:",
converted_string)

Output

9. Find the largest/smallest number in a list/tuple.


Code
def find_largest_smallest(numbers): """Finds the largest and smallest
numbers in a list or tuple.
Args: numbers: A list or tuple of numbers.
Returns: A tuple containing the largest and smallest numbers. """
if not numbers: return None, None
largest = numbers[0] smallest = numbers[0]
for num in numbers: if num > largest: largest = num elif num < smallest:
smallest = num
return largest, smallest
Example usage:
my_list = [10, 5, 8, 20, 3]
my_tuple = (12, 7, 15, 9)
largest_list, smallest_list = find_largest_smallest(my_list) largest_tuple,
smallest_tuple = find_largest_smallest(my_tuple)
print("Largest in list:", largest_list) print("Smallest in list:", smallest_list)
print("Largest in tuple:", largest_tuple) print("Smallest in tuple:",
smallest_tuple)

Output

10. Input a list of numbers and swap elements at the even


location with the elements at the odd location.
Code
def swap_even_odd(lst): """Swaps elements at even locations with elements
at odd locations in a list."""
for i in range(0, len(lst) - 1, 2):
lst[i], lst[i + 1] = lst[i + 1], lst[i]

return lst

Get input from the user


num_list = [int(x) for x in input("Enter a list of numbers separated by
spaces: ").split()]
Swap the elements
swapped_list = swap_even_odd(num_list)
Print the result
print("Swapped list:", swapped_list)

Output

11. Input a list/tuple of elements, search for a given


element in the list/tuple.
Code
def search_element(data, target):
for element in data:
if element == target:
return True
return False

# Get input from the user


data = input("Enter a list or tuple of elements (comma-
separated): ").split(",")
target = input("Enter the element to search for: ")
# Convert the input string to a list or tuple based on user's
choice
if data[0].startswith("("):
data = tuple(data)
else:
data = [eval(x) for x in data]

# Search for the element


if search_element(data, eval(target)):
print("Element found!")
else:
print("Element not found.")
Output
12. Create a dictionary with the roll number, name and
marks of n students in a class and display the names of
students who have marks above 75.

Code
def create_student_dict(): n = int(input("Enter the number of
students: ")) student_data = {}
for i in range(n):
roll_no = int(input("Enter roll number: "))
name = input("Enter name: ")
marks = int(input("Enter marks: "))
student_data[roll_no] = {"name": name, "marks": marks}

return student_data

def display_above_75(student_data): print("Students with marks


above 75:") for roll_no, data in student_data.items(): if
data["marks"] > 75: print(data["name"])
--- Main program ---
student_dict = create_student_dict()
display_above_75(student_dict)
Output

Thank you

You might also like