[go: up one dir, main page]

0% found this document useful (0 votes)
12 views10 pages

PWP Question Bank

The document is a question bank for Python programming, containing both 2-mark and 4-mark questions. It covers various topics such as features of Python, control flow structures, data types, and operations on tuples. Each question is followed by a detailed answer, providing examples and explanations relevant to Python programming concepts.

Uploaded by

shreyagowda206
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)
12 views10 pages

PWP Question Bank

The document is a question bank for Python programming, containing both 2-mark and 4-mark questions. It covers various topics such as features of Python, control flow structures, data types, and operations on tuples. Each question is followed by a detailed answer, providing examples and explanations relevant to Python programming concepts.

Uploaded by

shreyagowda206
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/ 10

PWP Question Bank

2 Marks Questions

Q1: List the features of Python.

Answer:

1.​ Interactive: Python provides an interactive shell for testing and debugging.
2.​ Object-Oriented: Supports object-oriented programming concepts like
inheritance and polymorphism.
3.​ Interpreted: No need to compile; the Python interpreter directly executes
the code.
4.​ Platform-Independent: Can run on different operating systems without
modifications.

Q2: What is the use of indentation in Python?

Answer:​
Indentation in Python defines the structure of the code. Instead of braces ({})
used in other languages, Python uses indentation to indicate blocks of code.​
Example:

if True:
print("Indented block is part of 'if'")
print("This is outside the 'if' block")

Q3: Define keywords in Python with an example.

Answer:​
Keywords are reserved words in Python that have predefined meanings and
cannot be used as variable names.​
Examples: if, else, return, for, while.​
Example usage:

if True:
print("This is a keyword example")
Q4: List identity operators in Python.​
Answer:​
Identity operators in Python are:

is: Returns True if two variables point to the same object.

is not: Returns True if two variables point to different objects.

Q5: Give membership operators in Python.​


Answer:​
Membership operators in Python are:

in: Returns True if a value exists in a sequence.

not in: Returns True if a value does not exist in a sequence.

Q6: What are the main control flow structures in Python?​


Answer:​
The main control flow structures in Python are:

Conditional Statements (if, elif, else).

Loops (for, while).

Control Flow Statements (break, continue, pass).

Exceptions (try, except, else, finally).

Q.7.Give two differences between list and tuple


Q8: What is a dictionary?
Answer:
A dictionary is an unordered collection of key-value pairs where each key is unique.
Values can be of any data type.
Example:
my_dict = {"name": "Alice", "age": 25}

Q9: What are tuples in Python? ​


Answer:​
Tuples are immutable, ordered collections of elements. They allow storage of multiple
items in a single variable.​
Example:

my_tuple = (1, 2, 3)

4 Marks Questions

Q1: Explain the building blocks of Python with examples.

Answer:

Identifiers: Names for variables, functions, etc.​


Example:​
my_variable = 10

Keywords: Reserved words like if, else, for, etc.​


Example:​
for i in range(5):
print(i)

Indentation: Used to define blocks of code.​


Example:​
if True:
print("Inside block")
Variables: Names that store data values.​
Example:​
x = 10

Comments: Lines ignored by the interpreter, used for explanation.​


Example:​
# This is a comment

Q2: List the data types in Python and explain any two with examples.

Answer:

Numbers: Represent integers and floating-point numbers.​


Example:​
x = 10 # Integer
y = 20.5 # Float

Strings: Represent a sequence of characters enclosed in quotes.​


Example:​
name = "Alice"
print(name) # Output: Alice

1.​ Tuples: Immutable collections of elements.


2.​ Lists: Mutable collections of elements.
3.​ Dictionaries: Collections of key-value pairs.

Q3: Write simple Python program using operators:


a) Arithmetic Operators
b) Logical Operators
c) Bitwise Operators

a) Arithmetic Operators
# Taking two numbers as input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Displaying the results directly


print(f"Addition: {num1 + num2}")
print(f"Subtraction: {num1 - num2}")
print(f"Multiplication: {num1 * num2}")
print(f"Division: {num1 / num2}")
print(f"Floor Division: {num1 // num2}")
print(f"Modulus: {num1 % num2}")
print(f"Exponentiation: {num1 ** num2}")

b) Logical Operators
# Taking two boolean values as input
x = bool(int(input("Enter 1 for True or 0 for False (x): ")))
y = bool(int(input("Enter 1 for True or 0 for False (y): ")))

# Displaying the results of logical operations directly


print(f"x AND y: {x and y}")
print(f"x OR y: {x or y}")
print(f"NOT x: {not x}")
print(f"NOT y: {not y}")

c) Bitwise Operators
# Taking two numbers as input
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

# Displaying bitwise operation results directly


print(f"AND: {num1 & num2}")
print(f"OR: {num1 | num2}")
print(f"XOR: {num1 ^ num2}")
print(f"NOT {num1}: {~num1}")
print(f"NOT {num2}: {~num2}")
Q4: - Write python program to demonstrate use of looping statements:

a) 'while' loop
b) 'for' loop
c) Nested loops

Answer:​
a) while loop:

b) for loop:

c) Nested loops:

Q5: Explain decision-making statements (if-else, if-elif-else) with examples.​


Answer:

if Statement: Executes a block if the condition is True.​


Example:​
if 5 > 3:
print("5 is greater than 3")
if-else Statement: Executes one block if the condition is True, otherwise another
block.​
Example:​

if-elif-else Statement: Used to test multiple conditions.​


Example:​

Q.6. Describe control flow statements in Python.

Answer:​
Control flow statements alter the flow of a program:

1.​ Break: Terminates the loop.​


Example:
2. Continue: Skips the current iteration.
Example:

3. Pass: A placeholder for future code.​


Example:

Q7: Explain indexing and slicing in lists with an example. (4 mks)


Answer:
Q8: Explain any four set operations with examples. (4 mks)
Answer:
Q.9. - Write python program to perform following operations on Tuples:
a) Create Tuple
b) Access Tuple
c) Update Tuple
d) Delete Tuple

# a) Create Tuple
my_tuple = (1, 2, 3, 4, 5)
print("Created Tuple:", my_tuple)

# b) Access Tuple
print("First element:", my_tuple[0])
print("Last element:", my_tuple[-1])

# c) Update Tuple
# Since tuples are immutable, we can't modify them directly.
# But we can create a new tuple by combining or slicing.
new_tuple = my_tuple + (6,) # Adding 6 to the tuple
print("New tuple after adding 6:", new_tuple)

# d) Delete Tuple
del my_tuple

# Trying to print the deleted tuple (will raise an error)


try:
print("Deleted Tuple:", my_tuple)
except NameError:
print("The tuple has been deleted.")

Output -
Created Tuple: (1, 2, 3, 4, 5)
First element: 1
Last element: 5
New tuple after adding 6: (1, 2, 3, 4, 5, 6)
The tuple has been deleted.

You might also like