Python Question Bank Answers
Python Question Bank Answers
Unit 1
3 In this line, the program is trying to change the second character of the string 'hello' to 'bye', but strings are immutable in
Python, meaning that their values cannot be modified once created. Attempting to change a value in a string will raise a
"TypeError: 'str' object does not support item assignment" error.
The second line:
x[0] *= 3
is valid and will work as expected, it will multiply the first element in the list by 3, which is '12', it will change the first
element of the list to '121212'
Ques: Explain why Python is considered an interpreted language.
Ans: Python is considered an interpreted language because the source code of a Python program is passed through an
interpreter, which reads and executes the code line by line, rather than being translated into machine code by a compiler before
execution.
In contrast, compiled languages such as C or C++, require that the source code is first translated into machine code by a
compiler before it can be executed. The machine code is then executed by the computer.
4 The main advantage of interpreted languages is that they are generally easier to write and debug because you can see the
results of your code as soon as you run it, without the need to wait for it to be compiled. Additionally, interpreted languages
are often more portable, since the same source code can be run on multiple platforms with only a change in interpreter.
Furthermore, Python's interpreter is able to change the program while running it, which is not possible in compiled languages.
This enables the developer to test the code and make changes on the fly, making the debugging process more efficient.
In summary, Python is considered an interpreted language because it does not require compilation, the source code is executed
directly by an interpreter, making the development process more efficient and portable.
Ques: What is short circuit evaluation? What is printed by the following Python program?
a=0
b=2
c=3
x = c or a
print(x)
Ans: Short-circuit evaluation is a feature in some programming languages where the interpreter evaluates the left side of a
logical operator (such as "and" or "or") first and if it determines that the result of the expression is already known based on the
left side, it does not evaluate the right side.
5
In the given Python program, the variable x is assigned the value of c or a. The operator or is a logical operator that returns
the first true value it encounters. Since c is equal to 3, which is a true value, the interpreter does not evaluate the right side of
the "or" operator.
Therefore, the program will print out 3.
In summary, short-circuit evaluation is a feature that allows the interpreter to stop evaluating an expression as soon as it has
enough information to determine the outcome. The Python program will print 3 because c is assigned 3 which is a True value,
so the interpreter does not evaluate the right side of the or operator and assigns the value of c to x.
Ques: Explain the Programming Cycle for Python in detail.
Ans: Planning: Before writing any code, it's important to plan what the program should do and how it will accomplish its
goals. This can involve creating flowcharts, diagrams, or pseudocode to help visualize the program's structure and logic.
Writing the code: After the planning is complete, the next step is to write the actual code. This can be done using a text editor
or an integrated development environment (IDE) such as PyCharm, IDLE or Visual Studio Code. The code should be written
in a clear and organized manner, making use of comments and proper indentation to make it easy to read and understand.
Debugging: As soon as you have written the code, the next step is to debug it. This involves running the code and checking
for any syntax errors, logical errors, or other mistakes that may prevent the program from running correctly. You can use the
built-in Python debugging tools such as pdb, or use the debugging features of the IDE you are using.
Testing: After debugging, the next step is to test the program to ensure it behaves as expected and meets the requirements.
This can involve writing test cases, and running the program with different inputs and comparing the output with the expected
6
results.
Deployment: Once the program is tested and debugged, it is ready to be deployed. This can involve packaging the code for
distribution, documenting the code, and preparing it for use in the target environment.
Maintenance: After deployment, the program will require maintenance to fix any bugs that are found, update the program to
meet changing requirements, and ensure that it continues to run smoothly.
The programming cycle is an iterative process, meaning that the program may need to go through several rounds of
debugging, testing, and maintenance before it is ready for deployment.
It's worth mentioning that in the industry, the cycle may vary depending on the type of project, the team working on it, and the
methodology they are following. However, the steps described above are the general process of creating a software using
Python.
IDLE: IDLE is the default IDE that comes with Python. It is a simple, lightweight IDE that is suitable for beginners.
Visual Studio Code: Visual Studio Code is a popular, open-source IDE that supports a wide range of programming languages
including Python. It has a large user community and lots of extensions available to customize the IDE for specific needs.
Spyder: Spyder is an open-source IDE for scientific computing and data science. It has built-in support for NumPy and
Pandas, and is often used for data analysis and visualization.
Eclipse with PyDev: Eclipse is an open-source IDE that is widely used for Java development. PyDev is an Eclipse plugin that
adds support for Python development.
Using an IDE can greatly improve the efficiency and productivity of Python development. It also makes it easier to write, test
and debug your code, and it can help you write clean and maintainable code with features like code completion, refactoring
and code analysis.
Ques: Discuss why Python is called as dynamic and strongly typed language?
Ans:
Python is called a dynamic language because its type checking is done at runtime, which means the type of a variable can
change during the execution of a program. For example, consider the following code:
x = 10
print(x) # prints 10
x = "hello"
print(x) # prints hello
In this example, the variable x is initially set to an integer value of 10, but later it's set to a string value "hello". This is possible
because Python is a dynamic language, and the interpreter will automatically adjust to the new type, in this case, from integer
to string.
8
On the other hand, Python is called a strongly typed language because the interpreter strictly enforces the types of variables.
This means that when you try to perform an operation on a variable with an incompatible type, Python will raise a type error,
preventing the operation from being executed.
For example, in Python, if you try to add a number to a string, the interpreter will raise a TypeError, because you can't add two
different types together.
x = "hello"
y = 10
z = x + y # raises TypeError
In this example, x is a string and y is an integer and python does not allow adding a string to an integer, and raises a
TypeError. This is an example of strong type checking in Python.
Ques: What do you mean by operator precedence and associativity? Explain.
Ans:
In Python, operator precedence refers to the order in which operations are performed in an expression. Operators with higher
precedence are evaluated before operators with lower precedence.
For example, in the expression "2 + 3 * 4", the multiplication operator (*) has higher precedence than the addition operator
(+).
9 So the multiplication is done first, resulting in the expression being evaluated as "2 + 12" rather than "6 * 4".
Associativity refers to the order in which operators of the same precedence are evaluated.
In Python, operators can be either left-associative or right-associative.
Left-associative operators are evaluated from left to right, while right-associative operators are evaluated from right to left.
For example, in the expression "2 ** 3 ** 4", the exponentiation operator ** is right-associative,
So the expression is evaluated as "2 ** (3 ** 2)", which equals to 512.
Ans:
print("Before swapping:")
print("First number:", num1)
print("Second number:", num2)
print("After swapping:")
print("First number:", num1)
print("Second number:", num2)
Output:
Enter first number: 50
Enter second number: 100
Before swapping:
First number: 50
Second number: 100
After swapping:
First number: 100
Second number: 50