Introduction
Variables and Data Types are the fundamental concepts of Python programming. Variables are used to store data, while Data Types define the kind of data a variable can hold. These concepts form the foundation of every Python program, making code simple, organized, efficient, and easy to understand and maintain.
Roman English: Variables data store karte hain, aur Data Types batate hain data kis type ka hai. Ye Python programming ki basic foundation hain.Learning Objectives
After completing this topic, you will be able to:
Understand how to write basic Python code.
Create and use variables in Python.
Store data in variables correctly.
Understand the purpose of Data Types.
Identify different types of Data Types in Python.
Choose the appropriate Data Type for different situations.
Write simple, organized, and efficient Python programs.
Roman English:
Is topic ko complete karne ke baad aap Python me basic code likhna, variables banana aur unme data store karna seekh jayenge. Aap Data Types kya hote hain, unke different types aur kab kaunsa Data Type use karna chahiye, yeh bhi aasani se samajh jayenge.
Table of Contents:
1.What are Variables?
What is a Variable?Why are Variables Used?
Syntax
Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
2.Variable Naming Rules
What are Variable Naming Rules?Rules and Conventions
Valid and Invalid Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
3. What are Data Types?
What are Data Types?Why are Data Types Important?
Types of Data Types
Mutable vs Immutable
Summary
4. Integer (int)
What is int?Syntax
Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
5. Float (float)
What is float?Syntax
Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
6. String (str)
What is str?Syntax
Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
7. Boolean (bool)
What is bool?Syntax
Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
8. List (list)
What is list?Syntax
Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
9. Tuple(tuple)
What is tuple?Syntax
Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
10. Dictionary (dict)
What is dict?Syntax
Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
10. Set (set)
What is set?Syntax
Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
11. Mutable vs Immutable Data Types
What are Mutable Data Types?What are Immutable Data Types?
Key Differences
Examples
Real-World Usage
Common Mistakes
Practice Questions
Summary
12. Useful Python Concepts
type() Functionoperators
Multiple Variable Assignment
Dynamic Typing
Examples
Common Mistakes
Practice Questions
Summary
1.What are Variables?
What is a Variable?
A variable is a named container used to store data or information in a program. The stored data can be used, changed, or updated whenever the program runs.
Simple Example:
Imagine you have a glass.
🥛 Glass = Variable
💧 Water = Data
Just as a glass stores water, a variable stores data. You can also replace the water with juice or milk. In the same way, a variable can store different values.
Python
name="Hamid"
Here: name → Variable, "Hamid" → Data (Value)
Why are Variables Used?
Variables are used to store data so that it can be accessed and reused whenever needed.
In a program, there can be a lot of data such as names, ages, marks, prices, and addresses. Instead of writing these values again and again, we store them in variables with meaningful names. This makes the code easier to read, write, update, and manage.
Syntax:
Python
variable_name = value
Explanation:
variable_name → Name of the variable.
= → Assignment operator used to store the value.
value → The data you want to store.
Examples
Python
name = "Hamid"
age = 19
height = 5.8
is_student = True
Boold_group="B+"
print(name)
print(age)
print(height)
print(is_student)
print(Boold_group)
Output
Terminal
Hamid
19
5.8
True
B+
Real-World Usage
Variables are used in almost every Python program.
For example:
Store a student's name.
Store exam marks.
Store product prices in an online shopping app.
Store a user's email and password during login.
Store a bank account balance.
Store the score in a game.
Common Mistakes
❌ Using spaces in variable names
Python
user name = "Saad"
a g e =19
✅ Correct
Python
user_name = "Saad"
age=19
❌ Starting a variable name with a number or only number
Python
1name = "Raju"
2="Rahul"
✅ Correct
Python
name1 = "Raju"
name_2="Rahul"
❌ Forgetting quotes for text
Python
name = Hamid
✅ Correct
Python
name = "Hamid"
❌ Forgetting double quotes when mixing letters, numbers, or symbols.
Python
username = Hamid123@
✅ Correct
Python
username = "Hamid123@"
Practice Questions
Create a variable to store your name.
Create a variable to store your age.
Create a variable to store your city.
Create a variable to store your favorite color.
Create a variable to store your school or college name.
Summary
A variable is a named container that stores data in a program. It helps us save information so we can use it whenever needed. Instead of writing the same value multiple times, we store it in a variable and use its name. This makes the code cleaner, easier to understand, and easier to maintain. Variables are one of the most basic and important concepts in Python programming.
2.Variable Naming Rules
What are Variable Naming Rules?
Variable Naming Rules are the rules used to create and declare valid variable names in Python.
These rules help programmers write correct, readable, and error-free code.
Rules and Conventions
Rules (Must Follow)
A variable name must start with a letter (A–Z, a–z) or an underscore (_).
It cannot start with a number.
It can contain letters, numbers, and underscores (_).
Spaces and special characters (@, #, %, -, etc.) are not allowed.
Python keywords (such as if, for, class) cannot be used as variable names.
Variable names are case-sensitive (name and Name are different).
Conventions (Recommended)
Use snake_case for variable names.
Choose meaningful and descriptive names.
Keep names short and readable.
Valid and Invalid Examples
✅ Valid
Python
name = "Hamid"
student_name = "Ali"
marks2025 = 95
_price = 100
❌ Invalid
Python
1name = "Hamid"
student-name = "Ali"
student name = "Ali"
class = "Python"
Real-World Usage
Python
student_name = "Hamid"
student_age = 20
total_marks = 450
is_pass = True
Meaningful variable names make programs easier to read, understand, and maintain.
Practice Questions
-
What are variable naming rules?
Can a variable name start with a number? Why?
Which characters are allowed in a variable name?
Why are Python keywords not allowed as variable names?
What does case-sensitive mean?
What is snake_case?
Identify the valid and invalid variable names:
Python
student_name
2marks
_price
user-name
class
Summary
Variable names must follow Python's naming rules.
Use only letters, numbers, and underscores (_).
Never start a variable name with a number.
Do not use spaces, special characters, or Python keywords.
Use snake_case and meaningful names for better readability.
Tip: Remember the difference between Rules (must follow) and Conventions (recommended best practices). This is one of the most common interview and exam questions for Python beginners.
Variables are used to store data so that it can be accessed and reused whenever needed. In a program, there can be a lot of data such as names, ages, marks, prices, and addresses. Instead of writing these values again and again, we store them in variables with meaningful names. This makes the code easier to read, write, update, and manage.
Syntax:
Python
variable_name = value
Explanation:
variable_name → Name of the variable.
= → Assignment operator used to store the value.
value → The data you want to store.
Examples
Python
name = "Hamid"
age = 19
height = 5.8
is_student = True
Boold_group="B+"
print(name)
print(age)
print(height)
print(is_student)
print(Boold_group)
Output
Terminal
Hamid 19 5.8 True B+
Real-World Usage
Variables are used in almost every Python program.
For example:
Store a student's name.
Store exam marks.
Store product prices in an online shopping app.
Store a user's email and password during login.
Store a bank account balance.
Store the score in a game.
Common Mistakes
❌ Using spaces in variable namesPython
user name = "Saad"
a g e =19
Python
user_name = "Saad"
age=19
Python
1name = "Raju"
2="Rahul"
Python
name1 = "Raju"
name_2="Rahul"
Python
name = Hamid
Python
name = "Hamid"
Python
username = Hamid123@
Python
username = "Hamid123@"
Practice Questions
-
Create a variable to store your name.
Create a variable to store your age.
Create a variable to store your city.
Create a variable to store your favorite color.
Create a variable to store your school or college name.
Summary
A variable is a named container that stores data in a program. It helps us save information so we can use it whenever needed. Instead of writing the same value multiple times, we store it in a variable and use its name. This makes the code cleaner, easier to understand, and easier to maintain. Variables are one of the most basic and important concepts in Python programming.
2.Variable Naming Rules
What are Variable Naming Rules?
Variable Naming Rules are the rules used to create and declare valid variable names in Python. These rules help programmers write correct, readable, and error-free code.
Rules and Conventions
Rules (Must Follow)
A variable name must start with a letter (A–Z, a–z) or an underscore (_).It cannot start with a number.
It can contain letters, numbers, and underscores (_).
Spaces and special characters (@, #, %, -, etc.) are not allowed.
Python keywords (such as if, for, class) cannot be used as variable names.
Variable names are case-sensitive (name and Name are different).
Conventions (Recommended)
Use snake_case for variable names.Choose meaningful and descriptive names.
Keep names short and readable.
Valid and Invalid Examples
✅ Valid
Python
name = "Hamid"
student_name = "Ali"
marks2025 = 95
_price = 100
Python
1name = "Hamid"
student-name = "Ali"
student name = "Ali"
class = "Python"
Real-World Usage
Python
student_name = "Hamid"
student_age = 20
total_marks = 450
is_pass = True
Meaningful variable names make programs easier to read, understand, and maintain.
Practice Questions
-
What are variable naming rules?
Can a variable name start with a number? Why?
Which characters are allowed in a variable name?
Why are Python keywords not allowed as variable names?
What does case-sensitive mean?
What is snake_case?
Identify the valid and invalid variable names:
Python
student_name
2marks
_price
user-name
class
Comments
Post a Comment