0% found this document useful (0 votes)
2 views25 pages

Python Question Bank Solution PA-1

The document is a Python question bank that covers various fundamental concepts including features of Python, variable declaration, comments, data types, string manipulation, operators, control flow statements, and data structures. Each section provides definitions, examples, and explanations to help understand the topics effectively. It serves as a comprehensive guide for beginners and those looking to reinforce their Python programming knowledge.

Uploaded by

virajkurhadeteam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views25 pages

Python Question Bank Solution PA-1

The document is a Python question bank that covers various fundamental concepts including features of Python, variable declaration, comments, data types, string manipulation, operators, control flow statements, and data structures. Each section provides definitions, examples, and explanations to help understand the topics effectively. It serves as a comprehensive guide for beginners and those looking to reinforce their Python programming knowledge.

Uploaded by

virajkurhadeteam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 25

Python

Python Question Bank


P.A. Test–1
Q.1 List the features of Python and explain any two?

=> Python is a high-level, interpreted programming language known for its simplicity and versatility. Some
key features of Python include:

• Easy to Learn – Python has a simple syntax that resembles natural language, making it easy for
beginners.

• Interpreted – Python code is executed line by line, eliminating the need for compilation.

• Platform Independent – Python programs can run on different operating systems without
modification.

• Cross-Platform and Portable – Code written in Python can be executed on multiple platforms like
Windows, Linux, and macOS.

• Large Standard Library – Python provides a rich set of built-in libraries for various applications,
reducing the need for external dependencies.

• Free and Open Source – Python is freely available and open source, meaning its source code can be
modified and distributed.

• Auto Destructive (Automatic Garbage Collection) – Python automatically manages memory using
garbage collection to free up unused memory.

• Support Exception Handling – Python has built-in support for exception handling to manage
runtime errors effectively.

Page 1 of 25
Python

Q.2 What do you mean by variable? Does Python allow explicit declaration of variables? Justify your
answer.

=> What is a Variable?

A variable is a name given to a memory location where a value is stored. In Python, variables are used to
store data like numbers, text, or lists. Python has some rules for naming variables:

a. The name can be a combination of letters (a-z, A-Z), digits (0-9), and the underscore (_) symbol.
b. The name cannot start with a digit.
c. Whitespace is not allowed in variable names.
d. Variable names are case-sensitive, meaning Age and age are different variables.
Does Python Allow Explicit Declaration of Variables?

No, Python does not require explicit declaration of variables before using them.

Justification

In many programming languages like C, C++, and Java, you must declare a variable with a data type before
using it (e.g., int num = 10;). However, in Python, variables are dynamically typed, meaning:

• You can create a variable just by assigning a value to it (e.g., num = 10).

• Python automatically determines the data type based on the assigned value.

• You can change the type of a variable by assigning a new value (e.g., num = "Hello" changes num
from an integer to a string).

Example in Python

x=5 # x is an integer

y = "Hello" # y is a string
x = 3.14 # Now x is a float

Since Python does not require explicit declarations, it provides flexibility but also requires careful handling
of variable types.

Page 2 of 25
Python

Q. 3 What is a Comment? How to Apply Comments in Python?

A comment is a statement in a program that is ignored by the interpreter—it is neither compiled nor
executed. Comments are used to provide extra information about the code, making it easier to understand.

Types of Comments in Python

