Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Unit 5(Part B - Python)

The document covers key concepts in Python programming, including flowcharts, algorithms, control structures, and data types. It explains the importance of variables, the differences between loops, and error types in Python. Additionally, it provides examples of list operations and indexing methods.

Uploaded by

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

Unit 5(Part B - Python)

The document covers key concepts in Python programming, including flowcharts, algorithms, control structures, and data types. It explains the importance of variables, the differences between loops, and error types in Python. Additionally, it provides examples of list operations and indexing methods.

Uploaded by

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

Grade 9

Part B UNIT - 5
Introduction to Python

Short answer type questions.


1. Why are flowcharts more preferred than algorithms?
Flowcharts are preferred over algorithms as they provide a visual representation, making it
easier to understand, debug, and communicate the process flow. They help in identifying the
logical flow and errors more efficiently.

2. Can we write more than one algorithm for the same problem? Explain.
Yes, multiple algorithms can solve the same problem. Different algorithms might vary in
efficiency, complexity, or approach but still achieve the same result.

3. What is the meaning of selection flow in the control structures?


Selection flow in control structures means making decisions in a program. It involves choosing
different paths of execution based on conditions, like if-else statements, which direct the flow
accordingly.

4. How can we create a multi-line string in Python?


In Python, a multi-line string can be created using triple quotes, either ''' or """ allowing the
string to span multiple lines without using escape characters for line breaks.

5. What is the use of the print function? Give a few examples to support your answer.
The print function outputs data to the console. It converts the expression into a string before
writing to the screen.
Syntax to use the print() function is:
print(object(s), sep = separator, end = end)
Examples:
‘print("Hello, World!")’ prints a string.
‘print(123)’ prints a number.
‘print([1, 2, 3])’ prints a list.

6. What are variables? Why are they important in any programming language?
Variables store data that can be used and manipulated in a program. They are crucial as they
enable dynamic data handling and allow for more flexible and complex programming.

7. How is floor division different from normal division?


Floor division (//) returns the largest integer less than or equal to the division result, while
normal division (/) returns a floating-point number representing the exact quotient.
8. What are the rules for naming the variables and constants
Variable names must start with a letter or underscore, cannot start with a number, and should
not include spaces or special characters. Constants are typically written in uppercase to
differentiate them from variables.

9. What is the meaning of sequential programming?


Sequential programming executes code statements in a linear, top-down order. This is the
default flow of the execution of a program. Each statement runs one after another without
branching, looping, or jumping to other sections.

10. What is a nested if statement?


A nested if statement is an if statement inside another if statement. It allows multiple levels of
condition checks, providing more detailed decision-making paths in the code.

11. Why is it necessary to give step value in the while loop?


The step value in a while loop is necessary to prevent infinite loops. It ensures that the loop
progresses towards the termination condition, making the loop logic functional.

12. Why the while loop is called an entry-controlled loop?


The while loop is called an entry-controlled loop because the condition is checked before the
execution of the loop’s body. If the condition is false initially, the loop will not execute at all.

13. What is the use of the sort function of a list?


The sort function arranges list elements in ascending or descending order. It organizes data for
easier analysis and retrieval, improving data handling and operations.
For Example:
City = [“Delhi”, “Mumbai”, “Bengaluru”, “Amritsar”]
City.sort()
print(City)

Output:
[“Amritsar”, “Bengaluru”, “Delhi”, “Mumbai”]

14. What is the difference between pop and remove functions?


The pop function removes an element from the list based on the index number specified in the
function and returns it,
while the remove function deletes the first occurrence of a specified value without returning it.
Only one value can be removed at a time using the remove function even if there are duplicate
values in the list.

15. What will be the output of the following statement?


print ([1,2,6]) < [1,2,5])
The output of ‘print ([1, 2, 6] < [1, 2, 5])’ will be ‘False’. The comparison evaluates element-wise,
and since 6 is not less than 5, the result is false.
Long answer type questions:

1. Give three differences between an algorithm and a flow chart.


• An algorithm is a step-by-step textual procedure for solving a problem, while a flowchart
is a graphical representation of that procedure.
• Algorithms use plain language or pseudocode, whereas flowcharts use standardized
symbols like ovals, rectangles, and diamonds.
• Algorithms are generally more abstract and concise, focusing on the logic and sequence
of steps. Flowcharts provide a visual understanding, making it easier to see the process
flow and debug errors.
• Algorithms are more suited for detailed, technical documentation, while flowcharts are
useful for communicating processes to a broader audience, including non-technical
stakeholders.

2. Explain with an example the three control structures.


The three control structures are sequential, selection, and loops. These structures allow for
comprehensive program flow, accommodating various scenarios and iterative processes.
1. Sequential statements: Sequential statements control execute statements in order, one
after another.
Example:
print("Hello")
print("World")

2. Selection Statements: Selection statements control involves decision-making using if-else


statements:
Example:
if age >= 18:
print("Adult")
else:
print("Minor")

3. Looping statements: Looping statements control involves loops like for and while:
Example:
for i in range (5):
print(i)

3. Explain the three different numeric data types available in Python.


The Numeric data types allow Python to handle various mathematical computations, from
simple integer arithmetic to more complex calculations involving floating-point and complex
numbers. Python has three numeric data types: int, float, and complex.
1. The ‘int’ type represents whole numbers without a decimal point, such as 5 or -42.
2. The ‘float’ type represents real numbers with a decimal point, such as 3.14 or -0.001.
3. The ‘complex’ type represents complex numbers with real and imaginary parts, denoted as
`a + bj`, where ‘a’ is the real part and ‘b’ is the imaginary part.
4. Explain three different types of errors in Python.
Python has three main types of errors: syntax, runtime, and logical errors.
1. Syntax errors occur when the code violates Python's syntax rules, such as missing colons or
parentheses, and prevent the program from running.
2. Runtime errors occur during execution, such as division by zero or file not found, causing
the program to terminate abruptly.
3. Logical errors are mistakes in the program's logic, leading to incorrect output, such as using
the wrong formula or conditions.

5. What Conversion? Explain the types of type conversion with the help of an example.
Conversion refers to changing the data type of a value. There are two types of type conversion:
implicit and explicit.
1. Implicit conversion is automatically performed by Python, such as converting an
integer to a float during a calculation.
2. Explicit conversion, or typecasting, is done manually using functions like `int()`,
`float()`, or `str()`.
For example:
x = 5 # int
y = 2.5 # float
result = x + y # implicit conversion to float
print(result) # 7.5

z = "123"
num = int(z) # explicit conversion to int
print(num) # 123

6. Differentiate between the for loop and the while loop.


A `for` loop and a `while` loop are both used for iteration in Python, but they serve different
purposes and have distinct characteristics:
For Loop: It is used when the number of iterations is known beforehand, or when iterating over
a sequence (such as a list, tuple, or range).
Syntax:
for element in sequence:
# code block
Example:
for i in range(5):
print(i) # prints 0 to 4

While Loop: It is used when the number of iterations is not known beforehand and the loop
should continue until a certain condition is no longer true.
Syntax:
while condition:
# code block
Example:
i=0
while i < 5:
print(i) # prints 0 to 4
i += 1

7. Name the different ways of deleting an element in a list.


There are two ways to delete an element in a list: `del`, `remove ()`, and `pop ()`.
The `del` statement removes an element by index:
• del my_list[2] # removes the element at index 2
The `remove ()` method deletes the first occurrence of a value:
• my_list.remove('apple') # removes 'apple' from the list

The `pop ()` method removes and returns an element by index:


• my_list.pop(1) # removes and returns the element at index 1

8. Explain two different ways of indexing in a list.


In Python, lists can be indexed using positive and negative indices. These indexing methods allow
accessing list elements from both the beginning and end, providing flexibility in data retrieval.

Positive indexing starts from 0 and increases from left to right.


For example:
my_list = ['a', 'b', 'c', 'd']
print(my_list[1]) # prints 'b'

Negative indexing starts from -1 and decreases from right to left.


For example:
print(my_list[-1]) # prints 'd'
print(my_list[-2]) # prints 'c'

You might also like