惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
小众软件
小众软件
S
SegmentFault 最新的问题
GbyAI
GbyAI
量子位
爱范儿
爱范儿
L
LangChain Blog
Vercel News
Vercel News
A
About on SuperTechFans
腾讯CDC
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
V
Visual Studio Blog
美团技术团队
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Conditional Statements and Control Flow in Python
Kathirvel S · 2026-05-25 · via DEV Community

When we first start learning programming, most beginners immediately jump into writing code. But before writing even a single line, there is something more important:

Thinking.

Programming is not only about typing code.
Programming is about teaching the computer how to think step by step.

A computer is very fast, but it is not intelligent by itself.
It only follows instructions that we give.

So before learning Python syntax, ask yourself:

  • How does a program make decisions?
  • How does a program repeat tasks?
  • How does a computer choose between two options?

This is where Conditional Statements and Control Flow come into the picture.


What is Control Flow?

Control flow means:

The order in which a program executes instructions.

Think of it like traffic on a road.

A program does not run everything at once.
It moves step by step.

Sometimes:

  • it goes forward,
  • sometimes it takes a different path,
  • sometimes it repeats the same path again and again.

Python controls this flow using:

  • if
  • elif
  • else
  • loops like while

These statements help Python make decisions like a human.


Imagine This Real-Life Situation

Suppose three students scored marks:

  • Arun → 15
  • Bala → 20
  • Charles → 45

Now someone asks:

“Who scored the highest marks?”

Before answering, your brain automatically compares all three values.

You think like this:

  • Is Arun greater than Bala and Charles?
  • No.
  • Is Bala greater than Arun and Charles?
  • No.
  • Then Charles must be the highest.

This exact thinking process is what we teach Python.


Conditional Statements in Python

Python uses:

if
elif
else

Enter fullscreen mode Exit fullscreen mode

to make decisions.


Finding the Largest Number

Now let’s convert our thinking into code.

a = 15
b = 20
c = 45

if a > b and a > c:
    print(a)

elif b > a and b > c:
    print(b)

else:
    print(c)

Enter fullscreen mode Exit fullscreen mode


Understanding the Code Slowly

Step 1 — Creating Variables

a = 15
b = 20
c = 45

Enter fullscreen mode Exit fullscreen mode

Variables are containers that store values.

Here:

  • a stores 15
  • b stores 20
  • c stores 45

Step 2 — The if Statement

if a > b and a > c:

Enter fullscreen mode Exit fullscreen mode

Python checks:

  • Is a greater than b?
  • AND
  • Is a greater than c?

The keyword and means:

Both conditions must be True.

Now Python checks:

15 > 20    False
15 > 45    False

Enter fullscreen mode Exit fullscreen mode

Since the condition is False, Python skips this block.


Step 3 — elif Statement

elif b > a and b > c:

Enter fullscreen mode Exit fullscreen mode

elif means:

“Check another condition.”

Now Python checks:

20 > 15    True
20 > 45    False

Enter fullscreen mode Exit fullscreen mode

Again False.

So Python skips this too.


Step 4 — else Statement

else:
    print(c)

Enter fullscreen mode Exit fullscreen mode

else means:

“If nothing above is true, run this.”

So Python prints:

45

Enter fullscreen mode Exit fullscreen mode


Output

45

Enter fullscreen mode Exit fullscreen mode


Why This Program Matters

This small program teaches powerful programming concepts:

  • Decision making
  • Comparison operators
  • Logical thinking
  • Problem solving

This is the foundation of real programming.


Another Example — Comparing Two Numbers

Before coding, think first.

Suppose:

  • a = 10
  • b = 5

Question:

Which number is greater?

Your brain instantly says:

10 is greater.

Now let’s teach Python the same thinking.


a = 10
b = 5

if a > b:
    print(a)

else:
    print(b)

Enter fullscreen mode Exit fullscreen mode


Understanding the Logic

Python checks:

10 > 5

Enter fullscreen mode Exit fullscreen mode

This is True.

So Python executes:

print(a)

Enter fullscreen mode Exit fullscreen mode

Output:

10

Enter fullscreen mode Exit fullscreen mode

Since the condition is True, the else block is ignored.


Truthy and Falsy Values

Now let’s explore something interesting.

Before coding, think carefully:

If I write:

if a:

Enter fullscreen mode Exit fullscreen mode

What is Python checking?

Most beginners get confused here.


The Important Idea

In Python:

  • Some values behave like True
  • Some values behave like False

This is called:

Truthy and Falsy Values


Example

a = -0.1

if a:
    print("a")

else:
    print("b")

Enter fullscreen mode Exit fullscreen mode


What Happens Here?

