0% found this document useful (0 votes)
2 views16 pages

Cs 11

The document contains practical Python programming exercises that cover various topics such as input/output operations, number comparisons, pattern generation, series summation, number classification (perfect, Armstrong, palindrome), prime checking, Fibonacci series, GCD and LCM calculation, string analysis, list manipulation, and dictionary creation. Each exercise includes code snippets and example inputs/outputs to demonstrate functionality. The exercises aim to enhance programming skills through hands-on practice.

Uploaded by

dulal
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)
2 views16 pages

Cs 11

The document contains practical Python programming exercises that cover various topics such as input/output operations, number comparisons, pattern generation, series summation, number classification (perfect, Armstrong, palindrome), prime checking, Fibonacci series, GCD and LCM calculation, string analysis, list manipulation, and dictionary creation. Each exercise includes code snippets and example inputs/outputs to demonstrate functionality. The exercises aim to enhance programming skills through hands-on practice.

Uploaded by

dulal
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/ 16

Python Programming Practical File

 Input a welcome message and display it.

msg = input("Enter welcome message: ")


print("You entered:", msg)

Enter welcome message: hello and welcome to python

You entered: hello and welcome to python


 Input two numbers and display the larger / smaller number.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Larger number:", max(a, b))
print("Smaller number:", min(a, b))

Enter first number: 2

Enter second number: 3

Larger number: 3

Smaller number: 2
 Input three numbers and display the largest / smallest number.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Largest number:", max(a, b, c))
print("Smallest number:", min(a, b, c))

Enter first number: 2

Enter second number: 6

Enter third number: 9

Largest number: 9

Smallest number: 2
 Generate the patterns using nested loops:

Pattern-1
for i in range(1, 6):
print('*' * i)
*

**

***

****

*****

2
for i in range(1, 6):
for j in range(1, i + 1):
print(j, end="")
print()
1

12

123

1234

12345

3
for i in range(1, 6):
for j in range(65, 65 + i):
print(chr(j), end="")
print()
A

AB

ABC

ABCD

ABCDE
 Write a program to input the value of x and n and print the
sum of the following series: 1 + x + x^2 + x^3 + … + x^n
x = int(input("Enter value of x: "))
n = int(input("Enter value of n: "))
sum_series = 0
for i in range(n + 1):
sum_series += x ** i
print("Sum of series is:", sum_series)

Enter value of x: 3

Enter value of n: 5

Sum of series is: 364


 Determine whether a number is a perfect number, an
Armstrong number or a palindrome.
Perfect Number
num = int(input("Enter a number: "))
sum_div = sum(i for i in range(1, num) if num % i == 0)
print("Perfect number" if sum_div == num else "Not a perfect number")

Enter a number: 6

Perfect number

Enter a number: 12

Not a perfect number

Armstrong Number
num = int(input("Enter a number: "))
temp = num
sum_pow = 0
while temp > 0:
digit = temp % 10
sum_pow += digit ** len(str(num))
temp //= 10
print("Armstrong number" if sum_pow == num else "Not an Armstrong
number")
Enter a number: 153

Armstrong number

Enter a number: 23

Not an Armstrong number

Palindrome Number
num = input("Enter a number: ")
print("Palindrome" if num == num[::-1] else "Not a palindrome")

Enter a number: 121

Palindrome
Enter a number: 145

Not a palindrome

 Input a number and check if the number is a prime or


composite number.
num = int(input("Enter a number: "))
if num <= 1:
print("Neither prime nor composite")
else:
for i in range(2, num):
if num % i == 0:
print("Composite number")
break
else:
print("Prime number")

Enter a number: 6

Composite number

Enter a number: 2

Prime number
 Display the terms of a Fibonacci series.
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
Enter number of terms: 5

0 1 1 2 3
 Compute the greatest common divisor and least common
multiple of two integers.
import math

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


b = int(input("Enter second number: "))

gcd = math.gcd(a, b)
lcm = (a * b) // gcd

print("GCD:", gcd)
print("LCM:", lcm)
Enter first number: 77

Enter second number: 789

GCD: 1

LCM: 60753
 Count and display the number of vowels, consonants,
uppercase, lowercase characters in a string.
text = input("Enter a string: ")
vowels = "aeiouAEIOU"
v, c, u, l = 0, 0, 0, 0

for char in text:


if char.isalpha():
if char in vowels:
v += 1
else:
c += 1
if char.isupper():
u += 1
else:
l += 1

print("Vowels:", v)
print("Consonants:", c)
print("Uppercase letters:", u)
print("Lowercase letters:", l)

Enter a string: fghhnfawr

Vowels: 1

Consonants: 8

Uppercase letters: 0

Lowercase letters: 9
 Input a string and determine whether it is a palindrome or not;
convert the case of characters in a string.
Palindrome Check
text = input("Enter a string: ")
text = text.lower()
print("Palindrome" if text == text[::-1] else "Not a palindrome")

Enter a string: qwertyytrewq

Palindrome

Conversion
text = input("Enter a string: ")
converted = text.swapcase()
print("Converted string:", converted)
Enter a string: rty

Converted string: RTY


 Find the largest/smallest number in a list/tuple.
nums = [int(x) for x in input("Enter numbers separated by space:
").split()]
print("Largest:", max(nums))
print("Smallest:", min(nums))
Enter numbers separated by space: 1 2 3 4 5 6 7 8 9

Largest: 9

Smallest: 1
 Input a list of numbers and swap elements at the even
location with the elements at the odd location.
lst = [int(x) for x in input("Enter elements: ").split()]
for i in range(0, len(lst) - 1, 2):
lst[i], lst[i+1] = lst[i+1], lst[i]
print("Swapped list:", lst)

Enter elements: 1 2 3 4 5

Swapped list: [2, 1, 4, 3, 5]


 Input a list of elements, search for a given element in the
list/tuple.
lst = input("Enter elements separated by space: ").split()
search = input("Enter element to search: ")
print("Found" if search in lst else "Not found")
Enter elements separated by space: 1 2 4 6 990

Enter element to search: 4

Found

Enter elements separated by space: 2 9 55 21

Enter element to search: 4

Not found
 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.
n = int(input("Enter number of students: "))
students = {}

for _ in range(n):
roll = input("Enter roll number: ")
name = input("Enter name: ")
marks = int(input("Enter marks: "))
students[roll] = {'name': name, 'marks': marks}

print("\nStudents scoring above 75:")


for roll, info in students.items():
if info['marks'] > 75:
print(f"Roll: {roll}, Name: {info['name']}, Marks:
{info['marks']}")
Enter number of students: 4

Enter roll number: 1

Enter name: RAJ

Enter marks: 88

Enter roll number: 2

Enter name: RAJU

Enter marks: 82

Enter roll number: 3

Enter name: RAHUL

Enter marks: 78

Enter roll number: 4

Enter name: RAJESH

Enter marks: 62
Students scoring above 75:

Roll: 1, Name: RAJ, Marks: 88

Roll: 2, Name: RAJU, Marks: 82

Roll: 3, Name: RAHUL, Marks: 78

You might also like