Week 02: Operators, Conditions & Loops
Input and Output
Output:
● Output means showing something on the screen.
● Python uses the print() function.
● print() can display text, numbers, or results of expressions.
Example-
print("Hello, World!")
print(10 + 5)
#Output:
Hello, World!
15
Input:
● Input means taking data from the user.
● Python uses the input() function.
Example-
name = input("Enter your name: ")
print("Hello", name)
#Output:
Hello, Enter your name:
● input() always takes data as string (text).
● If we need numbers, we must convert them.
Example-
age = int(input("Enter your age: "))
print(age)
#Output:
Enter your age:
Operators in Python
Meaning: Operators are special symbols in Python that perform operations on
values/variables.
Types of Operators in Python
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Logical Operators
4. Assignment Operators
1. Arithmetic Operators:
Used for basic math operations.
● + Addition → 5 + 3 = 8
● - Subtraction → 5 - 3 = 2
● * Multiplication → 5 * 3 = 15
● / Division → 5 / 2 = 2.5
● // Floor Division → 5 // 2 = 2
● % Modulus (remainder) → 5 % 2 = 1
● ** Exponent → 2 ** 3 = 8
Example:
Num1= 2
Num2= 4
Print(“The sum is”, Num1+Num2)
#Output-
The sum is 6
2.Comparison/Relational Operators
Used to compare values → result is True or False.
● == Equal to
● != Not equal to
● > Greater than
● < Less than
● >= Greater than or equal to
● <= Less than or equal to
Example:
x= 3
y=5
print(x > y)
print(x == y)
#Output-
True
False
3.Logical Operators:
These operators are used to combine or modify conditional statements and return a
Boolean value (True or False).
● and → True if both are True
● or → True if at least one is True
● not → Reverses the result
Example:(#Output)
a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False
4.Assignment Operators:
Used to assign values.
● = Assign → x = 10
● += Add and assign → x += 5 (same as x = x + 5)
● -= Subtract and assign
● *= Multiply and assign
● /= Divide and assign
● %= Modulus and assign
● **= Exponent and assign
● //= Floor divide and assign
Example:
x = 10
x += 5
print(x)
#Output-
15
Conditional Statements: if, else, elif
● Conditional statements allow a program to make decisions based on
conditions.
● In Python programming, we must be able to execute instructions on a
condition.
Types of Conditional Statements in Python
Python uses if, elif, and else keywords for conditions.
1. if Statement
● Used when you want to run code only if a condition is true.
● If the statement doesn't satisfy the condition, then it will show nothing in the
output.
Example:
age = 20
if age >= 18:
print("You are eligible to vote.")
#Output-
You are eligible to vote.
2.if-else Statement
● Used when there are two possible outcomes.
● If the ‘if statement’ fails to fulfill the condition, then the ‘else statement’ will
work.
Example:
age = 15
if age >= 18:
print("You can vote.")
else:
print("You cannot vote yet.")
#Output-
You cannot vote yet.
3.if-elif-else Statement
● Used when you have multiple conditions to check.
Example:
marks = 72
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Fail")
#Output-
Grade: C
Important Points
1. Indentation is crucial in Python.
2. Conditions use comparison operators (==, !=, <, >, <=, >=) and logical
operators (and, or, not).
3. Boolean values True and False are the basis of all conditions.
Loops: for, while, range(), break/continue
● Loops are used to execute a block of code repeatedly until a condition is met.
● Helps in reducing code repetition.
● Python mainly provides two types of loops:
➡for loop
➡while loop
1. for Loop
Used when a sequence needs to be repeated over.
Syntax:
for variable in sequence:
# code block
Example:
for i in range(5):
print(i)
#Output-
2.while Loop
● Used when you don’t know in advance how many times to run the loop.
● Runs until the condition becomes False.
Syntax:
while condition:
# code block
Example:
Count= 1
While count<=5:
Print(“Hello”)
Count+=1
Range in python
● range() is a built-in function in Python.
● It is used to generate a sequence of numbers.
Commonly used in loops (especially for loops).
Syntax:
range(start, stop, step)
1. start → (optional) starting number of the sequence (default = 0)
2. stop → (required) end of the sequence (not included)
3. step → (optional) difference between each number (default = 1)
Example using stop
for i in range(5):
print(i)
#Output-
Example using start and stop
for i in range(2, 6):
print(i)
#Output-
Example using start, stop, and step
for i in range(1, 10, 2):
print(i)
#Output-
9
Loop Control Statements
Used to change the normal flow of loops.
break → Stops the loop immediately.
for i in range(10):
if i == 5:
break
print(i)
#Output-
continue → Skips the current iteration and moves to the next.
for i in range(5):
if i == 2:
continue
print(i)
Assignment Questions:
1. Explain input() function in your own words.
2. Give one example code for each operator.
3. Write a program that checks if a number entered by the user is greater than
100.
4. Write a program that checks whether a student has passed or failed an exam
using the following rule:
● Marks >= 50: Passed
● Marks >= 40: Retake
● Marks < 40: Failed.
5. If a = 15 and b = 4,find:
● a + b
● a - b
● a * b
● a / b
● a // b
● a % b
6. Compare two variables using a comparison operator.
7. Print all even numbers from 2 to 20 using ‘for’ loop.
8. Print the multiplication table of 5.