0% found this document useful (0 votes)
18 views3 pages

Python Programming Exercises for Class 11

Uploaded by

bishnoir814
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)
18 views3 pages

Python Programming Exercises for Class 11

Uploaded by

bishnoir814
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

Practical File for Class 11 – Python Programming

Program 1: Printing "Hello, World!"


print("Hello, World!")

Output:
Hello, World!

Program 2: Taking user input


name = input("Enter your name: ")
print("Hello, " + name + "!")

Output Example:
Enter your name: John
Hello, John!

Program 3: Arithmetic Operations


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)
print("Difference:", a - b)
print("Product:", a * b)
print("Quotient:", a / b)

Output Example:
Enter first number: 5
Enter second number: 3
Sum: 8
Difference: 2
Product: 15
Quotient: 1.6667

Program 4: Swapping two numbers without using a third variable


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print("After swapping: a =", a, ", b =", b)

Output Example:
Enter first number: 2
Enter second number: 5
After swapping: a = 5 , b = 2

Program 5: Check whether a number is even or odd


num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")

Output Example:
Enter a number: 4
Even number

Program 6: Find the largest number among three


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

if a >= b and a >= c:


print("Largest number:", a)
elif b >= c:
print("Largest number:", b)
else:
print("Largest number:", c)

Output Example:
Enter first number: 3
Enter second number: 7
Enter third number: 5
Largest number: 7

Program 7: Print numbers from 1 to 10 using a loop

for i in range(1, 11):

print(i)

Program 8: Calculate the factorial of a number

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

factorial = 1

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

factorial *= i

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

Program 11: Reverse a tuple


tup = (1, 2, 3, 4, 5)
print("Reversed tuple:", tup[::-1])

Program 12: Create and manipulate a dictionary


student = {"name": "John", "age": 16, "grade": "A"}

print(student)

print("Name:", student["name"])

student["age"] = 17 # Update value

print(student)

Common questions

Powered by AI

Calculating a factorial using a loop involves a time complexity of O(n), reflecting how it iteratively multiplies numbers up to 'n'. In contrast, a recursive approach also has a time complexity of O(n), but with additional space complexity due to the call stack, resulting in O(n) space usage. This makes loops generally more efficient in terms of memory, as they operate in constant space O(1). For larger inputs, iterative methods are preferable to avoid stack overflow issues inherent in recursion .

Input validation is vital for ensuring that user-provided data adheres to expected formats and ranges, preventing errors and potential security vulnerabilities. For instance, converting user input to integers necessitates verifying the input is numeric ('int()' on non-numeric input throws a ValueError). Proper validation mechanisms prevent erroneous program execution, enhance robustness, and guide user input through feedback. It is crucial for maintaining program reliability, especially in data-centric and user-driven applications .

Dictionaries in Python provide a flexible means of associating keys with values, allowing for efficient data retrieval, insertion, and updating operations. They offer an average complexity of O(1) for these operations, which makes them suitable for applications requiring fast access to data. However, they may consume more memory than other simple data structures like lists and do not maintain order before Python 3.7. For managing related data, like in handling user information, dictionaries are highly beneficial due to their ease of key-value access and update .

Tuple slicing in Python ('tup[::-1]') allows efficient data manipulation by providing ways to access subsets and reverse data without altering the original tuple. This enhances flexibility in handling immutable data and avoids the overhead of creating new data structures. Tuple slicing can simplify operations like reordering or selecting conditional elements, allowing for concise and expressive code ideal for read-only configurations or scenarios where data integrity is crucial .

Understanding control structures, particularly conditional statements, allows developers to implement complex decision-making capabilities within their programs. By using 'if', 'elif', and 'else', one can guide program flow based on conditions, thereby yielding more adaptable and responsive applications. These structures enhance code efficiency by enabling selective execution and reducing unnecessary computations, exemplified by checking even/odd status or finding the largest among numbers. Effective use of control structures results in more maintainable and scalable code .

Type conversion in Python is crucial for performing arithmetic operations because user inputs are defaulted as strings. An operation like addition requires integers or floats for correct results ('int(input())' converts strings to integers). Risks include runtime errors if conversions are misapplied (e.g., trying to convert a non-numeric string to int) and potential data loss during conversions, particularly from floats to integers (truncation of decimals). Error handling mechanisms are essential to mitigate these risks .

In Python, user input is retrieved as a string type, necessitating explicit conversion to the required data type before performing operations that expect numbers (e.g., arithmetic). This ensures type safety and correct computation results. For instance, converting input using 'int()' allows for arithmetic operations like addition ('a + b') to function properly. Mismanaging type conversion may lead to runtime errors or incorrect program behavior .

Python's built-in functions, like 'print()' and 'input()', offer simplicity and ease of use for fundamental tasks. They abstract complex underlying operations, enabling developers to focus on logic without dealing with low-level details. These functions facilitate rapid development and maintainability, fostering code readability and reducing error-prone implementations. However, over-reliance on defaults without understanding their nuances (e.g., input always being string) can lead to inefficient practices unless carefully applied .

Swapping two numbers without using a third variable in Python can be achieved using tuple unpacking: 'a, b = b, a'. This method leverages the ability of Python to pack and unpack values in a single line. The computational advantage of this method is that it avoids the temporary storage requirement for a third variable, reducing memory overhead and potentially speeding up execution, especially relevant in resource-constrained environments .

Using a for loop with a 'range()' function provides a straightforward method to iterate over a specific sequence of numbers. It simplifies the code, improving readability and reducing the potential for errors compared to while loops, which require explicit incrementing of counters. The 'range(1, 11)' parameter ensures the loop runs exactly 10 times, producing a sequence from 1 to 10 inclusively. This method efficiently handles repetitive tasks, making it useful for tasks like generating sequences .

You might also like