100% found this document useful (1 vote)
82 views2 pages

Python Exercises for Class 10

This document is a Python worksheet for Class 10 students containing 10 simple programs to practice basic programming concepts. The exercises include finding the greatest of two numbers, checking even or odd, printing multiplication tables, and more. Students are encouraged to type, execute, and modify the programs for better understanding.

Uploaded by

nireljoshua5177
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
100% found this document useful (1 vote)
82 views2 pages

Python Exercises for Class 10

This document is a Python worksheet for Class 10 students containing 10 simple programs to practice basic programming concepts. The exercises include finding the greatest of two numbers, checking even or odd, printing multiplication tables, and more. Students are encouraged to type, execute, and modify the programs for better understanding.

Uploaded by

nireljoshua5177
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

Python Worksheet – Class 10

This worksheet contains 10 simple Python programs based on the Class 10 Computer
Applications curriculum.
Students should practice typing and executing each program, observe the output, and try
modifying them for better understanding.

1. Find the Greatest of Two Numbers


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

if a > b:
print("Greatest number is:", a)
else:
print("Greatest number is:", b)

2. Check Whether Number is Even or Odd


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

if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")

3. Check if Number is Positive, Negative, or Zero


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

if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")

4. Print Multiplication Table


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

for i in range(1, 11):


print(n, "x", i, "=", n * i)

5. Sum of First N Natural Numbers


n = int(input("Enter a number: "))
sum = 0

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


sum += i
print("Sum of first", n, "natural numbers is:", sum)

6. Check if a String is a Palindrome


s = input("Enter a string: ")

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

7. Count Vowels in a String


s = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0

for ch in s:
if ch in vowels:
count += 1

print("Number of vowels:", count)

8. Find Largest Element in a List


numbers = [10, 25, 4, 78, 56]
print("List:", numbers)
print("Largest number:", max(numbers))

9. Display Fibonacci Series up to N Terms


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

10. Find Factorial of a Number (Using Function)


def factorial(n):
fact = 1
for i in range(1, n + 1):
fact *= i
return fact

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


print("Factorial of", num, "is:", factorial(num))

Common questions

Powered by AI

The program checks if a string is a palindrome by using Python's string slicing feature to reverse the string. This is done with 's[::-1]', which creates a reverse copy of 's'. It then compares the original string with the reversed version using an equality check. If both are equal, the string is a palindrome. String slicing is significant here as it provides a compact and efficient way to reverse a string without requiring additional loops or data structures, thus simplifying the palindrome check .

The use of Python's built-in max() function simplifies finding the largest element in a list by abstracting and optimizing what would otherwise require manual iteration and comparison. With 'max(numbers)', Python handles the iteration efficiently under the hood. This built-in function offers advantages in readability, reliability, and performance, eliminating errors that might arise from manual implementation and leveraging internally optimized algorithms .

The program iterates over each character in a given string, checking if it belongs to a predefined set of vowels 'aeiouAEIOU'. It increments a counter for each vowel occurrence. The approach involves a linear scan, making it efficient in terms of time complexity (O(n)), where n is the string length. Computationally, it balances directness and efficiency, performing necessary operations in a single pass through the string without extra overhead .

Understanding data types is critical in comparing two numbers because Python's comparison operations depend on operand types. In the program, it is crucial the inputs 'a' and 'b' are integers for accurate comparison: 'a = int(input('Enter first number: '))'. Using the right data type ensures the program correctly executes numerical comparisons without runtime errors related to data type mismatches. This knowledge ensures reliable, precise comparisons and outcomes as intended .

Loops, specifically for loops, in Python facilitate generating multiplication tables by iterating over a sequence of numbers (from 1 to 10) and computing the product of each sequence element with the specified number. The loop allows concise and controlled iteration, performing precise, repeated operations necessary for constructing a multiplication table: 'for i in range(1, 11): print(n, 'x', i, '=', n * i)'. This construct minimizes manual repetition and logic errors, boosting performance and reducing computational complexity by leveraging built-in iteration mechanics .

The program uses a for loop to iteratively generate the Fibonacci sequence. It initializes two variables 'a' and 'b' to 0 and 1, respectively, and repeatedly swaps and sums them across n iterations to produce the sequence up to the desired term. Iterative processes like this are generally more memory-efficient than recursion, especially for Fibonacci calculations, as recursion can lead to excessive function calls, consuming more stack memory and risking a stack overflow for larger n. Thus, iteration is optimal, offering control and efficiency .

The Python program defines a function named 'factorial' which takes a number n as a parameter and initializes a variable 'fact' to 1. It then uses a for loop to multiply 'fact' by each integer from 1 to n, effectively calculating n!. The use of a function is beneficial as it encapsulates the factorial logic, allowing it to be reused for different inputs without rewriting code. Additionally, it enhances code readability and manageability, and functions can be tested independently for correctness .

The program employs conditional constructs to determine whether an integer is positive, negative, or zero using an if-elif-else statement. The logic checks if 'n' is greater than zero to classify it as 'Positive'; if less than zero, 'Negative'; and zero otherwise. This sequence logically orders conditions by likelihood and exclusivity, ensuring the integer is evaluated through all possibilities efficiently and unambiguously .

The summation program uses a simple for loop, incrementally adding each natural number up to n. This algorithm naturally follows arithmetic progressions rules: sum = n(n + 1)/2, albeit implemented iteratively for demonstration and understanding purposes. It effectively illustrates algorithmic development in a straightforward manner, demonstrating iterative accumulation and control flow logic. Although an arithmetic formula is more efficient, the iterative approach aids beginners in grasping loops and sum logic, balancing didactic simplicity with functional accuracy .

In Python, conditionals can be used with the modulo operator (%) to determine if a number is even or odd. The modulo operation returns the remainder of a division operation. By checking if the remainder of the division of the number by 2 is zero, we can conclude the number is even; otherwise, it is odd. This is implemented using an if-else statement: 'if num % 2 == 0: print(num, 'is Even') else: print(num, 'is Odd')'. The conditional effectively helps in branching the logic or execution flow based on whether a condition is met or not .

You might also like