cd back (2)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

a)* *Output of given code snippet.* d)* *Explain the following loops with example.

d)* *Explain the following loops with example.* - *While loop:* Repeats while a e)* *How to add multiple elements at the end of a list?*
Corrected code: ,, python ,, check1 = ['Learn', 'Quiz', 'Practice', 'Contribute'] condition is true. ,, python,, i = 0,, while i < 5:,, print(i),, i += 1 Use the extend() method. Python,, lst = [1, 2, 3],, lst.extend([4, 5, 6])
check2 = check1 ,, check3 = check1[:] ,, check2[0] = 'Code' ,, check3[1] = 'Mcq' - *For loop:* Iterates over items in a sequence. print(lst) # Output: [1, 2, 3, 4, 5, 6]
count = 0 ,, for c in (check1, check2, check3): Python,, for i in range(5):,, print(i) f)* *Explain the remove() method.* The remove() method removes the first
if c[0] == 'Code': ,, count += 1 ,, if c[1] == 'Mcq': *e)* *How to perform input-output operations? Explain with example.* occurrence of a specified value from a list.Example:,, python,, lst = [1, 2, 3, 2]
count += 10 ,, print(count) # Output: 11 - *Input:* input() to read user input. lst.remove(2),, print(lst) # Output: [1, 3, 2]
b)* *Program to update Counter.* python ,, Counter = {} - *Output:* print() to display output. *g)* *Write a lambda function to add 10 to a given integer.*
def addToCounter(country): ,, if country in Counter: ,, Counter[country] += 1 Example: ,, python,, name = input("Enter your name: "),, print(f"Hello, {name}!") Python,, add_ten = lambda x: x + 10,, print(add_ten(5)) # Output: 15
else: ,, Counter[country] = 1 ,, addToCounter("USA"),, addToCounter("India") a)* *Reverse a string and eliminate the letter ‘s’.* python,, s = input("Enter a a)* *What is a package? Explain with example how to create a package.*
addToCounter("USA") ,, print(Counter) # Output: {'USA': 2, 'India': 1} string: "),, reversed_s = s[::-1].replace('s', '').replace('S', ''),, print(reversed_s) A package is a collection of Python modules organized in directories with an
a)* *What are the advantages of Python?* - Easy to learn and use. *b)* *Raise an exception if age is less than 18.* __init__.py file. *Example:*1. Create a directory mypackage.
- Extensive library support. - Platform-independent. - Strong community support. Python,, class AgeException(Exception): ,, pass ,, age = int(input("Enter your age: 2. Inside mypackage, add __init__.py and modules like module1.py.
- Dynamic typing and memory management. *b)* *List out main differences ")),, if age < 18: ,, raise AgeException("Age is less than 18."). Python,, # In module1.py,, def greet():,, print("Hello from module1!")
between lists & tuples.* - *Lists*: Mutable, defined with square brackets []. - *d)* *Demonstrate list slicing.* 3. Import and use:,, python,, from mypackage import module1
*Tuples*: Immutable, defined with parentheses ().*c)* *Python is a scripting lst = [10, 20, 30, 40, 50] .. print(lst[1:4]) # Output: [20, 30, 40] module1.greet()
language. Comment.* j)* *What is lambda function? Give example.* e)* *A tuple is an ordered collection of items. Comment.* Yes, tuples maintain
A lambda function is an anonymous function defined using the lambda keyword. the order of elements, and the elements can be accessed via indices.
Python:,, square = lambda x: x * x:,,print(square(4)) # Output: 16

a)* *Recursive function to display the sum of digits until a single digit.* a) What is dry run in Python? - A dry run is the process of manually walking a) Write a Python program to check if a given number is Armstrong.
def sum_of_digits(n):,, if n < 10:,, return n through the code to understand how it works and to check for any logical errors, def is_armstrong(n): ,, num_str = str(n) ,, length = len(num_str)
return sum_of_digits(sum(int(digit) for digit in str(n))) without actually running the code. sum_of_powers = sum(int(digit) ** length for digit in num_str)
num = int(input("Enter a number: ")) ,, b) Give the purpose of selection statements in Python.* return sum_of_powers == n
print("Single-digit sum:", sum_of_digits(num)) - Selection statements like if, if-else, and if-elif-else are used to make decisions in b) Write a Python program to display power of 2 using an anonymous function.
b)* *Compute sum of squares of list integers.* the program, allowing different paths of execution based on conditions. => power_of_2 = lambda x: 2 ** x print(power_of_2(5)) # Output: 32
n = int(input("Enter number of integers: ")) c) List the types of type conversion in Python. => The types of type conversion in c) Write a Python program to print even-length words in a string.
lst = [int(input()) for _ in range(n)] Python are: 1. Implicit type conversion (Automatic) 2. Explicit type conversion def even_length_words(text):,, words = text.split() ,, for word in words:,,
print("Sum of squares:", sum(x**2 for x in lst)) d) What is the use of pass statement? - The pass statement in Python is used as a if len(word) % 2 == 0: print(word)
c)* *Count occurrences of "India" and "Country" in pledge.txt.* placeholder for future code. It allows the program to run without any errors if a # Example even_length_words("This is an example string")
with open("pledge.txt", "r") as file: block of code is empty, especially in class or function definitions. a) Write a Python program to check for Zero Division Error Exception.
text = file.read() e) Explain the function enumerate().:- The enumerate() function in Python adds a try: a = 5 / 0 ,, except ZeroDivisionError: ,, print("Error: Division by zero.")
print("India:", text.count("India")) counter to an iterable and returns it as an enumerate object, which can then be b) Write a Python program to find gcd of a number using recursion.
print("Country:", text.count("Country")) used in loops. It is useful when you need both index and value in a loop. def gcd(a, b): if b == 0: ,, return a,, else: return gcd(b, a % b)
b)* *Output of the second code snippet:* f) Explain the extend method of list.* - The extend() method adds all elements of # Example print(gcd(56, 98)) # Output: 14
The code raises an error because the decorator @f is applied incorrectly, leading to an iterable (like another list or tuple) to the end of the list. For example:
a recursive call that violates the decorator's logic. list1.extend([4, 5]) will add 4 and 5 to list1.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy