Page 254 - CTS - CSA TP - Volume 2
P. 254
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 125 : Construct and analyze code segments
that use branching statements
Objectives
At the end of this exercise you shall be able to
• develop python program to Construct and analyze code segments that use branching statements.
Requirements
Tools/Materials
• PC/Laptop with Windows OS
• Latest Version of Python
Procedure
Indentation
In Python, indentation is the whitespace (typically spaces or tabs) at the beginning of a line that determines the
grouping of statements. Unlike many other programming languages that use curly braces {} or keywords like
begin and end to indicate code blocks, Python relies on indentation for this purpose.
Indentation is not just for readability; it is a syntactical element in Python. Blocks of code with the same level of
indentation are considered part of the same block or suite. Indentation is used to define the scope of control flow
structures, such as loops, conditionals, functions, and classes.
For example, in the following Python code:
if x > 0:
print(“x is positive”)
y = x * 2
print(“Double of x is:”, y)
The statements print(“x is positive”) and y = x * 2 are indented under the if x > 0: statement, indicating that they
are part of the same block and will be executed only if the condition is true.
It’s essential to maintain consistent indentation throughout your code to avoid syntax errors and to ensure that the
structure of your code is correctly interpreted by the Python interpreter. Typically, four spaces are used for each
level of indentation, although you can also use tabs or a different number of spaces as long as it is consistent
within the same block.
TASK 1: If Statement
Code:
# Program to check if a number is positive
num = int(input(“Enter a number: “))
ifnum> 0:
print(“The number is positive.”)
In this example, if the entered number is greater than 0, it prints a message indicating that the number is positive.
Output:
239