1. Single-Line Comment (#)


o A single-line comment provides additional information about a specific statement in just one
line.
o It starts with the # symbol and extends until the end of the line.

Example:

# This is a single-line comment


x = 10 # This assigns 10 to variable x

2. Multi-Line Comment (''' or """)


o A multi-line comment spans across multiple lines.
o It can be written using triple single quotes (''') or triple double quotes (""").

Example:

'''
This is a
multi-line comment
using single quotes.
'''
"""
This is a
multi-line comment
using double quotes.
"""

Comments improve code readability, making it easier for others (or even yourself) to understand the logic of
the program later.

Page 3 of 25
Python

Q.4 Explain numeric data types in Python.

=> Numeric Data Types in Python

In Python, numeric data types are used to store numbers. Python supports three main types of numeric
data:

1. Integer (int)

o Stores whole numbers (positive, negative, or zero).


o No decimal point is allowed.

o Example:

a = 10 # Positive integer

b = -5 # Negative integer

c=0 # Zero

2. Floating-Point (float)
o Stores numbers with a decimal point.

o Can also store scientific notation (e.g., 2.5e3 means 2500).


o Example:

x = 3.14 # Floating-point number

y = -0.75 # Negative float

z = 2.5e3 # Scientific notation (2500.0)

3. Complex (complex)

o Stores numbers with a real and imaginary part.


o Represented as a + bj, where j is the imaginary unit.
o Example:

num1 = 2 + 3j # Complex number with real part 2 and imaginary part 3j

num2 = -4j # Purely imaginary number

Conclusion

Python provides three numeric data types: int (whole numbers), float (decimal numbers), and complex
(numbers with an imaginary part). These data types are widely used in calculations, programming logic, and
scientific computations.

Page 4 of 25
Python

Q.5 Explain the use of strings in Python. Also explain string operators

=> Use of Strings in Python

A string in Python is a sequence of characters enclosed in single ('), double ("), or triple quotes (''' or
""""). Strings are used to store and manipulate text data.

Python allows various operations on strings based on their index numbers, starting from 0 for the first
character.

Example of String Operations in Python

str = 'Welcome to Python'

print(str) # Prints the complete string

print(str[0]) # Prints the first character ('W')

print(str[2:7]) # Prints substring from index 2 to 6 ('lcome')

print(str[4:]) # Prints substring from index 4 to end ('ome to Python')

print(str * 3) # Repeats the string 3 times

print(str + ' programming') # Concatenates with another string

String Operators in Python

Python provides several operators for string manipulation:

1. Concatenation Operator (+)

o Joins two or more strings.


o Example:

o str1 = "Hello"

o str2 = " World"

o print(str1 + str2) # Output: Hello World


2. Repetition Operator (*)

o Repeats a string n times.

o Example:

o str1 = "Hi "

o print(str1 * 3) # Output: Hi Hi Hi
3. Slicing Operator ([] or [:])

o Extracts a portion of a string using index numbers.


o Example:
Page 5 of 25
Python

o str1 = "Python"

o print(str1[1:4]) # Output: yth

4. Membership Operators (in, not in)

o Checks if a substring exists within a string.


o Example:

o print("Py" in "Python") # Output: True

o print("abc" not in "Python") # Output: True

Conclusion

Strings in Python are widely used for text processing and data manipulation. Python provides several
operators like + (concatenation), * (repetition), and slicing ([:]) to handle string operations effectively.

Page 6 of 25
Python

Q. 6 Explain arithmetic operators in Python with an example.

Arithmetic operators in Python are used to perform basic mathematical operations such as addition,
subtraction, multiplication, division, and more. These operators work with numerical values (integers and
floats) to perform calculations.

Code:

# Defining two numbers

a = 10

b = 3

# Performing arithmetic operations

print("Addition:", a + b)

print("Subtraction:", a - b)

print("Multiplication:", a * b)

print("Division:", a / b)

print("Floor Division:", a // b)

print("Modulus:", a % b)

print("Exponentiation:", a ** b)

Output:

Addition: 13

Subtraction: 7

Multiplication: 30

Division: 3.3333333333333335
Page 7 of 25
Python

Floor Division: 3

Modulus: 1

Exponentiation: 1000

Page 8 of 25
Python

Q. 7 Explain bitwise operators in Python with an example.

Bitwise operators in Python perform operations at the bit level. These operators work on binary
representations of integers, manipulating individual bits. They are useful in low-level programming,
cryptography, and optimization tasks.

Code:

# Defining two numbers

a = 5 # Binary: 101

b = 3 # Binary: 011

# Performing bitwise operations

print("Bitwise AND:", a & b)

print("Bitwise OR:", a | b)

print("Bitwise XOR:", a ^ b)

print("Bitwise NOT of a:", ~a)

print("Left Shift (a << 1):", a << 1)

print("Right Shift (a >> 1):", a >> 1)

Output:

Bitwise AND: 1

Bitwise OR: 7

Bitwise XOR: 6
Page 9 of 25
Python

Bitwise NOT of a: -6

Left Shift (a << 1): 10

Right Shift (a >> 1): 2

Page 10 of 25
Python

Q.8 Explain membership operator and identity operators in Python with examples.

Membership and Identity Operators in Python

Python provides Membership Operators and Identity Operators for checking membership in sequences
and comparing object identities.

Code:

# Membership operator with string

print("a" in "apple") # True

print("z" not in "apple") # True

# Membership operator with list

numbers = [1, 2, 3, 4, 5]

print(3 in numbers) # True

print(10 not in numbers) # True

Output:

True

True

True

True

Page 11 of 25
Python

Q.9 Explain if-else statement with example.

Example Program:
# Check if a number is positive, negative, or zero

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

if num > 0:

print("The number is positive.")

else:

print("The number is not positive.")

Output:
Enter a number: -5

The number is not positive.

Page 12 of 25
Python

Q.10 Show the use of keyword elif in Python with example.

Example:

# Checking the grade based on marks

marks = int(input("Enter your marks: "))

if marks >= 90:

print("Grade: A")

elif marks >= 80:

print("Grade: B")

elif marks >= 70:

print("Grade: C")

elif marks >= 60:

print("Grade: D")

else:

print("Grade: F (Fail)")

Output:
Enter your marks: 85

Grade: B
Page 13 of 25
Python

Page 14 of 25
Python

Q. 11 How to use short-hand if-else statement in Python. Explain with example.

Example:

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

result = "Even" if num % 2 == 0 else "Odd"

print(result)

Output:

Enter a number: 7

Odd

Page 15 of 25
Python

Q. 12 Explain for-loop in Python with example.

Example:
fruits = ["Apple", "Banana", "Cherry"]

for fruit in fruits:

print(fruit)

Output:

Apple

Banana

Cherry

Page 16 of 25
Python

Q.13 Can we use keyword else with any loop? Justify your answer.

Python allows the use of the else keyword with both for and while loops. The else block runs only if the loop
completes all iterations without encountering a break statement. This feature is useful for executing code
after a successful loop completion, such as confirming that a search operation did not find a match.

However, if the loop is interrupted by a break statement, the else block does not execute. This makes it
helpful in scenarios like searching for an item in a list, where the else block can handle cases where no
match is found. This behavior differentiates Python’s loop-else construct from the traditional else used in
conditional statements.

Page 17 of 25
Python

Q.14 State the use of keyword pass.

Page 18 of 25
Python

Q. 15 Explain the use of keywords break and continue in Python with example.

The break statement terminates the loop immediately when encountered and exits the loop. The code after
the loop continues execution.

Page 19 of 25
Python

Page 20 of 25
Python

Q.16 Explain Following python data types with example.

a) List

b) Tuple c) Dictionary

