17th Apr NB - Controlled Statements in Python
17th Apr NB - Controlled Statements in Python
Operands – they are the values or variables that are used in an operation. They are
the data that is being manipulated or compared. Eg. - Variables: x, y, z, etc.
Types of Operators:
Concatenation - A process of combining two or more strings into a single string. It is performed using the + operator.
Replication- A process of repeating a string a specified number of times. It is performed using the * operator.
Concatenation Replication
• Symbol used is + • Symbol used is *
• Combines multiple strings • Repeats a single string
• Single string • Repeated string
• "Hello, " + "world!" • "Hello" * 3
An assignment operator is a symbol used to assign a value to a variable. In programming, assignment operators are used
to store values in variables, which can then be used in expressions and statements.
Precedence refers to the order in which operators are evaluated in an expression. Operators with higher precedence are
evaluated before operators with lower precedence.
Associativity refers to the order in which operators of the same precedence are evaluated in an expression. There are
two types of associativity:
SOLVE:
Control statements in Python are like the decision-makers and loop controllers of your code. They let you tell the
program to do different things based on conditions (using if, Elif, else) or to repeat actions multiple times (using for and
while loops).
Think of it like reading a book from the first page to the last – each line of code is processed in sequence. There's no
decision-making or repetition involved; it's just a straight line of commands.
name = "Alice"
age = 30
print("Hello,", name)
In the above example, the first line assigns the value "Alice" to the variable name. Then, the second line assigns 30 to the
variable age. Finally, the two print statements are executed in order, displaying the greeting and age. Each statement
completes before the next one begins.
Conditional statements in Python let your program make choices. They check if something is true or false and then run
different pieces of code based on that result.
It's like saying "If this is true, do this; otherwise, do that." This allows your program to respond differently to various
situations.
For example:
temperature = 25
print("It's warm.")
else:
In this example, the program checks if the temperature is greater than 20. If it is true, it prints "It's warm." If it's false, it
prints "It's not too warm." The program follows only one of these paths based on the condition.
Iterative in Python means doing something repeatedly. Iterative statements, also known as loops, let you run the same
block of code multiple times. This is useful when you need to process many items in a list, count up to a certain number,
or keep doing something until a specific condition is met. Instead of writing the same instructions over and over, you use
a loop to tell Python to repeat them.
print(number)
This code uses a for loop to go through each number in the list [1, 2, 3]. For each number, it runs the print(number)
command. So, it prints 1, then 2, then 3 – repeating the print action for each item.