0% found this document useful (0 votes)
17 views14 pages

PWP QB Answers

The document provides a comprehensive overview of Python programming concepts, including features, comments, indentation, membership and identity operators, and comparisons between data types like lists, tuples, and dictionaries. It also includes practical programming exercises, such as checking for palindromes, counting digits, and performing set operations. Additionally, the document explains the use of if-else statements and built-in functions, along with sample outputs for various code snippets.
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)
17 views14 pages

PWP QB Answers

The document provides a comprehensive overview of Python programming concepts, including features, comments, indentation, membership and identity operators, and comparisons between data types like lists, tuples, and dictionaries. It also includes practical programming exercises, such as checking for palindromes, counting digits, and performing set operations. Additionally, the document explains the use of if-else statements and built-in functions, along with sample outputs for various code snippets.
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
You are on page 1/ 14

2 marks questions:


1) Enlist any 4 features of python.
Ans: Features of Python are:(any 4)
1.​ Easy to code
2.​ Free and Open source
3.​ Object Oriented Language
4.​ GUI Programming language
5.​ High level language
6.​ Portable Language
7.​ Interpreted Language
8.​ Large Standard Library
9.​ Dynamically Typed Language
10.​Integrated Language

2) Describe the comments in python.


Ans:
●​ Python Comment is an essential tool for the programmers. Comments are
generally used to explain the code.
●​ We can easily understand the code if it has a proper explanation.
●​ A good programmer must use the comments because in the future anyone
wants to modify the code as well as implement the new module; then, it
can be done easily.
●​ Python provides the single-lined Python comment.
●​ To apply the comment in the code we use the hash(#) at the beginning of
the statement or code like:
# This is the print statement
print("Hello Python")
●​ To write a multiline comment in Python, you simply need to enclose your
text within triple quotes.
Here's an example:
''' This is a multiline comment It spans multiple lines Python will ignore
these lines when executing
the code '''
3) State the use of indentation in python.
Ans:
●​ Indentation is a very important concept of Python because without
properly indenting the Python code,you will end up seeing Indentation
Error and the code will not get compiled.
●​ Python indentation refers to adding white space before a statement to a
particular block of code.
●​ In another word, all the statements with the same space to the right,
belong to the same code block.
●​ To indicate a block of code in Python, you must indent each line of the
block by the same whitespace.
●​ The two lines of code in the while loop are both indented four spaces.
●​ It is required for indicating what block of code a statement belongs to.
●​ For example, Python code structures by indentation:
Code:-
j=1
while(j<= 5):
print(j)
j=j+1

4) Describe membership operators in python.


Ans:
●​ Membership operators check whether a value is in another.
●​ Python has 2 membership operators:
1.​ In: Returns True if a sequence with the specified value is present in
the object.
2.​ Not in: Returns True if a sequence with the specified value is not
present in the object.
​ Example:
5) Compare list and tuple (any 4 points).
Ans:
List Tuple

List is mutable. Tuple is immutable.

We can change the elements of list We cannot change the elements of


once it is created. tuple once it is created.

Enclosed within square brackets “[]” Enclosed within parentheses “()”

Syntax: list_name = [element1, Syntax: tuple_name = (element1,


element2,...element n] element2,...element n)

Eg: list1 = [1, 2, 3, 4, 5] Eg: tuple1 = (1, 2, 3, 4, 5)

6) Write steps to install and to run the python code.


Ans: Steps to install python:
Step 1: Visit the link https://www.python.org/downloads/ to download the latest
release of Python
Step 2:Select the Python's version to download.
Click on the download button.
Step 3: Click Install Now
Wait for installation to complete
Step 4: Verify Python installation by typing “python –version” on the cmd prompt.
Step 5: Open IDLE

Steps to Execute Program using Interactive and Script Mode :-


7) Compare interactive mode and script mode( any 4 points).
Ans:

8) List membership and identity operator.


Ans: Mostly Example not required but then too if time is there then try to
write

