🌟 Welcome Learners 🌟

🎓 Think Logic 🖥️ Code Magic🪄

Go Slow — doubling the speed doesn’t double understanding ✨

Get Started

🚀 Go from Zero to Hero with our YouTube Playlists 📺

️ 30 Lessons • 15K+ Views
Computer Science
🎬 Start Learning
20 Lessons • 10K+ Views
Informatics Practices
🎬 Start Learning
50 Lessons • 25K+ Views
Informatics Practices
🎬 Start Learning
🧑‍35 Lessons • 30K+ Views
Computer Science
🎬 Start Learning
Lessons • 12K+ Views
Artificial Intelligence (Class X)
🎬 Start Learning
15 Lessons • 7K+ Views
Artificial Intelligence (Class IX)
🎬 Start Learning
Join live, interactive sessions via Zoom
Online Classes
📲 Learn Comfortably From Home!
Visit our learning centers at:
Offline Classes
📍 Sant Nagar, Outram Lane, Pitampura

🌟 Student's Feedback 🌟

"Rohit Sir you are just amazing. Sir you always trusted in your students and never demotivated us. I will never be able to forget the fun banter which you included in your classes just to make the class more engaging, lively and interesting......."

★★★★★

— Disha Sachdeva

"Sir is one of the most genuine and hardworking teacher I've ever had. Thankyou !" ."

★★★★★

— Tanisha Singhal

"Best teacher for CBSE Computer Science. Notes were crisp and to the point. Weekly assignments kept me focused, and quizzes made learning engaging."

★★★★★

— Rishi Verma

"Excellent for CUET preparation. Syllabus coverage was focused and mock tests boosted my confidence. It's the perfect choice for serious students."

★★★★★

— Meenal Singh

"Live classes were interactive and crisp. Sir gave individual attention to every student. Weekly coding sessions really made a difference in my learning."

★★★★★

— Kabir Sharma

"Rohit Sir cleared all my doubts easily. He ensured I understood every topic before moving ahead. His revision tips were crucial for my exam success."

★★★★★

— Anjali Yadav

Mastering Conditional & Looping Constructs in Python

What is a Statement?

In Python, a statement is a single command that the computer can understand and execute. Think of it as a complete sentence in a language—it expresses a full instruction.


There are three main types of statements:


Simple Statement: A single, straightforward action. For example, assigning a value to a variable.


Compound Statement: A group of statements that run together as a single unit, like an if block or a loop.


Empty Statement: A placeholder using the pass keyword, which tells Python to do nothing. It's useful when a statement is syntactically required, but you don't need any code to execute.


Here’s a simple example that uses three statements to get two numbers from a user and print their sum:


Python


# Example of simple statements

n1 = int(input("Enter a number: "))

n2 = int(input("Enter a number: "))

print("The sum is:", n1 + n2)

Control Structures: The Brain of Your Program

Control structures are the real magic. They allow your program to evaluate conditions and repeat actions, making your code dynamic and efficient. There are two primary types you'll use constantly:


Selection (Decision-Making): Decides which path of code to execute based on a condition.


Repetition (Looping): Repeats a block of code as long as a condition is true or for a set number of times.


Let's explore how to use these to build logic into your applications.


Selection Constructs (Conditionals)

Conditionals help your program make decisions. Python provides a clear and readable syntax for this.


1. The if Statement

The if statement is the most basic decision-making tool. The code inside the if block only runs if the specified condition is True.


Python


# This code checks if the number is positive

a = 10

if a > 0:

    print("The number is positive.")

2. The if-else Statement

Use if-else when you want to execute one block of code if a condition is true, and a different block if it is false.


Python


# This code checks if a person is eligible to vote

age = int(input("Enter your age: "))


if age >= 18:

    print("You are eligible to vote.")

else:

    print("You are not eligible to vote yet.")

3. The if-elif-else Ladder

When you have more than two possibilities, you can chain conditions together using if-elif-else. Python will test each condition in order and execute the block for the first one that is True.


Python


# This code assigns a grade based on marks

marks = int(input("Enter your marks: "))


if marks >= 90:

    print("Grade: A")

elif marks >= 75:

    print("Grade: B")

elif marks >= 60:

    print("Grade: C")

else:

    print("Grade: F. Keep improving!")

Looping Constructs (Repetition)

When you need to perform the same task over and over, loops are the perfect tool.


The for Loop

A for loop is used for iterating over a sequence (like a list, a tuple, a dictionary, a set, or a string). It's best used when you know how many times you want the loop to run.


Python


# Prints numbers 1 through 5

for i in range(1, 6):

    print("Number:", i)

You can control the sequence with the range(start, stop, step) function:


Python


# Prints numbers from 10 to 100, counting by 10s

for i in range(10, 101, 10):

    print(i)

The while Loop

A while loop continues to execute a block of code as long as its condition remains True. This is ideal when you don't know in advance how many times you'll need to repeat the action.


Python


# Keeps asking for a number until it's greater than 100

num = 0

while num <= 100:

    num = int(input("Enter a number greater than 100: "))

print("Thank you! You entered:", num)

Advanced Loop Control

Sometimes you need to alter a loop's flow from within. Python provides two statements for this:


break: Immediately terminates the loop and continues execution at the first statement after the loop.


continue: Skips the rest of the code inside the current iteration and proceeds to the next iteration of the loop.


Example using break:


Python


# Stop the loop when the number 3 is found

for i in range(1, 10):

    if i == 3:

        break

    print(i)

# Output: 1 2

Example using continue:


Python


# Skip printing the number 3

for i in range(1, 6):

    if i == 3:

        continue

    print(i)

# Output: 1 2 4 5

Practice Time: Challenge Yourself

The best way to learn is by doing. Try to solve these common problems using the control structures you've just learned.


Write a program to check if a number entered by the user is divisible by both 5 and 3.


Create a program that calculates the total cost of an item (price × quantity). If the total is over $1,000, apply a 10% discount.


Write a loop that displays the sequence: 105, 98, 91, ... down to 7.


Develop a script to find the largest of three numbers provided by the user.


Print the first 10 even numbers using a for loop.


Final Thoughts

Control structures are the backbone of programming logic. They transform simple, sequential scripts into intelligent programs that can react, adapt, and handle complex tasks. By mastering if, for, and while, you gain the power to build truly functional applications.


Start with these fundamentals, practice consistently, and you'll be well on your way to mastering the art of programming. Happy coding!

WhatsApp icon Ask Your Query