Python 20 Important 20 Question
Python 20 Important 20 Question
1. What is an Algorithm?
Algorithm is an ordered sequence of finite, well defined, unambiguous instructions for completing a task. It is an
English-like representation of the logic which is used to solve the problem. It is a step-by-step procedure for solving a
task or a problem. The steps must be ordered, unambiguous and finite in number
Functions are named sequence of statements that accomplish a specific task. Functions usually "take in" data, process
it, and "return" a result. Once a function is written, it can be used over and over and over again. Functions can be
"called" from the inside of other functions
4. Write the pseudo code to calculate the sum and product of two numbers
INITIALIZE variables sum, product, number1, number2 of type real
PRINT “Input two numbers”
READ number1, number2
COMPUTE sum = number1 + number2
PRINT “The sum is", sum
COMPUTE product = number1 * number2
PRINT “The Product is", product
END program
7. Define a Variable and write down the rules for naming a variable
A name that refers to a value is a variable . Variable names can be arbitrarily long. They can contain both letters and
numbers, but they have to begin with a letter. It is legal to use uppercase letters, but it is good to begin variable names
with a lowercase letter.
8. What is Tuple?
A tuple is a sequence of immutable Python objects. Tuples are sequences, like lists. The differences between tuples
and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
9. What is an expression?
An expression is a combination of values, variables, and operators. An expression is evaluated using assignment
operator.
Develop a Strategy
Choose an Approach: Decide on a method to solve the problem (e.g., brute force,
recursion, dynamic programming). Consider efficiency and scalability.
Plan the Algorithm: Outline the steps of the algorithm you'll implement, including data
structures you might use.
(OR)
11.c. Write a Recursive algorithm of “Tower of Hanoi”
11.d. Draw a flow chart to accept three numbers, find the greatest and print the result.
12.a. Write about different types of python operators with example scripts
(OR)
12.b. Compare interpreter and compiler. What type of translator is used for python explain with neat diagram
PART – C (1 × 8 = 8)
13. Write an algorithm to insert a card into a list of sorted cards
8. What do you mean by an operand and an operator? Illustrate your answer with relevant example
An operator is a symbol that specifies an operation to be performed on the operands. The data items that an operator
acts upon are called operands. The operators +, -, *, / and ** perform addition, subtraction, multiplication, division and
exponentiation. Example: 20+32 In this example, 20 and 32 are operands and + is an operator
9. What is the order in which operations are evaluated? Give the order of precedence.
The set of rules that govern the order in which expressions involving multiple operators and operands are evaluated is
known as rule of precedence. Parentheses have the highest precedence followed by exponentiation. Multiplication and
division have the next highest precedence followed by addition and subtraction.
PART – B (2 × 16 = 32)
11.a. What is recursive function? What are its advantages and disadvantages? Compare it with iterative
function
A recursive function is a function that calls itself in order to solve a problem. It breaks down a problem into smaller
sub-problems, solving each sub-problem in a similar way. Each recursive call typically includes a base case that stops
the recursion when a certain condition is met.
Reduction of Code: Recursive functions often require fewer lines of code compared to their iterative
counterparts, making the solution more concise.
Easier to Implement Certain Algorithms: Some algorithms, such as those for searching and sorting (like
quicksort and mergesort), are more naturally expressed recursively.
Stack Overflow: Deep recursion can lead to a stack overflow error if the recursion depth exceeds the call
stack limit, especially in languages that don’t optimize for tail recursion.
Debugging Difficulty: Debugging recursive functions can be more challenging because the flow of
execution is not linear.
Comparison with Iterative Functions
Feature Recursive Function Iterative Function
Definition Calls itself to solve subproblems Uses loops to repeat a block of code
Often simpler for naturally recursive Can be more complex for certain
Simplicity
problems problems
Lower; uses a fixed amount of
Memory Usage Higher due to call stack overhead
memory
Generally faster for the same
Performance Can be slower due to overhead
problem
Low; does not involve recursive
Stack Overflow Risk High if too deep
calls
Ease of Can be less intuitive for complex
Sometimes clearer
Understanding logic
OR)
11.b. Explain about algorithm, Pseudocode and flowchart with an example of finding the sum of ‘n’ numbers.
Algorithm
INPUT N
sum = 0
FOR i FROM 1 TO N DO
sum = sum + i
END FOR
OUTPUT sum
END
Division (/) : Divides the first number by the second. This always returns a float.
result = a / b # result is 2.0
Floor Division (//) : Divides and returns the largest whole number (integer).
result = a // b # result is 2
Exponentiation (**) : Raises the first number to the power of the second.
result = a ** 2 # result is 100
# Print results
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Floor Division:", floor_division)
print("Modulus:", modulus)
print("Exponentiation:", exponentiation)
Output
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Floor Division: 2
Modulus: 0
Exponentiation: 100
(OR)
12.b. Write a Python program to (a) Swap two variables (b) Circulate the values of “n” variables
PART – C (1 × 8 = 8)
13. Mention the list of keywords available in Python. Compare it with Variable name