PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Python · Theory

if-else in Python

← All stacks

Theory

20/268

if-else in Python

if-else chooses a path. The computer looks at a condition. If it is True, it runs the if block. If not, it tries elif, then else. Only the first true branch runs. That is how Pass/Fail and even/odd work.

Shape on the board: if n % 2 == 0: print("Even") else: print("Odd"). Colon after the condition. Body indented. Missing colon → SyntaxError. Wrong indent → IndentationError. = assigns; == compares. Freshers write if n = 2 and lose marks.

Let’s dry-run. n = 7. 7 % 2 is 1, not 0, so if is False. else runs. Output: Odd. Change n to 8 → Even. Say that out loud before you run. That is the viva.

elif is extra checks in order. marks >= 75 → A, elif >= 50 → B, else C. Don’t stack ten separate ifs when elif is the story.

if-else in Python — output — Odd. 7 % 2 is 1, not 0. Change n to 8 → Even.

Diagram
n % 2 == 0 ?
       /        \
     yes         no
    Even        Odd
Exam tip

Trace a pass/fail or even/odd example aloud.

Example

# if-else
n = 7
if n % 2 == 0:
    print("Even")
else:
    print("Odd")

if-else in Python — output: Odd. 7 % 2 is 1, not 0. Change n to 8 → Even.

Short notes

  • DefIf / elif / else
  • RuleFirst true wins
  • RememberNeeds colon + indent
  • UseBoolean conditions

Questions

1

Explain if-else as if you are teaching a junior — definition, then one tiny script.

2

What does the example print, and why?

3

What mistake do freshers make with if-else?

Previous← Boolean in PythonNextLoops in Python →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.