- 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
Variables and Data Types
Add to Bookmark1. Introduction to Variables
A variable is a named storage location in memory used to store data. In Python, variables are dynamically typed, meaning you don’t need to declare their type explicitly.
Declaring Variables
x = 10 # Integer
name = "Dynamic Duniya" # String
price = 99.99 # FloatPython automatically determines the data type based on the assigned value.
2. Rules for Naming Variables
- Must start with a letter (a-z, A-Z) or underscore (_).
- Can contain letters, digits (0-9), and underscores (_).
- Cannot be a Python keyword (like
if,else,while, etc.). - Case-sensitive (
ageandAgeare different variables).
Valid examples:
my_var = 5
_name = "Dynamic Duniya"
value123 = 10.5Invalid examples:
3var = 10 # Invalid: cannot start with a number
my-var = 5 # Invalid: hyphens are not allowed
if = 20 # Invalid: 'if' is a reserved keyword3. Data Types in Python
Python has several built-in data types.
1. Numeric Types
- Integer (
int): Whole numbers. - Float (
float): Decimal numbers. - Complex (
complex): Numbers with real and imaginary parts.
num1 = 10 # Integer
num2 = 20.5 # Float
num3 = 3 + 4j # Complex number2. String Type (str)
Strings store text and are enclosed in single (' '), double (" "), or triple (''' or """) quotes.
text = "Hello, Python!"
multiline_text = """This is
a multiline string."""3. Boolean Type (bool)
Booleans hold True or False values.
is_active = True
is_logged_in = False4. Sequence Types
- List (
list): Ordered, mutable collection. - Tuple (
tuple): Ordered, immutable collection. - Range (
range): Sequence of numbers.
fruits = ["apple", "banana", "cherry"] # List
numbers = (1, 2, 3, 4) # Tuple
range_nums = range(1, 10, 2) # Range from 1 to 9 with step 25. Set and Dictionary Types
- Set (
set): Unordered, unique elements. - Dictionary (
dict): Key-value pairs.
unique_numbers = {1, 2, 3, 4, 5} # Set
student = {"name": "Rahul", "age": 25, "grade": "A"} # Dictionary4. Checking Data Types
To check the type of a variable, use type():
print(type(10)) # <class 'int'>
print(type(10.5)) # <class 'float'>
print(type("Hello")) # <class 'str'>
print(type([1, 2, 3])) # <class 'list'>5. Type Conversion
Python allows converting between data types using type casting.
Implicit Type Conversion (Automatic)
x = 5
y = 2.5
result = x + y # Python converts 'x' to float automatically
print(result) # 7.5
print(type(result)) # <class 'float'>Explicit Type Conversion (Manual)
a = 10
b = "20"
# Convert string to integer before addition
c = a + int(b)
print(c) # 30
# Convert integer to string
d = str(a) + b
print(d) # "1020"6. Summary
- Variables store data in Python without explicitly declaring their type.
- Python has different data types like integers, floats, strings, lists, tuples, sets, and dictionaries.
- Use
type()to check the data type. - Type conversion helps convert values between types when needed.
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
- 15 Amazing Keyword Research Tools You Should Explore
- Understanding LLMs (Large Language Models): The Ultimate Guide for 2025
- Deep Learning (DL): The Core of Modern AI
- The Ultimate Guide to Data Science: Everything You Need to Know
- AI in Marketing & Advertising: The Future of AI-Driven Strategies
- The Ultimate Guide to Machine Learning (ML) for Beginners
- How Multimodal Generative AI Will Change Content Creation Forever
- Datasets for Exploratory Data Analysis for Beginners
- Understanding Data Lake, Data Warehouse, Data Mart, and Data Lakehouse – And Why We Need Them
- OLTP vs. OLAP Databases: Advanced Insights and Query Optimization Techniques
- Big Data: The Future of Data-Driven Decision Making
- How to Become a Good Data Scientist ?
- SQL Joins Explained: A Complete Guide with Examples
- String Operations in Python
- Robotics & AI – How AI is Powering Modern Robotics
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


