Chapter 1 - Computational Thinking and Programming - Topic Wise Unsolved
Questions [1.5 - 1.6]
Questions 1.5
1. What is the command word for a conditional statement in Python?
Answer: The command word for a conditional statement in Python is `if`.
Explanation: In Python, the `if` statement is used to test a condition. If the condition
evaluates to `True`, the code block under the `if` statement will execute. The general syntax
is:
```python
if condition:
Code to execute if the condition is true
```
For example:
```python
age = 18
if age >= 18:
print("You are an adult.")
```
In this case, since `age` is 18, which is greater than or equal to 18, the output will be "You
are an adult."
2. How do you identify the code that you want to run when the condition is true in
Python?
Answer: The code that you want to run when the condition is true is identified by indenting it
under the `if` statement.
Explanation: Python relies on indentation (typically 4 spaces) to define the scope of the code
that should be executed within an `if` statement. Any code that is indented under the `if`
statement will be executed if the condition is true.
For example:
```python
num = 5
if num > 0:
print("The number is positive.")
```
In this case, the print statement is indented under the `if` condition, so it will be executed
only if `num` is greater than 0. If the condition is false, the indented code block will be
skipped.
3. How many ELSE statements can one IF statement have in Python?
Answer: One `if` statement can have only one `else` statement.
Explanation: The `else` statement is used to specify a block of code to be executed if the
condition in the `if` statement is false. In Python, an `if` statement can be followed by one
`else` statement. If the `if` condition is not met, the code block under the `else` statement will
be executed.
For example:
```python
is_sunny = False
if is_sunny:
print("Let's go to the park.")
else:
print("Let's stay indoors.")
```
Here, since `is_sunny` is `False`, the `else` block will execute, and the output will be
"Let's stay indoors."
4. Identify the output that this program will give:
```python
value = 10
if value < 10:
print("Tortoise")
else:
print("Hamster")
```
Answer: The output will be `Hamster`.
Explanation: In the provided code, the `if` condition checks if the variable `value` is less than
10. Since `value` is 10, the condition `value < 10` evaluates to `False`. Therefore, the code
block under the `else` statement is executed, printing "Hamster".
Here's how the code executes step-by-step:
1. `value` is set to 10.
2. The condition `if value < 10` is checked, which evaluates to `False`.
3. Since the condition is `False`, the `else` block is executed.
4. The output is "Hamster".
Here is the analysis and explanation of the questions based on the provided image:
5. Identify the output that this program will give:
```python
data1 = "yes"
data2 = "no"
total = 0
if data1 == "no":
total = 10
elif data2 == "no":
total = 20
print(total)
```
Answer: The output will be `20`.
Explanation: The code checks two conditions using an `if-elif` statement:
1. The `if` condition checks if `data1` is equal to `"no"`. Since `data1` is `"yes"`, this
condition is `False`, so the code inside this block is not executed.
2. The `elif` condition checks if `data2` is equal to `"no"`. Since `data2` is `"no"`, this
condition is `True`, and the block of code inside the `elif` statement is executed, setting `total
= 20`.
Finally, `print(total)` is executed, and it prints `20`.
6. Identify the output that this program will give:
```python
data1 = "yes"
data2 = "yes"
total = 0
if data1 == "no":
total = 10
elif data2 == "no":
total = 20
print(total)
```
Answer: The output will be `0`.
Explanation: The code checks two conditions:
1. The `if` condition checks if `data1` is equal to `"no"`. Since `data1` is `"yes"`, this
condition is `False`, so the code inside this block is not executed.
2. The `elif` condition checks if `data2` is equal to `"no"`. Since `data2` is also `"yes"`, this
condition is `False` as well.
Since both conditions are `False`, the value of `total` remains `0`. The `print(total)`
statement prints `0`.
7. In an ELSEIF statement, when does the second condition get tested?
Answer: The second condition in an `elif` statement gets tested only if the `if` condition is
`False`.
Explanation: In an `if-elif-else` structure, the program first checks the `if` condition. If the `if`
condition is `True`, the code inside the `if` block is executed, and the rest of the `elif` and
`else` blocks are skipped. However, if the `if` condition is `False`, the program moves on to
the `elif` condition. The `elif` condition is tested, and if it is `True`, the code inside the `elif`
block is executed. If the `elif` condition is also `False`, then the program will execute the
`else` block (if it exists).
8. Write a Python program to ask the user to enter the year group they are in at school
and output a message about that year.
Answer:
```python
Ask the user for the year group
year_group = input("Enter your year group: ")
Output a message based on the year group
print(f"You are in Year {year_group} at school.")
```
Explanation: The program uses the `input()` function to ask the user to enter their year
group. The input is stored in the variable `year_group`. The program then uses `print()` to
output a message that includes the year group entered by the user. The `f-string` in the
`print()` function allows for easy string formatting to include the value of `year_group` in the
output message.
Example output if the user enters `10`:
```
Enter your year group: 10
You are in Year 10 at school.
```
Questions 1.6:
1. What is the difference between a variable and a constant?
Answer: A variable is a storage location in programming that can hold different values
during the execution of a program. A constant, on the other hand, is a storage location
whose value is fixed and cannot change during the execution of the program.
Explanation:
Variable: In programming, a variable is a symbolic name that refers to a memory location
where data is stored. The value of a variable can be modified as the program runs. For
example:
```python
age = 25
age = 26 The value of 'age' changes from 25 to 26
```
Constant: A constant is similar to a variable, but once a value is assigned to it, the value
cannot be changed throughout the program. Constants are used to represent fixed values,
such as mathematical constants (e.g., `PI = 3.14159`). For example:
```python
PI = 3.14159 This value will not change throughout the program
```
2. What do both constants and variables have?
Answer: Both constants and variables have a name (or identifier) and a value.
Explanation:
Name (Identifier): This is the symbolic name used to reference the memory location where
the value is stored. In the examples provided earlier, `age` and `PI` are identifiers.
Value: This is the data stored in the memory location referenced by the identifier. In the
examples, `25`, `26`, and `3.14159` are the values of the variables or constants.
Both constants and variables use names (identifiers) to access the value they store.
3. Why are constants used in programs?
Answer: Constants are used in programs to ensure that certain values remain unchanged
throughout the execution of the program. This helps prevent accidental modification of
important values, improves code readability, and makes it easier to maintain the program.
Explanation:
Preventing Accidental Modification: By using constants, programmers can ensure that
certain values, such as configuration settings or fixed values like mathematical constants,
remain unchanged, reducing the risk of bugs.
Improving Readability: Using constants with descriptive names makes the code easier to
understand. For example, using `PI = 3.14159` instead of directly using the number
`3.14159` in multiple places helps others understand what the value represents.
Ease of Maintenance: If a constant needs to be updated, it can be changed in one place,
and the change will be reflected throughout the program. This makes it easier to maintain
and update the code.
For example:
```python
TAX_RATE = 0.18 Constant representing tax rate
price = 100
tax = price TAX_RATE
total_price = price + tax
```
Here, `TAX_RATE` is used as a constant to ensure the tax rate is consistent across the
program.