- 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
- Mastering Python in 2025: A Complete Roadmap for Beginners
- Data Analytics: The Power of Data-Driven Decision Making
- Navigating AI Careers in 2025: Data Science, Machine Learning, Deep Learning, and More
- Python Challenging Programming Exercises Part 2
- Compiler SQL Online: A Beginner-Friendly Guide to Running SQL Queries Anywhere
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
- Where to Find Free Datasets for Your Next Machine Learning & Data Science Project
- The Ultimate Guide to Machine Learning (ML) for Beginners
- Understanding LLMs (Large Language Models): The Ultimate Guide for 2025
- Understanding SQL vs MySQL vs PostgreSQL vs MS SQL vs Oracle and Other Popular Databases
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- The Ultimate Guide to Data Science: Everything You Need to Know
- Best Platform to Learn Digital Marketing in Free
- Important Mistakes to Avoid While Advertising on Facebook
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


