- 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
1. 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 # Float
Python 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 (
age
andAge
are different variables).
Valid examples:
my_var = 5
_name = "Dynamic Duniya"
value123 = 10.5
Invalid examples:
3var = 10 # Invalid: cannot start with a number
my-var = 5 # Invalid: hyphens are not allowed
if = 20 # Invalid: 'if' is a reserved keyword
3. 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 number
2. 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 = False
4. 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 2
5. 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"} # Dictionary
4. 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
- 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
- Important Mistakes to Avoid While Advertising on Facebook
- Grow your business with Facebook Marketing
- Data Analytics: The Power of Data-Driven Decision Making
- Python Challenging Programming Exercises Part 3
- Top 10 Knowledge for Machine Learning & Data Science Students
- Types of Numbers in Python
- Role of Digital Marketing Services to Uplift Online business of Company and Beat Its Competitors
- Generative AI - The Future of Artificial Intelligence
- The Ultimate Guide to Machine Learning (ML) for Beginners
- AI & Space Exploration – AI’s Role in Deep Space Missions and Planetary Research
- What is YII? and How to Install it?
- Python Challenging Programming Exercises Part 2
- Exploratory Data Analysis On Iris Dataset
- Top 15 Recommended SEO Tools
- Mastering Python in 2025: A Complete Roadmap for Beginners
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