- Python Basics
-
Overview
- Introduction to Python and Installation
- Variables and Data Types
- Conditional Statements (if-else)
- Loops (for, while)
- Functions and Lambda Expressions
- Lists, Tuples, and Dictionaries
- File Handling (Reading/Writing Files)
- Exception Handling (Try, Except)
- Modules and Packages
- List Comprehensions and Generators
Conditional Statements (if-else)
1. Introduction
Conditional statements allow a program to execute different blocks of code based on specific conditions. Python provides the following conditional statements:
if
if-else
if-elif-else
- Nested
if
statements
2. The if
Statement
The if
statement executes a block of code only if the condition evaluates to True
.
Syntax
if condition:
# Code to execute if condition is True
Example
age = 18
if age >= 18:
print("You are eligible to vote!")
Output
You are eligible to vote!
If the condition is False
, the code inside if
is skipped.
3. The if-else
Statement
The else
block executes when the if
condition is False
.
Syntax
if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is False
Example
num = 10
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Output
Even number
4. The if-elif-else
Statement
Use elif
(short for "else if") to check multiple conditions.
Syntax
if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:
# Code if all conditions are False
Example
marks = 85
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
else:
print("Grade: F")
Output
Grade: B
5. Nested if
Statements
You can place one if
statement inside another.
Example
num = 15
if num > 10:
print("Greater than 10")
if num % 5 == 0:
print("Divisible by 5")
Output
Greater than 10
Divisible by 5
6. Short-Hand (if
Statement in One Line)
You can write simple if
statements in a single line.
Example
x = 5
if x > 0: print("Positive number")
For if-else
, use the ternary operator:
num = 7
result = "Even" if num % 2 == 0 else "Odd"
print(result)
Output
Odd
7. Logical Operators in Conditions
You can combine multiple conditions using:
and
(both conditions must beTrue
)or
(at least one condition must beTrue
)not
(negates the condition)
Example
age = 20
has_id = True
if age >= 18 and has_id:
print("Allowed to enter")
Output
Allowed to enter
8. Practical Example: Login System
username = "admin"
password = "1234"
user_input = input("Enter username: ")
pass_input = input("Enter password: ")
if user_input == username and pass_input == password:
print("Login successful!")
else:
print("Invalid credentials")
9. Summary
if
executes a block if the condition isTrue
.if-else
provides an alternative block if the condition isFalse
.if-elif-else
checks multiple conditions.- Nested
if
allows conditions inside anotherif
. - Logical operators (
and
,or
,not
) combine conditions.
Prepare for Interview
- Debugging in Python
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
- Generators in Python
- Requests in Python
- Django
- Flask
- Matplotlib/Seaborn
- Pandas
- NumPy
- Modules and Packages in Python
- File Handling in Python
- Error Handling and Exceptions in Python
- Indexing and Performance Optimization in SQL
Random Blogs
- 5 Ways Use Jupyter Notebook Online Free of Cost
- AI in Marketing & Advertising: The Future of AI-Driven Strategies
- Generative AI - The Future of Artificial Intelligence
- Deep Learning (DL): The Core of Modern AI
- 15 Amazing Keyword Research Tools You Should Explore
- Top 10 Blogs of Digital Marketing you Must Follow
- Store Data Into CSV File Using Python Tkinter GUI Library
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- String Operations in Python
- AI & Space Exploration – AI’s Role in Deep Space Missions and Planetary Research
- Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
- Exploratory Data Analysis On Iris Dataset
- Extract RGB Color From a Image Using CV2
- Loan Default Prediction Project Using Machine Learning
- Big Data: The Future of Data-Driven Decision Making
Datasets for Machine Learning
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset
- Bitcoin Heist Ransomware Address Dataset