0% found this document useful (0 votes)
11 views

Python 20 Important 20 Question

Python presentation

Uploaded by

upsidegaming07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python 20 Important 20 Question

Python presentation

Uploaded by

upsidegaming07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

II GE 3151 PROBLEM SOLVING AND PYTHON PROGRAMMING

Important Questions for Internal Assessment Test

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

2. Write an algorithm to find minimum of 3 numbers in a list


Step 1: Start
Step 2: Read the three numbers A, B, C
Step 3: Compare A,B and A,C. If A is minimum, perform step 4 else perform step 5.
Step 4:Compare B and C. If B is minimum, output “B is minimum” else output “C is minimum”.
Step 5: Stop

3. Define Statements and what is Functions?


The instructions in Python, or indeed in any high-level language, are designed as components for algorithmic problem
solving, rather than as one-to-one translations of the underlying machine language instruction set of the computer.

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

5. Give the rules for writing Pseudo Code


 Write one statement per line.
 Capitalize initial keywords.
 Indent to show hierarchy.
 End multiline structure.
 Keep statements language independent.

6. What is Value? What are the different types of Values?


A value is one of the fundamental things – like a letter or a number – that a program manipulates. Its types are: integer,
float, strings , lists and Dictionary.

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.

10. Illustrate the use of * and + operators in String with an example


The * operator performs repetition on strings and the + operator performs concatenation on strings.
Example: >>> ‘Hello*3’
Output: HelloHelloHello
>>>’Hello+World’
Output: HelloWorld
PART – B (2 × 16 = 32)
11.a. Discuss about the building blocks of an Algorithm
11.b. Explain the steps to identify the computational problem

Understand the Problem


 Read the Problem Statement: Carefully read and comprehend what is being asked. Identify the
inputs, outputs, and any constraints.
 Clarify Requirements: If necessary, ask questions to clarify any ambiguities.

Break Down the Problem


 Decompose the Problem: Divide the problem into smaller, manageable parts or subproblems.
This can make it easier to tackle each part individually.
 Identify Relationships: Look for relationships between different parts of the problem to
understand how they interact.
Analyze Examples
 Work Through Examples: Create and analyze sample inputs and outputs. This can help
illustrate the problem and reveal patterns or edge cases.
 Identify Edge Cases: Consider special or extreme cases that could affect the solution,
such as empty inputs, large values, or invalid data.

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.

Implement the Solution


 Write the Code: Translate your algorithm into Python code, following best practices for
readability and efficiency.
 Use Comments: Add comments to your code to explain your thought process and logic.

Test the Solution


 Run Test Cases: Use your earlier examples and edge cases to test the implementation.
Check for correctness and performance.
 Debug: If the code doesn’t work as expected, use debugging tools or print statements to
identify where the logic breaks.

Optimize and Refine


 Review the Code: Look for opportunities to optimize the code, whether through
algorithmic improvements or by simplifying the logic.
 Refactor: Clean up the code to make it more efficient or easier to understand, if
necessary.

Document the Solution


 Add Documentation: Write clear documentation for your code, explaining how to use it,
any dependencies, and how it works.
 Share Insights: If applicable, note any lessons learned or interesting insights gained
during the problem-solving process.

