- 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)
Add to Bookmark1. Introduction
Conditional statements allow a program to execute different blocks of code based on specific conditions. Python provides the following conditional statements:
ifif-elseif-elif-else- Nested
ifstatements
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 TrueExample
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 FalseExample
num = 10
if num % 2 == 0:
print("Even number")
else:
print("Odd number")Output
Even number4. 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 FalseExample
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: B5. 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 56. 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
Odd7. 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 enter8. 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
ifexecutes a block if the condition isTrue.if-elseprovides an alternative block if the condition isFalse.if-elif-elsechecks multiple conditions.- Nested
ifallows conditions inside anotherif. - Logical operators (
and,or,not) combine conditions.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- OLTP vs. OLAP Databases: Advanced Insights and Query Optimization Techniques
- Grow your business with Facebook Marketing
- Top 10 Blogs of Digital Marketing you Must Follow
- Understanding LLMs (Large Language Models): The Ultimate Guide for 2025
- Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
- Downlaod Youtube Video in Any Format Using Python Pytube Library
- AI in Cybersecurity: The Future of Digital Protection
- Generative AI - The Future of Artificial Intelligence
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- String Operations in Python
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- Government Datasets from 50 Countries for Machine Learning Training
- Convert RBG Image to Gray Scale Image Using CV2
- Datasets for Speech Recognition Analysis
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- 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


