Control Structures in Python

344 Explore and Read Our Blogs Written By Our Insutry Experts Learn From KSR Data Vizon

Control structures are essential for making decisions and executing code conditionally. In this post, we will explore Python’s control structures, including conditional statements and loops.

1. Conditional Statements

Conditional statements allow you to execute different blocks of code based on certain conditions.

1.1. if Statement

The if statement executes a block of code if a specified condition is true.

Syntax:

python

if condition:

    # block of code

Example:

python

age = 18

if age >= 18:

    print("You are an adult.")

Output:

sql

You are an adult.

1.2. if-else Statement

The if-else statement executes one block of code if a condition is true and another block of code if the condition is false.

Syntax:

python

if condition:

    # block of code if condition is true

else:

    # block of code if condition is false

Example:

python

age = 16

if age >= 18:

    print("You are an adult.")

else:

    print("You are a minor.")

Output:

css

You are a minor.

1.3. if-elif-else Statement

The if-elif-else statement allows you to check multiple conditions.

Syntax:

python

if condition1:

    # block of code if condition1 is true

elif condition2:

    # block of code if condition2 is true

else:

    # block of code if none of the conditions are true

Example:

python

marks = 85

if marks >= 90:

    print("Grade: A")

elif marks >= 75:

    print("Grade: B")

elif marks >= 50:

    print("Grade: C")

else:

    print("Grade: F")

Output:

makefile

Grade: B

2. Loops

Loops are used to execute a block of code repeatedly as long as a certain condition is met.

2.1. for Loop

The for loop is used to iterate over a sequence (such as a list, tuple, string) or other iterable objects.

Syntax:

python

for item in sequence:

    # block of code

Example:

python

# Iterating over a list

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:

    print(fruit)

Output:

apple

banana

cherry

2.2. while Loop

The while loop executes a block of code as long as a specified condition is true.

Syntax:

python

while condition:

    # block of code

Example:

python

count = 1

while count <= 5:

    print(count)

    count += 1

Output:

1

2

3

4

5

2.3. break, continue, and pass Statements

  • break: Terminates the loop prematurely.
  • continue: Skips the rest of the code inside the loop for the current iteration and moves to the next iteration.
  • pass: Does nothing; acts as a placeholder.

Example:

python

# Using break

for i in range(10):

    if i == 5:

        break

    print(i)

# Using continue

for i in range(10):

    if i % 2 == 0:

        continue

    print(i)

# Using pass

for i in range(5):

    if i == 3:

        pass

    print(i)

Output:

0

1

2

3

4

1

3

5

7

9

0

1

2

3

4

Conclusion

In this post, we covered Python’s control structures, including conditional statements and loops. These constructs are essential for controlling the flow of your programs. In the next post, we will explore functions in Python, how to define and use them, and the difference between built-in and user-defined functions. Stay tuned!

Related Posts

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *