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

Python Programming Practical Exercises

This document contains a practical file for Class XI Computer Science focused on Python programming. It includes various exercises that demonstrate fundamental programming concepts such as input/output, conditional statements, loops, and data structures. Each exercise is accompanied by sample code and expected output.

Uploaded by

fantaop8
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)
10 views16 pages

Python Programming Practical Exercises

This document contains a practical file for Class XI Computer Science focused on Python programming. It includes various exercises that demonstrate fundamental programming concepts such as input/output, conditional statements, loops, and data structures. Each exercise is accompanied by sample code and expected output.

Uploaded by

fantaop8
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

Python Programming

Class XI - Computer Science Practical File (083)

Name: ____________________
Class: XI
Roll No: ________________
Q1. Python program to input a welcome message and print it

message = input("Enter welcome message: ")


print("Hello,", message)

Output:
Enter welcome message: Hi
Hello, Hi
Q2. Python program to input two numbers and display larger
number

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


b = int(input("Enter second number: "))
if a > b:
print("Larger number is:", a)
else:
print("Larger number is:", b)

Output:
Enter first number: 45
Enter second number: 78
Larger number is: 78
Q3. Python program to input two numbers and display smaller
number

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


b = int(input("Enter second number: "))
if a < b:
print("Smaller number is:", a)
else:
print("Smaller number is:", b)

Output:
Enter first number: 45
Enter second number: 78
Smaller number is: 45
Q4. Patterns using nested for loop

n = int(input("Enter number of rows: "))


for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()

Output:
Enter number of rows: 5
*
* *
* * *
* * * *
* * * * *
Q5. Determine if a number is Special (Perfect, Armstrong,
Palindrome)

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

# Palindrome Check
temp = num
rev = 0
while temp > 0:
rev = rev*10 + temp % 10
temp //= 10
if rev == num:
print("Palindrome Number")

# Armstrong Check
sum1 = 0
temp = num
while temp > 0:
digit = temp % 10
sum1 += digit ** 3
temp //= 10
if sum1 == num:
print("Armstrong Number")

# Perfect Check
sum2 = 0
for i in range(1, num):
if num % i == 0:
sum2 += i
if sum2 == num:
print("Perfect Number")

Output:
Enter number: 153
Palindrome Number
Armstrong Number
Q6. Check Prime or Composite Number

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


count = 0
for i in range(1, num+1):
if num % i == 0:
count += 1
if count == 2:
print("Prime Number")
else:
print("Composite Number")

Output:
Enter number: 29
Prime Number
Q7. Fibonacci Series

n = int(input("Enter number of terms: "))


a, b = 0, 1
print("Fibonacci Series:")
for i in range(n):
print(a, end=" ")
a, b = b, a + b

Output:
Enter number of terms: 7
Fibonacci Series:
0 1 1 2 3 5 8
Q8. Sum of a Series

n = int(input("Enter number of terms: "))


sum1 = 0
for i in range(1, n+1):
sum1 += i
print("Sum of series is:", sum1)

Output:
Enter number of terms: 10
Sum of series is: 55
Q9. GCD and LCM of Two Integers

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


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

# GCD using Euclid's Algorithm


x, y = a, b
while y:
x, y = y, x % y
gcd = x

# LCM
lcm = (a * b) // gcd

print("GCD:", gcd)
print("LCM:", lcm)

Output:
Enter first number: 24
Enter second number: 36
GCD: 12
LCM: 72
Q10. Count characters in a string

s = input("Enter a string: ")


vowels = consonants = upper = lower = 0

for ch in s:
if [Link]():
if [Link]():
upper += 1
else:
lower += 1
if [Link]() in 'aeiou':
vowels += 1
else:
consonants += 1

print("Vowels:", vowels)
print("Consonants:", consonants)
print("Uppercase:", upper)
print("Lowercase:", lower)

Output:
Enter a string: HelloWorld
Vowels: 3
Consonants: 7
Uppercase: 2
Lowercase: 8
Q11. Determine Palindrome and Case Conversion

s = input("Enter a string: ")


if s == s[::-1]:
print("Palindrome String")
else:
print("Not Palindrome")

print("Uppercase:", [Link]())
print("Lowercase:", [Link]())

Output:
Enter a string: MADAM
Palindrome String
Uppercase: MADAM
Lowercase: madam
Q12. Largest and Smallest in List/Tuple

numbers = list(map(int, input("Enter numbers: ").split()))