A List in Python is an ordered, mutable collection that can store different data types such as integers, strings,
and even other lists. Lists allow modification by adding, removing, or updating elements. They are defined
using square brackets [], and elements are separated by commas.

A Tuple is similar to a list but is immutable, meaning its elements cannot be changed after creation. Tuples
are defined using parentheses () and are useful when data should remain constant throughout program
execution. Since tuples are immutable, they offer better performance compared to lists.

A Dictionary is an unordered collection of key-value pairs, where each key is unique. It allows fast retrieval
of values based on keys, making it ideal for mapping relationships between data. Dictionaries are defined
using curly braces {} with keys and values separated by colons

Key Points:

• List: Mutable, ordered collection that allows adding, modifying, and removing elements.

• Tuple: Immutable, ordered collection that is faster and consumes less memory than lists.

• Dictionary: Unordered key-value collection that enables efficient data retrieval and mapping

Page 21 of 25
Python

Q.17 Name different modes of python.

Page 22 of 25
Python

Q.18 Explain range function with example.

The range() function in Python generates a sequence of numbers. It is mainly used in loops to iterate over a
sequence of numbers efficiently.

Page 23 of 25
Python

Page 24 of 25
Python

Q.19 Write python program for addition of two number to read data from user by using input
function.

# Taking input from the user

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

# Performing addition

sum_result = num1 + num2

# Displaying the result

print("The sum is:", sum_result)

Page 25 of 25

You might also like