Solve worksheet
Solve worksheet
Solve worksheet
Chapter: 7
A. Fill in the blanks :
1. ____________ construct is used in situations where a user can decide among multiple conditions.
Answer: `if-elif-else`.
Answer: `X = X + 1`.
4. The statement 5%2 will evaluate to ____________ and 5/2 will evaluate to ____________.
1. Original Code:
Corrected Code:
```python
```python
Value=40
Value = 40
If Value>=25
if Value >= 25:
print("Smaller Value)
print("Smaller Value")
print "Program Over"
print("Program Over")
```
```
```python ```python
A=10,B=20 A = 10
if (15==A) B = 20
print("A";A,"B=";B) if 15 == A:
```
Corrected Code:
3. Original Code:
```python
```python
A = input('Enter a number: ')
input('Entera number, A)
print('The number is', A)
Print ('The number is' A)
```
```
4. Original Code:
Corrected Code:
```python
```python
Num1=input()
Num1 = int(input("Enter first number: "))
Num2=int(input())
Num2 = int(input("Enter second number: "))
print(Num1+Num2)
print(Num1 + Num2)
```
C. Write Python programs :
1. Accept the length and the breadth of a rectangle 2. Accept three numbers from the user and display
from the user and calculate its area and perimeter. the largest number.
```python
length = float(input("Enter the length of the rectangle: ")) num1 = float(input("Enter the first number: "))
breadth = float(input("Enter the breadth of the rectangle: ")) num2 = float(input("Enter the second number: "))
area = length breadth num3 = float(input("Enter the third number: "))
perimeter = 2 (length + breadth) largest = max(num1, num2, num3)
print("area=",area)
print("perimeter=",perimeter) print("largest=",largest)
3. Accept a number from the user and check if it is an odd or even number.
```python
if num % 2 == 0:
print("number is an even number.")
else:
print("number is an odd number.")
D. Answer the following questions :
________________________________________
# Valid
First_Name = "John"
________________________________________
print(First_Name) # Output: John
c. Value1: Valid
Reason: Variable names can contain letters, digits, and underscores, but cannot start with a digit.
Example:
Value1 = 50
print(Value1) # Output: 50
________________________________________
d. Int: Valid
Reason: While it is valid, using Int (or other built-in names) as a variable name is discouraged because it may
create confusion or overwrite built-in functions.
Example:
Int = 5
print(Int) # Output: 5
________________________________________
e. Stock: Valid
Reason: It follows all the rules for valid variable naming.
________________________________________
Example:
Stock = 100
print(Stock) # Output: 100
5. How do you accept data from a user? Give the syntax with an example.
To accept input from a user in Python, you use the input() function. It always returns the input as a string, so you
may need to convert it to another data type (e.g., int, float) if needed.
Syntax:
variable = input("Enter your prompt: ")
Example:
# Accepting a string input
name = input("Enter your name: ")
4. You check the power supply points for ____________ related problems.
Answer: Hardware.
3. The three areas to troubleshoot are: hardware, software, and operating system.
Answer: True.
4. If your Windows restarts without warning, then there is a problem in the computer hardware.
Answer: True.
5. Proper maintenance will increase the speed and the life of your PC.
Answer: True.
C. Identify the troubleshooting areas for each of the following problems (Questions and Answers):
2. The system crashes when you are working with a specific software.
Answer: Software.
4. Write about two common problems related to software that you might have experienced.
Answer:
1. The ________ statement skips the rest of the loop statements and causes the next iteration of the
loop to take place.
Answer: `continue`.
2. The ________ statement enables a program to skip over a part of the ________.
3. The ________ part of the while loop is executed only when the while loop completes all its iterations.
Answer: `else`.
4. Condition is checked for True or False, and the statements are executed only if the condition is
________.
Answer: `True`.
5. The ________ loop is helpful when the number of iterations is not known prior to the execution of the
loop.
Answer: `while`.
x += 5
```
1. Code:
```python
Num = 0 Output:
Num += 1
if Num == 5:
break
print(Num, end=)
```
Output:
2. Code:
S
```python
t
for val in String:
r
if val == l:
i
break
n
print(val)
g
print(Over)
Over
D. Answer the following questions (5 marks each):
Answer:
Iterations are used to repeat a block of code multiple times, making the program efficient and concise.
They help automate repetitive tasks, such as traversing data structures like lists or ranges. Loops like `for`
and `while` enable decision-making and control flow in programs. Iterations also make it easy to perform
calculations or process data sets. Python's loops are versatile and can handle nested or conditional
structures.
Answer:
The `range()` function generates a sequence of numbers. It accepts up to three parameters: start
(inclusive), stop (exclusive), and step (increment).
Example:
```python
print(i)
```
Output: `1 3 5`.
Answer:
- A `while` loop is used when the condition for repetition is dynamic or not known beforehand.
- `for` loops iterate over sequences like lists or ranges, while `while` loops repeat as long as a condition
evaluates to `True`.
- `for` loops are concise and easier to read for finite iterations.
Answer:
Jump statements alter the flow of loops in Python. The three main jump statements are:
- `continue`: Skips the rest of the current iteration and proceeds to the next iteration.