PWP 22616 AnswerBank Unit Test I
PWP 22616 AnswerBank Unit Test I
(DIPLOMA ENGINEERING)
ANSWER BANK
Unit Test-I
Program: - Computer Engineering Group Program Code:- CO
Course Title: Programming with Python Semester: - Sixth
Course Code:-PWP (22616) Scheme: I
--------------------------------------------------------------------------------------------------
print("Done")
Keywords:
• Keywords are predefined, reserved words used in Python programming that have special meanings
to the compiler.
• We cannot use a keyword as a variable name, function name, or any other identifier. They are used
to define the syntax and structure of the Python language.
Indention:
• Indentation in Python is crucial as it defines the structure and flow of the code. Unlike other
programming languages that use braces {} to define code blocks, Python uses indentation to group
statements.
Example:
def fun():
print("Hi")
Note: AI Can Make Mistakes
if True:
print("true")
else:
print("false")
print("Done")
Comments:
• Comments are the non-executable statements in a program. They are just added to describe the
statements in the program code. Comments make the program easily readable and understandable
by the programmer as well as other users who are seeing the code. The interpreter simply ignores
the comments.
• Single Line Comments: In Python for single line comments use # sign to comment out everything
following it on that line.
• Multiple Lines Comments: Multiple lines comments are slightly different. Simply use 3 single
quotes before and after the part you want to be commented.
Example:
#This is comment
#print("Statement will not be executed")
print("Statement will be excuted")
'''print("Multiple line comment")
print("Multiple line comment")
print("Multiple line comment")'''
2. List data types used in python. Explain any two with example.
Text Type: String
Numeric Type: int, float, complex
Sequence Type: list, tuple, range
Mapping Type: dict
Set Type: set
Boolean Type: bool
Example of List:
fruits = ["Apple", "Banana", "Cherry"]
print(fruits) # Output: ['Apple', 'Banana', 'Cherry']
fruits.append("Mango") # Adding an element
print(fruits) # Output: ['Apple', 'Banana', 'Cherry', 'Mango']
Example of Dictionary:
student = {"name": "John", "age": 21, "course": "Python"}
print(student["name"]) # Output: John
student["age"] = 22 # Modifying value
print(student) # Output: {'name': 'John', 'age': 22, 'course': 'Python'}
2 Marks
1. in Operator
Returns True if the specified value exists in the sequence.
Otherwise, returns False.
2. not in Operator
Returns True if the specified value does not exist in the sequence.
Otherwise, returns False.
Example:
# Using 'in' operator
fruits = ["apple", "banana", "mango"]
print("apple" in fruits) # True
print("grape" in fruits) # False
Note: AI Can Make Mistakes
# Using 'not in' operator
text = "Hello Python"
print("Python" in text) # True
print("Java" not in text) # True
The elif (short for "else if") keyword is used in conditional statements when multiple
conditions need to be checked one after another.It helps avoid multiple if statements,
making the code cleaner and more readable.
if condition1:
elif condition2:
elif condition3:
else:
break Statement
• The break statement terminates the loop completely when a certain condition is met.
• The loop stops executing immediately.
Example:
for num in range(1, 6):
if num == 3:
break # Stops the loop when num is 3
print(num)
4 Marks
4 6 8
10 12 14 16 18
num = 2
rows = 3 # Number of rows
# Printing 1 0 pattern
for j in range(2 * i - 1):
if j % 2 == 0:
print("1", end=" ")
else:
print("0", end=" ")
7. Write a python program that takes a number and checks whether it is a palindrome.
8. Write a python program takes in a number and find the sum of digits in a number.
num = int(input("Enter a number: ")) # User input
sum_digits = 0
9. Write a Python program to find the factorial of a number provided by the user.
num = int(input("Enter a number: ")) # User input
fact = 1
11. Explain decision making statements If - else, if - elif - else with example.
if-else Statement: The if-else statement is used when we want to execute one block of code if
a condition is true and another block if the condition is false.
Syntax:
if condition:
# Code to execute if the condition is true
else:
# Code to execute if the condition is false
Example:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
if-elif-else Statement: The if-elif-else statement is used when there are multiple conditions to
check.
Syntax:
if condition1:
# Code if condition1 is true
elif condition2:
# Code if condition2 is true
elif condition3:
# Code if condition3 is true
else:
# Code if none of the above conditions are true
Example:
marks = int(input("Enter your marks: "))
2. pop() Method
The pop() method removes a key-value pair from the dictionary and returns the value.
Syntax:
dictionary.pop(key, default_value)
Example:
student = {"name": "AI", "age": 20, "city": "Pune"}
age = student.pop("age") # Removes "age" and returns its value
print(age) # Output: 20
print(student) # Output: {'name': AI, 'city': 'Pune'}
4 Marks
1. Compare list and tuple.
3. index() – Finding the Index of an Element: The index() function returns the first
position (index) where a given element is found in a tuple.
Example:
numbers = (5, 10, 15, 20, 25)
4. max() & min() – Finding Maximum and Minimum Values: The max() function
returns the largest element, and min() returns the smallest element in a tuple.
Example:
values = (3, 8, 1, 6, 9)
print(max(values)) # Output: 9
print(min(values)) # Output: 1
2. Slicing in Lists
Slicing is used to extract multiple elements from a list.
The syntax for slicing is:
list[start:end:step]
start: The index where slicing begins (inclusive).
end: The index where slicing stops (exclusive).
step: Defines how many elements to skip (optional).
Example:
numbers = [10, 20, 30, 40, 50, 60, 70]
6. Write a python program to input any two tuples and interchange the tuple variables.
# Input two tuples from the user
# Swapping tuples
tuple1, tuple2 = tuple2, tuple1
2. remove()
• Removes a specific element from the set.
• Raises an error if the element is not found.
• Syntax: set.remove(element)
Example:
s = {1, 2, 3, 4}
s.remove(3)
print(s) # Output: {1, 2, 4}
3. discard()
• Removes an element from the set if it exists.
• Does not raise an error if the element is missing.
• Syntax: set.discard(element)
Example:
Note: AI Can Make Mistakes
s = {1, 2, 3, 4}
s.discard(3)
print(s) # Output: {1, 2, 4}
s.discard(10) # No error even though 10 is not in the set
4. union()
• Returns a new set containing all elements from both sets.
• Syntax: set1.union(set2)
Example:
s1 = {1, 2, 3}
s2 = {3, 4, 5}
result = s1.union(s2)
print(result) # Output: {1, 2, 3, 4, 5}
5. intersection()
• Returns a new set with common elements of both sets.
• Syntax: set1.intersection(set2)
Example:
s1 = {1, 2, 3}
s2 = {2, 3, 4}
result = s1.intersection(s2)
print(result) # Output: {2, 3}
6. difference()
• Returns a new set with elements only in the first set, not in the second.
• Syntax: set1.difference(set2)
Example:
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5}
result = s1.difference(s2)
print(result) # Output: {1, 2}
2. remove()
• Removes a specific element from the set.
• Raises an error if the element is not found.
• Syntax: set.remove(element)
Example:
s = {1, 2, 3, 4}
s.remove(3)
print(s) # Output: {1, 2, 4}
3. discard()
• Removes an element from the set if it exists.
• Does not raise an error if the element is missing.
• Syntax: set.discard(element)
Example:
s = {1, 2, 3, 4}
s.discard(3)
print(s) # Output: {1, 2, 4}
s.discard(10) # No error even though 10 is not in the set
4. union()
• Returns a new set containing all elements from both sets.
• Syntax: set1.union(set2)
Example:
s1 = {1, 2, 3}
5. intersection()
• Returns a new set with common elements of both sets.
• Syntax: set1.intersection(set2)
Example:
s1 = {1, 2, 3}
s2 = {2, 3, 4}
result = s1.intersection(s2)
print(result) # Output: {2, 3}
6. difference()
• Returns a new set with elements only in the first set, not in the second.
• Syntax: set1.difference(set2)
Example:
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5}
result = s1.difference(s2)
print(result) # Output: {1, 2}