Membership operator:
●​ Membership operators check whether a value is in another.
●​ Python has 2 membership operators:
1.​ In: Returns True if a sequence with the specified value is present in
the object.
2.​ Not in: Returns True if a sequence with the specified value is not
present in the object.
​ Example:
Identity operator:
●​ Identity operators check whether two values are identical.
●​ Python has 2 identity operators as well::
1.​ Is: checks if two operands refer to the same object in memory,
returning True if they are the same, and False otherwise.
2.​ Is not: The is not operator in Python checks if two operands do not
refer to the same object in memory, returning True if they are
different, and False if they are the same.
​ Example:
9) Explain if else ladder.
Ans:

●​ The if-elif-else ladder is used to execute different blocks of code based on


multiple conditions.
●​ The program checks conditions from top to bottom; once a true condition
is found, its block executes, skipping the rest.
●​ If no conditions are true, the else block runs.
●​ This structure helps in decision-making by allowing multiple conditions to
be checked sequentially.
●​ The general syntax of an if-elif-else ladder will be:
if <Condition 1>:
<Execute this code block>
elif <Condition 2>:
<Execute this code block>.
elif <Condition X>:
<Execute this code block>
else:
<Execute this code block>
●​ Eg:​ num = int(input("Enter a number: "))
if num > 0:
​ ​ print("The number is positive.")
elif num < 0:
​ ​ print("The number is negative.")
else:
​ ​ print("The number is zero.")

10) Define mutable and immutable data type.

Mutable Data Types

Definition: Objects whose values can be changed after creation


are known as Mutable data types. These allow modification without
changing their identity.​
Types: list, dict, set, bytearray
Immutable Data Types

Definition: Objects whose values cannot be changed after


creation are known as Non- mutable data types. Any modification
creates a new object.​
Types: str, tuple, int, float, bool, bytes

11) Compare List and dictionary(4 points)

Difference Between List and Dictionary

List Dictionary

1. Uses index to access elements. 1. Uses keys to access values.

2. Allows duplicate values. 2. Keys must be unique, but values can


be duplicated.

3. Stores elements in an ordered 3. Stores elements as key-value pairs.


sequence.

4. Searching is slower (O(n) in worst 4. Searching is faster (O(1) on average).


case).

5. Supports slicing and indexing. 5. Does not support slicing.

6. Syntax: my_list = [1, 2, 3] 6. Syntax: my_dict = {"a": 1,


"b": 2}

12) Write the output for the following if the variable course="Programming"
>>>course[:4]
Prog
>>>course[2:]
ogramning
>>>course[2:3]
o
>>>course[-1]
g
4 mark Questions:
1)​ Write a program that takes a number and checks whether it is palindrome.
number = int(input("Enter a number: "))
original_number = number
reversed_number = 0
while number > 0:
digit = number % 10
reversed_number = (reversed_number * 10) + digit
number //= 10
if original_number == reversed_number:
print(original_number, "is a palindrome.")
else:
print(original_number, "is not a palindrome.")

O/P: ​ Enter a number: 123321


123321 is a palindrome.

2)​ Write a program to count the total number of digits in a number.


number = int(input("Enter a number: "))
original_number = number
count = 0
while number > 0:
number //= 10
count += 1
print("Number of digits: ", count)

O/P: ​ Enter a number: 1


Number of digits: 1

3)​ Write a program to find the greatest among 4 numbers.


num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
num4 = int(input("Enter number 4: "))
if num1>num2 and num1>num3 and num1>num4:
print(num1, "is the greatest")
elif num2>num1 and num2>num3 and num2>num4:
print(num2, "is the greatest")
elif num3>num2 and num3>num1 and num3>num4:
print(num3, "is the greatest")
else:
print(num4, "is the greatest")

OR

num1 = int(input("Enter number 1: "))


num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
num4 = int(input("Enter number 4: "))
if num1>num2 and num1>num3 and num1>num4:
print(num1, "is the greatest")
elif num2>num1 and num2>num3 and num2>num4:
print(num2, "is the greatest")
elif num3>num2 and num3>num1 and num3>num4:
print(num3, "is the greatest")
elif num4>num1 and num4>num2 and num4>num3:
print(num4, "is the greatest")
else:
print("All are Equal")