print("Largest:", max(numbers))
print("Smallest:", min(numbers))

Output:
Enter numbers: 12 45 67 23 89
Largest: 89
Smallest: 12
Q13. Swap List Elements at Even and Odd Indices

lst = list(map(int, input("Enter list 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)

Output:
Enter list elements: 1 2 3 4 5 6
Swapped List: [2, 1, 4, 3, 6, 5]
Q14. Search in List/Tuple

lst = list(map(int, input("Enter list elements: ").split()))


x = int(input("Enter element to search: "))
if x in lst:
print("Element found")
else:
print("Element not found")

Output:
Enter list elements: 5 10 15 20
Enter element to search: 15
Element found
Q15. Student Record Management

students = {}
n = int(input("Enter number of students: "))

for i in range(n):
name = input("Enter student name: ")
marks = int(input("Enter marks: "))
students[name] = marks

print("Students scoring above 75:")


for name, marks in [Link]():
if marks > 75:
print(name, marks)

Output:
Enter number of students: 3
Enter student name: Aman
Enter marks: 80
Enter student name: Neha
Enter marks: 72
Enter student name: Rahul
Enter marks: 90

Students scoring above 75:


Aman 80
Rahul 90

Common questions

Powered by AI

The Python program swaps list elements specifically at even and odd indices by iterating through the list with a step of 2, starting from index 0. This ensures that each pair consists of even-odd index combinations. This design is significant because it maintains logical pairing and swapping across even positioned elements and their succeeding elements without affecting list integrity or missing an index inadvertently .

The Python program classifies a number as Prime or Composite by counting its divisors. It loops from 1 to the number itself, checking divisibility. If the number has exactly two divisors, 1 and the number itself, it is classified as Prime; otherwise, it is Composite. This approach is effective because it directly tests the fundamental definition of a prime number, ensuring an accurate classification through exhaustive divisor checking .

The Python program determines whether a number is a Special number by checking three characteristics: if it's a Perfect, Armstrong, or Palindrome number. A Palindrome number reads the same backward as forward, verified by reversing the digits and comparing to the original number. An Armstrong number is equal to the sum of its digits each raised to the power of three. A Perfect number equals the sum of its proper divisors. For a given number, all these checks are performed, and the program prints the respective type if the condition is met .

The algorithm used in the Python program to calculate the Greatest Common Divisor (GCD) is Euclid's Algorithm. This algorithm works by repeatedly replacing the larger number by its remainder when divided by the smaller number until one of the numbers becomes zero. The other number at this point is the GCD. This method correctly finds the GCD because it relies on the principle that the GCD of two numbers also divides their difference .

The Python program computes the Fibonacci sequence using iterative addition. It initializes the first two terms as 0 and 1. For each subsequent term up to the specified number, it prints the current term and updates the next term as the sum of the previous two terms. This computation continues until all desired terms in the sequence are generated, relying on simple addition to build the sequence iteratively .

The program detects if a string is a palindrome by comparing it to its reverse using slicing `s[::-1]`. For case conversion, it uses the `upper()` and `lower()` methods to display the string in uppercase and lowercase respectively. This dual functional approach makes it efficient for both palindrome checking and providing different case representations .

The Python program manages student records using a dictionary to store names and marks. It employs a simple loop to collect data for each student, then iterates over the stored records to print those scoring above a certain threshold (75 marks). This strategy allows efficient data retrieval based on conditional checks, demonstrating a straightforward yet effective method for record management and querying based on specific criteria .

The Python program implements a search in a list using the `in` keyword, which checks for the presence of a specified element within the list. It reads the target element from the user input and iterates over the list elements to see if the element is matched. If found, it prints 'Element found'; otherwise, it prints 'Element not found'. This logic is efficient for membership testing as it leverages Python's built-in list operations .

The Python program counts different character types in a string using a loop that iterates over each character. It distinguishes between vowels and consonants by checking if the character is in 'aeiou'. It also differentiates between uppercase and lowercase characters using `.isupper()` and `.islower()` methods. Counts for vowels, consonants, uppercase, and lowercase are maintained separately for accurate results .

The pattern printing algorithm uses nested loops to print a triangular pattern of asterisks. The outer loop determines the number of rows, while the inner loop prints an increasing number of asterisks per row. This construct effectively scales with the number of rows, producing output efficiently with time complexity proportional to the square of n, ensuring consistent performance irrespective of input size .

You might also like