Python Basics

Introduction to Python
Variables and Data Types
Operators
Conditional Statements
Loops
Functions

Your Progress

45% Complete 3/7 Lessons

Conditional Statements

Conditional statements allow your program to make decisions based on certain conditions. In Python, we use if, elif, and else statements.

Basic 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 no conditions are True

Example

Let's write a program that checks if a number is positive, negative, or zero:

Interactive Example

Output

Click "Run Code" to see the output here.

Pro Tip

Python uses indentation to define code blocks. Make sure to use consistent indentation (4 spaces is recommended) for your if statements.

Exercise: Grade Calculator

Write a program that takes a student's score (0-100) as input and prints their grade based on the following criteria:

  • 90-100: A
  • 80-89: B
  • 70-79: C
  • 60-69: D
  • Below 60: F

Your Solution

Community Discussion

JaneDoe 2 hours ago

I'm confused about when to use elif vs multiple if statements. Can someone explain?

PythonTutor 1 hour ago

Great question! Use elif when the conditions are mutually exclusive (only one can be true). Use multiple if statements when multiple conditions could be true and you want to check all of them.

Made with DeepSite LogoDeepSite - 🧬 Remix