Python - Chapter 5 Notes In English Part- 1
A Simple Guide to Python Control Statements
Learn how to make your programs smarter with decision-making and looping structures.
What Are Control Statements?
In Python, code normally runs from the first line to the last. Control statements are special instructions that change this normal flow. They allow your program to make decisions or repeat actions, making your code much more powerful and useful.
There are two main types of control statements:
- Conditional Statements (Decisions): These decide which block of code to run based on whether a condition is true or false.
- Iterative Statements (Loops): These repeat a block of code over and over as long as a certain condition is met.
Conditional Statements (Making Decisions)
Conditional statements use if, elif, and else to control which code is executed.
The `if` Statement
The if statement is the simplest decision-making statement. It runs a block of code only if a specific condition is true. If the condition is false, the code block is just ignored.
Example: Credit Card Limit
# Credit Card Program
amount = int(input("Enter the amount :"))
if (amount <= 1000):
print("Your charge is accepted.")
if (amount > 1000):
print("The amount exceeds your credit limit.")
The `if-else` Statement
The if-else statement provides an alternative action. If the if condition is true, its block runs. If it's false, the else block runs.
Example: Checking for Even or Odd Numbers
# Program to print if a number is even or odd
num = int(input("Enter a number: "))
if (num % 2 == 0):
print(num, "is an even number.")
else:
print(num, "is an odd number.")
The `if-elif-else` Statement
When you need to check several different conditions, you can use the if-elif-else chain. elif is short for "else if."
Example: Assigning Grades
# Assigning grades based on score
grade = eval(input('Enter your score: '))
if grade >= 90:
print('A')
elif (grade >= 70):
print('B')
elif (grade >= 60):
print('C')
elif (grade >= 40):
print('D')
else:
print('F')
Iterative Statements (Loops)
Loops are used to perform repetitive tasks without writing the same code over and over again.
The `while` Loop
A while loop repeats a block of code as long as its condition remains true.
Example: Printing "Hello World" Five Times
# Demonstration of while loop
count = 0
while (count <= 4):
print("Hello World")
count = count + 1
print("Done")
The `for` Loop and `range()`
A for loop is used to iterate over a sequence of items, like a list or a range of numbers.
Example: Generating a Multiplication Table
# Generation of Multiplication Table
num = int(input("Enter number for which you want table: "))
for i in range(1, 11):
print(num, 'X', i, '=', num * i)
Jump Statements
Jump statements are used to transfer program control from one point to another unconditionally.
The `break` Statement
The break statement is used to exit a loop immediately.
# Stop the loop when 'U' is found
for ch in 'COMPUTER':
if ch == 'U':
break
print(ch)
# Output: C O M P
The `continue` Statement
The continue statement is used to skip the rest of the current iteration and jump to the next one.
# Skip the letter 'U'
for ch in 'COMPUTER':
if (ch == 'U'):
continue
print(ch)
# Output: C O M P T E R
The `pass` Statement
The pass statement does nothing. It's a placeholder for code you plan to write later, preventing syntax errors from empty blocks.
# Placeholder for 'Ramesh'
name = input('Enter your name: ')
if (name == 'Ram'):
print('Welcome!')
elif (name == 'Ramesh'):
# Not finished yet...
pass
elif (name == 'Rohan'):
print('Access Denied')
Comments
Post a Comment