Python Variables Explained for Beginners (With Simple Examples)
Python Variables Explained for Beginners (With Simple Examples)
By Ayush Gupta
Introduction
In our previous posts, we learned how to install Python and how to write our first program. Now it’s time to understand one of the most important concepts in Python — Variables.
This post is written for absolute beginners, so don’t worry if you have never coded before.
---What is a Variable?
A variable is used to store data in a program. You can think of a variable as a container that holds a value.
Creating a Variable in Python
In Python, creating a variable is very easy. You don’t need to mention any data type.
x = 10
Here:
- x → variable name
- = → assignment operator
- 10 → value stored
Printing a Variable
To display the value stored inside a variable, we use the print() function.
x = 10
print(x)
Output:
10
Variables Can Store Different Types of Data
1️⃣ Integer (Whole Numbers)
age = 21
2️⃣ Float (Decimal Numbers)
price = 99.50
3️⃣ String (Text)
name = "Ayush"
Changing the Value of a Variable
The value of a variable can be changed anytime.
x = 5
x = 20
print(x)
Output:
20
Rules for Naming Variables
- Must start with a letter or underscore
- Cannot start with a number
- No spaces allowed
- Python is case-sensitive
name, _value, totalMarksInvalid:
1name, total marks
Multiple Variables in One Line
a, b, c = 1, 2, 3
print(a)
print(b)
print(c)
Why Variables Are Important?
- Store user input
- Perform calculations
- Reuse data
- Make programs dynamic
What You Learned in This Post
- What variables are
- How to create variables in Python
- How to print and change values
- Rules for naming variables
What’s Next?
Now that you understand variables, the next step is learning about data types in detail.
Next Post: Python Data Types Explained (int, float, string, bool)
---Contact Information
👋 About the Author
Ayush Gupta
MSc AI/ML Student | Machine Learning & Python Enthusiast
📧 Email:
aygupta9898@gmail.com
Comments
Post a Comment