Skip to main content

Conditional Statements in Python & Control Flow Statements

Conditional statements

Conditional statements allow you to execute a block of code only if a specific condition is true.

Syntax


if condition: # code to execute if condition is true elif another_condition: # code to execute if another_condition is true else: # code to execute if all conditions are false

Examples

  1. Simple if statement

age = 18 if age >= 18: print("You are eligible to vote.") # Output: You are eligible to vote.
  1. if-else statement

num = 10 if num % 2 == 0: print("Even number") else: print("Odd number") # Output: Even number
  1. if-elif-else ladder

marks = 85 if marks >= 90: print("Grade: A") elif marks >= 75: print("Grade: B") else: print("Grade: C") # Output: Grade: B
  1. Nested if statements

num = 10 if num > 0: if num % 2 == 0: print("Positive even number") else: print("Positive odd number") else: print("Negative number or zero") # Output: Positive even number

Control Flow Statements

Control flow statements are used to alter the natural sequence of execution in a program.

Types of Control Flow Statements

a. for Loop

Iterates over a sequence (list, tuple, string, etc.).

Example:


for i in range(1, 6): print(i) # Output: # 1 # 2 # 3 # 4 # 5

b. while Loop

Executes a block of code as long as a condition is true.

Example:


count = 1 while count <= 5: print(count) count += 1 # Output: # 1 # 2 # 3 # 4 # 5

c. break

Exits a loop prematurely.

Example:


for i in range(1, 6): if i == 3: break print(i) # Output: # 1 # 2

d. continue

Skips the current iteration and moves to the next.

Example:


for i in range(1, 6): if i == 3: continue print(i) # Output: # 1 # 2 # 4 # 5

e. pass

Does nothing; a placeholder for future code.

Example:


if True: pass # Code to be added later

f. else with Loops

You can attach an else block to a loop, which executes if the loop completes without a break.

Example:


for i in range(1, 6): if i == 3: break else: print("Loop completed without a break.") # Output: (No output because the loop breaks)

3. Example: Combining Conditional and Control Flow Statements


for i in range(1, 10): if i % 2 == 0: print(f"{i} is even") else: print(f"{i} is odd") # Output: # 1 is odd # 2 is even # 3 is odd # 4 is even # 5 is odd # 6 is even # 7 is odd # 8 is even # 9 is odd

Comments

Popular posts from this blog

Simple Number Guessing Game in Python

  Build a Simple Number Guessing Game in Python: A Beginner-Friendly Project Are you looking to dive into Python programming with a fun and interactive project? The number guessing game Python project is an excellent starting point for beginners. This classic game challenges players to guess a randomly generated number within a limited number of attempts, helping you practice essential concepts like loops, conditionals, and user input. In this blog post, we'll walk through how to create your own Python guessing game project , complete with code and detailed explanations. Whether you're a student, hobbyist, or aspiring developer, this tutorial will boost your skills while keeping things engaging. Why Choose a Number Guessing Game as Your Python Project? The number guessing game Python is one of the most searched beginner projects because it's simple yet versatile. It teaches core programming fundamentals without overwhelming you with complex libraries or frameworks. Her...

Classic Number Guessing Game Using Python

    Build Your First Python Game: The Classic Number Guessing Game! Are you learning Python and looking for a fun, hands-on project to test your skills? You’ve come to the right place. When starting out with programming, it’s easy to get stuck just reading tutorials. The real magic happens when you build something interactive. Today, we’re going to build a classic: The Number Guessing Game . It’s simple concept: The computer picks a random number between 1 and 100, and you have a limited number of tries to guess it. After every guess, the computer tells you if you need to go "Higher" or "Lower." Why This is the Perfect Beginner Project Despite its simplicity, this game is a fantastic learning tool because it combines several fundamental programming concepts into one cohesive package. By building this, you will master: Variables: Storing information like the secret number and attempts left. Loops ( while ): Keeping the game running until the player wins or loses. C...

Rock-Paper-Scissors Python Game Tutorial for Beginners

  A Rock-Paper-Scissors Game: Python Tutorial for Beginners Are you learning Python and itching to move beyond basic "Hello World" scripts? You’re ready to build something interactive. The best way to learn programming is by creating real projects. Today, we are going to build one of the most classic games ever devised: Rock-Paper-Scissors . This is the perfect beginner Python project. Why? Because despite its simplicity, it forces you to use three absolute cornerstones of programming: Handling User Input (Asking the player for their choice). Computer Randomness (Making the computer unpredictable). Conditional Logic (Using if , elif , and else to decide who wins). By the end of this tutorial, you will have a fully functional, interactive command-line game to show off. Let's get coding! The Game Plan Before we write code, let's understand the logic. A game of Rock-Paper-Scissors involves three steps: The Player chooses Rock, Paper, or Scissors. The Computer rando...