Python Variables Explained with Simple Examples
What is a Variable in Python?
A variable is used to store data in a Python program. Think of it as a name given to a value so that we can use that value later.
For example:
name = "Rahul" age = 18 city = "Bareilly"
Here, name, age, and city are variables.
1. Creating a Variable
Python does not require you to declare the variable type separately.
student_name = "Amit" age = 16 marks = 85.5
2. Different Types of Values
A variable can store different types of data:
name = "Amit" # String age = 16 # Integer marks = 85.5 # Float is_student = True # Boolean
3. Changing a Variable
The value of a variable can be changed:
course = "Python" print(course) course = "Java" print(course)
4. Using Variables in Calculations
Variables can also be used in calculations:
a = 10 b = 5 total = a + b print(total)
Output:
15
Practice
Try creating variables for your name, age, school and favourite subject and print them using Python.
Also Read: Why Should Students Learn Python?
Learn Python Through Practical Coding
Learn Python through practical coding and projects at PROMPT Computer Classes.
Explore Python Classes
Comments
Post a Comment