Unit 5(Part B - Python)
Unit 5(Part B - Python)
Part B UNIT - 5
Introduction to Python
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.
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.
Output:
[“Amritsar”, “Bengaluru”, “Delhi”, “Mumbai”]
3. Looping statements: Looping statements control involves loops like for and while:
Example:
for i in range (5):
print(i)
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
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