(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

GE 3151 PROBLEM SOLVING AND PYTHON PROGRAMMING


Important Questions for Internal Assessment Test

1. Give the difference between Recursion and Iteration?

2. Define Control Flow Statement


A program's control flow is the order in which the program's code executes. The control flow of a Python program is
regulated by conditional statements, loops, and function calls.

3. Write an algorithm to find the Minimum number in a given list of numbers

4. Give the various Data types in Python


Numbers, String, List, Tuple and Dictionary

5. Define the 2 modes in Python


Python has two basic modes: script and interactive.
The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. Interactive
mode is a command line shell which gives immediate feedback for each statement, while running previously fed
statements in active memory
6. What do you mean by flow of execution
In order to ensure that a function is defined before its first use, we have to know the order in which statements are
executed, which is called the flow of execution. Execution always begins at the first statement of the program.
Statements are executed one at a time, in order from top to bottom.

7. What is function call?


A function is a named sequence of statements that performs a computation. When we define a function, we specify the
name and the sequence of statements. Later, we can “call” the function by its name called as function call.

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.

10. What is Meant by Traceback


A list of the functions that tells us what program file the error occurred in, and what line, and what functions were
executing at the time. It also shows the line of code that caused the error.

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.

Advantages of Recursive Functions


Simplicity and Clarity: Recursive solutions can be more straightforward and easier to understand,
especially for problems that have a natural recursive structure (like tree traversal, factorial calculation, and
Fibonacci series).

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.

Disadvantages of Recursive Functions


Performance: Recursive functions can be less efficient due to the overhead of multiple function calls. Each
call adds a new layer to the call stack, which can lead to increased memory usage.

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

1. # Find the sum of N natural numbers. We assume that we are


2. # to input N and then find the sum of 1 + 2 + 3 + ... + N
3. Input N
4. I = 1
5. Sum = 0
6. While (I <= N)
7. Add I to Sum
8. Add 1 to I
9. Print Sum
10. Stop program

Pseudo Code representation


START

INPUT N

sum = 0

FOR i FROM 1 TO N DO

sum = sum + i

END FOR

OUTPUT sum

END

12.a. Appraise the Arithmetic operations in Python with an example

In Python, arithmetic operations are performed using standard mathematical symbols

Basic Arithmetic Operations


Addition (+) : Add two numbers.
a = 10
b=5
result = a + b # result is 15

Subtraction (-) : Subtract the second number from the first.


result = a - b # result is 5

Multiplication (*) : Multiplies two numbers.


result = a * b # result is 50

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

Modulus (%) : Returns the remainder of the division.


result = a % b # result is 0

Exponentiation (**) : Raises the first number to the power of the second.
result = a ** 2 # result is 100

# Define two numbers


a = 10
b=5

# Perform arithmetic operations


addition = a + b # 15
subtraction = a - b #5
multiplication = a * b # 50
division = a / b # 2.0
floor_division = a // b # 2
modulus = a % b #0
exponentiation = a ** 2 # 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

 False - Invalid variable name. Use is_false, flag, etc.


 await - Invalid variable name. Use awaiting, await_time, etc.
 else - Invalid variable name. Use alternative, fallback, etc.
 import - Invalid variable name. Use import_module, load_data, etc.
 pass - Invalid variable name. Use do_nothing, placeholder, etc.
 None - Invalid variable name. Use no_value, null_value, etc.
 break - Invalid variable name. Use exit_loop, stop_iteration, etc.
 except - Invalid variable name. Use exception_case, error_handling, etc.
 in - Invalid variable name. Use inside, contains, etc.
 raise - Invalid variable name. Use trigger_error, send_exception, etc.
 True - Invalid variable name. Use is_true, valid, etc.
 class - Invalid variable name. Use my_class, user_class, etc.
 finally - Invalid variable name. Use finally_block, cleanup, etc.
 is - Invalid variable name. Use is_equal, check_is, etc.
 return - Invalid variable name. Use send_back, output_value, etc.
 and - Invalid variable name. Use and_condition, both_true, etc.
 continue - Invalid variable name. Use continue_loop, proceed, etc.
 for - Invalid variable name. Use for_loop, each_item, etc.
 lambda - Invalid variable name. Use lambda_function, short_func, etc.
 try - Invalid variable name. Use attempt, try_block, etc.
 as - Invalid variable name. Use as_alias, role_as, etc.
 def - Invalid variable name. Use define_function, create_method, etc.
 from - Invalid variable name. Use source, origin, etc.
 nonlocal - Invalid variable name. Use outer_scope, enclosing, etc.
 while - Invalid variable name. Use while_condition, loop_while, etc.
 assert - Invalid variable name. Use check_condition, assertion, etc.
 del - Invalid variable name. Use delete_item, remove, etc.
 global - Invalid variable name. Use global_scope, shared_variable, etc.
 not - Invalid variable name. Use negate, is_not, etc.
 with - Invalid variable name. Use context_manager, using, etc.
 async - Invalid variable name. Use async_task, async_process, etc.
 elif - Invalid variable name. Use else_if, additional_condition, etc.
 if - Invalid variable name. Use condition, is_true, etc.
 or - Invalid variable name. Use or_condition, either_true, etc.
 yield - Invalid variable name. Use generate, produce_value, etc.

You might also like