O/P: ​ Enter number 1: 5


Enter number 2: 2
Enter number 3: 7
Enter number 4: 1
7 is the greatest

4)​ Write a program to explain 4 built-in functions of set in python.


#Creating Sets
S1 = { 1, 2, 3, 4, 5, 6, 7}
S2 = { 2, 5, 7, 8, 9, 10}

#Union
U = S1|S2
print ("Union = ", U)
#Intersection
I = S1&S2
print ("Intersection = ", I)

#Difference
D = S1-S2
print ("Difference = ", D)

#Symmetric Difference
S = S1^S2
print ("Symmetric Difference= ", S)

Output : -
Union = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Intersection = {2, 5, 7}
Difference = {1, 3, 4, 6}
Symmetric Difference= {1, 3, 4, 6, 8, 9, 10}

Draw Diagram for each 4 Operational Functions

5)​ Write a program to print following pattern using loop:


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
​ for i in range (1,6):
​ ​ for j in range(0,i):
​ ​ ​ print(i,end=" ")
​ ​ print()

6)​ Write a program to perform set operations:


1. Create a set of five elements.
2. Access set elements
3. Update set by adding one element.
4. Remove one element from the set.

#create set of 5 elements


set1 = {6,3,8,1,5}
#access set elements
print("Original set: ",end='')
for i in set1:
print(i, end=" ")
print()
#update set by adding element
set1.add(10)
print("Set after adding element:", set1)

#remove elements
set1.remove(10)
print("Set after removing:", set1)

​ O/P: ​ Original set: 1 3 5 6 8


Set after adding element: {1, 3, 5, 6, 8, 10}
Set after removing: {1, 3, 5, 6, 8}

7)​ Write a program to create a dictionary of students that includes their roll no and
name.
1. Add 4 students in the above dictionary.
2. Update name= "Priya" of roll no 2
3. Delete info of roll no=1
4. Print only keys then values and data in key:value pair.

# 1. Create a dictionary of students with roll no and name


students = {
1: "ABC",
2: "PQR",
3: "XYZ",
4: "DEF"
}

print("Original dictionary:", students)

# 2. Update name to "Priya" for roll no 2


students[2] = "Priya"
print("Dictionary after updating roll no 2:", students)
# 3. Delete info of roll no 1
del students[1]
print("Dictionary after deleting roll no 1:", students)

# 4. Print only keys, then values, and finally data in key:value pair

# Print only keys


print("Keys in the dictionary:", end=" ")
for key in students.keys():
print(key, end=" ")
print()

# Print only values


print("Values in the dictionary:", end=" ")
for value in students.values():
print(value, end=" ")
print()

# Print key:value pairs


print("Key:Value pairs in the dictionary:")
print(students)

O/P: ​ Original dictionary: {1: 'ABC', 2: 'PQR', 3: 'XYZ', 4: 'DEF'}


Dictionary after updating roll no 2: {1: 'ABC', 2: 'Priya', 3: 'XYZ', 4: 'DEF'}
Dictionary after deleting roll no 1: {2: 'Priya', 3: 'XYZ', 4: 'DEF'}
Keys in the dictionary: 2 3 4
Values in the dictionary: Priya XYZ DEF
Key:Value pairs in the dictionary:
{2: 'Priya', 3: 'XYZ', 4: 'DEF'}

8)​ Write the output of the following:


>>>t1=[3,4,6,10,5,9]
>>>print(t1[1])
>>>print(t1[-1])
>>>print(t1[3:])
>>>print(t1[:])

t1=[3,4,6,10,5,9]
O/P: #it will create a list t1, which is a list of integers
print(t1[1])
O/P: 4

print(t1[-1])
O/P: 9

print(t1[3:])
O/P: [10, 5, 9]

print(t1[:])
O/P: [3, 4, 6, 10, 5, 9]

You might also like