Python checks:

if -0.1

Enter fullscreen mode Exit fullscreen mode

Since -0.1 is not zero, Python treats it as True.

So output becomes:

a

Enter fullscreen mode Exit fullscreen mode


Important Rule

In Python:

These are considered False:

0
0.0
False
None
''
[]
{}

Enter fullscreen mode Exit fullscreen mode

Everything else is usually True.


Why This Concept Is Powerful

This helps programmers write cleaner and smarter code.

You will see this concept everywhere in real-world Python applications.


Now Let’s Think About Repetition

Imagine printing numbers manually:

print(1)
print(1)
print(1)
print(1)
print(1)

Enter fullscreen mode Exit fullscreen mode

This is boring and repetitive.

A programmer always asks:

“Can I automate this?”

That is why loops exist.


What is a while Loop?

A while loop repeats code while a condition remains True.

Basic syntax:

while condition:
    code

Enter fullscreen mode Exit fullscreen mode


Printing the Same Number Multiple Times

count = 1

while count <= 5:
    print(1, end=' ')
    count = count + 1

Enter fullscreen mode Exit fullscreen mode


Think Before Understanding the Loop

Ask yourself:

  • Where does the loop start?
  • When does it stop?
  • What changes inside the loop?

These three questions help you understand any loop in programming.


Step-by-Step Explanation

Step 1 — Initialize Variable

count = 1

Enter fullscreen mode Exit fullscreen mode

This variable controls the loop.


Step 2 — Condition

while count <= 5:

Enter fullscreen mode Exit fullscreen mode

Meaning:

Repeat while count is less than or equal to 5.


Step 3 — Print Statement

print(1, end=' ')

Enter fullscreen mode Exit fullscreen mode

This prints 1.

The end=' ' keeps everything on the same line.


Step 4 — Increment

count = count + 1

Enter fullscreen mode Exit fullscreen mode

This increases the count value.

Without this line, the loop would never stop.


Output

1 1 1 1 1

Enter fullscreen mode Exit fullscreen mode


Printing Numbers from 1 to 5

Now instead of printing the same number, let’s print the changing value of count.

count = 1

while count <= 5:
    print(count, end=' ')
    count = count + 1

Enter fullscreen mode Exit fullscreen mode


What Happens Internally?

count Output
1 1
2 2
3 3
4 4
5 5

When count becomes 6:

6 <= 5

Enter fullscreen mode Exit fullscreen mode

This becomes False.

So the loop stops.


Output

1 2 3 4 5

Enter fullscreen mode Exit fullscreen mode


Printing with Addition Logic

Now let’s make the loop smarter.

count = 1

while count <= 5:
    print(count + 5, end=' ')
    count = count + 1

print(count)
print(count * 2)

Enter fullscreen mode Exit fullscreen mode


Thinking Process

Instead of printing count, we print:

count + 5

Enter fullscreen mode Exit fullscreen mode

So Python calculates:

count count + 5
1 6
2 7
3 8
4 9
5 10

Output from Loop

6 7 8 9 10

Enter fullscreen mode Exit fullscreen mode


What Happens After the Loop?

After the loop finishes:

count = 6

Enter fullscreen mode Exit fullscreen mode

Because the loop stops only after count becomes greater than 5.


First Print

print(count)

Enter fullscreen mode Exit fullscreen mode

Output:

6

Enter fullscreen mode Exit fullscreen mode


Second Print

print(count * 2)

Enter fullscreen mode Exit fullscreen mode

Python calculates:

6 * 2 = 12

Enter fullscreen mode Exit fullscreen mode

Output:

12

Enter fullscreen mode Exit fullscreen mode


Final Output

6 7 8 9 10
6
12

Enter fullscreen mode Exit fullscreen mode


Common Beginner Mistakes

1. Forgetting Indentation

Python uses spaces to understand blocks.

Correct:

if a > b:
    print(a)

Enter fullscreen mode Exit fullscreen mode

Wrong:

if a > b:
print(a)

Enter fullscreen mode Exit fullscreen mode


2. Infinite Loops

If you forget:

count = count + 1

Enter fullscreen mode Exit fullscreen mode

the loop runs forever.


3. Confusing = and ==

=    assignment
==   comparison

Enter fullscreen mode Exit fullscreen mode

This mistake is extremely common for beginners.


Final Thoughts

Programming is not about memorizing syntax.

It is about:

  • observing problems,
  • thinking logically,
  • and breaking solutions into steps.

Conditional statements teach computers how to make decisions.

Loops teach computers how to repeat tasks efficiently.

Once you truly understand these concepts, you start thinking like a programmer.

And that is the real beginning of learning Python.