0% found this document useful (0 votes)
2 views5 pages

17th Apr NB - Controlled Statements in Python

The document explains operators and operands in programming, detailing unary and binary operators, as well as string manipulation through concatenation and replication. It covers assignment operators, operator precedence, and control statements like conditionals and loops in Python. The document illustrates how these concepts allow for decision-making and repetitive actions in code execution.

Uploaded by

hredaan2012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

17th Apr NB - Controlled Statements in Python

The document explains operators and operands in programming, detailing unary and binary operators, as well as string manipulation through concatenation and replication. It covers assignment operators, operator precedence, and control statements like conditionals and loops in Python. The document illustrates how these concepts allow for decision-making and repetitive actions in code execution.

Uploaded by

hredaan2012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Operators - They are symbols or keywords that are used to perform operations on

data. Eg. +, -, *, /, %, etc.

Operators define the operation to be performed

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.

Operands provide the data for the operation.

Types of Operators:

Unary Operators Binary Operators


• Unary operators are operators that take only one • Binary operators are operators that take two
operand. operands.
• They perform an operation on a single value or • They perform an operation on two values or
variable. variables.

Concatenation - A process of combining two or more strings into a single string. It is performed using the + operator.

Example: "Hello, " + "world!" # Output: "Hello, world!"

Replication- A process of repeating a string a specified number of times. It is performed using the * operator.

Example: "Hello" * 3 # Output: "HelloHelloHello"

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.

Simple Assignment (=): Assigns a value to a variable.

x = 5 assigns the value 5 to the variable x.


Operator precedence is the order in which operators are evaluated in an expression. It determines which operator is
evaluated first when there are multiple operators in an expression. This is based on the precedence and associativity of
the operators.

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:

- Left-associative: Operators are evaluated from left to right.

- Right-associative: Operators are evaluated from right to left.

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)

print("You are", age, "years old.")

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

if temperature > 20:

print("It's warm.")

else:

print("It's not too warm.")

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.

Here's a very simple example:

for number in [1, 2, 3]:

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.

You might also like