Skip to main content

Python Variables and Data Types Explained with Examples

🚀 Welcome to NiceinPythonlearn | 📘 Learn Python with Easy Tutorials, Notes & Practice Questions | 💻 Build Real-World Projects | 💬 Enjoying the content?Share your feedback! | 📝 If you have any questions, suggestions, or face any problems, please contact us using the Contact Form or leave a comment. We will do our best to solve it together. 🤝 | ⭐ Follow us for new updates and keep learning!

Python Variables and Data Types Explained with Examples

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() Function
operators
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

  1. Create a variable to store your name.
  2. Create a variable to store your age.
  3. Create a variable to store your city.
  4. Create a variable to store your favorite color.
  5. 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.

🚀 Welcome to NiceinPythonlearn | 📘 Learn Python with Easy Tutorials, Notes & Practice Questions | 💻 Build Real-World Projects | 💬 Enjoying the content?Share your feedback! | 📝 If you have any questions, suggestions, or face any problems, please contact us using the Contact Form or leave a comment. We will do our best to solve it together. 🤝 | ⭐ Follow us for new updates and keep learning!

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

Question Practice Tips 📝 Try to solve the question yourself → ✅ Check your answer → 🤖 Use ChatGPT or Gemini AI to verify whether your answer is correct or incorrect. If your answer is wrong or you don't understand the question, review the concept and try solving it again on your own. Keep practicing until you can solve it without help.

  1. What are variable naming rules?
  2. Can a variable name start with a number? Why?
  3. Which characters are allowed in a variable name?
  4. Why are Python keywords not allowed as variable names?
  5. What does case-sensitive mean?
  6. What is snake_case?
  7. 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.

Comments

Popular posts from this blog

Python input and output (I/O) Statements Explained with Examples

Introduction Python Input and Output are the foundation of interactive programming. Input allows users to enter information, while output displays results on the screen. By learning these concepts, you can create programs that communicate with users, solve real-world problems, and build applications that respond to user input effectively. Hinglish:Python Input aur Output interactive programming ki foundation hai. Input ka use karke user se information li jaati hai, aur Output ka use karke screen par result dikhaya jaata hai. In concepts ko seekhne ke baad aap aise programs bana sakte ho jo users se communicate karein, real-world problems solve karein, aur user ke input ke hisaab se response dene wale applications develop karein. Learning Objectives After completing this lesson, you will be able to: 1.Understand the concept of Input and Output in Python. 2.Use the input() function to take data from users. 3.Use the print() function to display info...

Python Print Statement and Comments Explained with Examples

1) Print Statement Function in Python A print statement Function is used to show output on the screen. In Python, we write print() with a small letter p, not a capital letter. Roman : Print statement Function screen par output dikhane ke liye use hota hai. Python me print() small letter p se likhte hain, kyunki Python case-sensitive hota hai. Example of Print Statement Function in Python : print("Hello World") Output(show on Terminal): Hello World Another Example of Print Statement Function in Python : print(10) print(2.0) print(True) print(10+5) print(2*5) print(0-0) Output(show on Terminal): 10 2.0 True 15 10 0 2) Comments in Python (Single and Multiples): Single Line Comments using Hash(#) in Python A comment is used to explain a line of code. In Python, a comment is written using #(hash...