What is Python Variable | How to use Python Variables

Overview – Python Variable

Let’s walk you through Python Variable and learn behind-the-scenes; rules of using variables in Python with exciting variable examples.

What’s Next? – Python print() Function

In this article, I’ll introduce what a variable in Python is along with highlighting the rules of Python variables. Finally, before going off, I’ll explore various Python variable examples with different Python data types.

Pre-requisite: Python print() Function

Table of Contents

Watch Video Explanation

What is the Python Variable?

A variable in Python is like a box or something you can store different values in it. A variable is defined with a name and then a value is assigned. Therefore, variables work as placeholders to store data.

Variables in Python do not need to explicitly declare the type of variable. Once the value is assigned, Python automatically knows what type of value it stored.

python-variable

How to Declare a Variable in Python?

In Python, a variable is declared once the value is assigned to it.

Syntax:

				
					Variable_name = Value
				
			

E.g., Math_marks = 89

Explanation: On the left side, you have to write a variable name and by using the assignment operator (=) you have to enter a value on the right side.

What are the Rules for defining Python Variables?

Let’s discuss some rules to define the variable in Python. From variable names to Python reserved keywords, I’ll take a look at each rule with Python variable examples.

Rules are given below:

1 – A variable name can start only with the letter or underscore.

2 – A variable name can never start with a number.

3 – A variable name can contain only alpha-numeric characters (A-Z, a-z, 0-9) and an underscore (_).

4 – Variables are case-sensitive i.e., (Name, name, NAME) are considered 3 different variables.

5 – A variable name cannot be any of the Python Reserved-keywords.

What are various examples of Variables in Python?

A variable in Python is like a box or something you can store different values in it. A variable is defined with a name and then a value is assigned. Therefore, variables work as placeholders to store data.

Unlike many other programming languages, variables in Python do not need to explicitly declare the type of variable. Once the value is assigned, Python automatically knows what type of value it stored.

Let’s discuss each rule one by one with the examples:

#Rule 1: Python variable naming rules.

				
					box = 'Chips & Chocolates'
_box = "Chips AND Chocolates"

print(box)
print(_box)

#Output:
Chips & Chocolates
Chips AND Chocolates
				
			

Explanation: In the above example, the first variable is defined only with letters i.e., box, whereas; the second variable is defined starting with an underscore + letters i.e.,  _box. Both variables are valid because they follow the Python variable naming rule.

Python print() function is used to display the output of the variables.

#Rule 2: Python variable naming rules.

				
					1box = 'Chips & Chocolates'
$box = 'Chips & Chocolates'

print(1box)
print($box)

#Output:
SyntaxError: invalid decimal literal
SyntaxError: invalid syntax
				
			

Explanation: In the above example, the first variable is defined starting with a number followed by letters i.e., 1box, whereas; the second variable is defined starting with a special character followed by letters i.e., $box.

Both variables are invalid because a variable can’t be defined starting from a number or any special characters (!, @, #, %, $, etc.)

Note: We know that a variable cannot be started with a number or any special character, but we can add a number at the end of the variable name.

				
					box1 = 'Chips & Chocolates'
print(box1)

#Output:
Chips & Chocolates
				
			

#Rule 3: Python variable with alpha-numeric characters (A-Z, a-z, A-z, 0-9, _).

				
					BOX = "Chips & Chocolates"
box = 'Chips and Chocolates'
Box = 'chips and chocolates'
bo8 = 'Chip & Chocolate'
box_weight = 0.5

print(BOX)
print(box)
print(Box)
print(bo8)
print(box_weight)
				
			
				
					#Output:
Chips & Chocolates
Chips and Chocolates
chips and chocolates
Chip & Chocolate
0.5
				
			

Explanation: In the above example,

  1. The first variable is defined in upper case i.e, BOX
  2. The second variable is defined in lowercase i.e, box
  3. The third variable is defined with only the first letter in upper case and the rest in lower case i.e, Box
  4. The fourth variable is defined in the combination of letter + number i.e, bo8
  5. The fifth variable is defined in the combination of letter + underscore i.e, box_weight

#Rule 4: Python variable is case-sensitive

				
					BOX = "Chip & Chocolate"
box = 'Chips and Chocolates'
Box = "Chips & Chocolates"

print(BOX)
print(box)
print(Box)
				
			
				
					#Output:
Chip & Chocolate
Chips and Chocolates
Chips & Chocolates
				
			

Explanation: In the above example, the variable name looks same but they are not, since variable names are case-sensitive, therefore; BOX, box, Box are considered 3 different variables. Each first letter of the variables is in a different case i.e., first- upper case (B), second – lower case (b), and third – Capital case (B).

#Rule 5: Python variable name cannot be any Python reserved keywords

				
					for = "Chip & Chocolate"
is = "Chip & Chocolate"

print(for)
print(is)
				
			
				
					#Output:
invalid syntax
invalid syntax
				
			

Explanation: In the above example, two variable names are defined – for and is, both are invalid variable names because both keywords are Python-reserved keywords.

Note: Python reserved keywords have special meanings or purpose to use in Python, it can’t be used for personal usage in Python language.

Learn more: Python reserved-keywords

What are Python Variables valid & invalid examples?

Python Variables valid examples:

				
					Channel = "codingstreets"
CHANNEL = "codingstreets"
channel = "codingstreets"
Channel_name = "codingstreets"
Channelname = "codingstreets"
_channel_name = "codingstreets"
Channe1 = "codingstreets"
				
			

Python Variables valid examples:

				
					$ubscribe = True
subs cribe = True
subs-cribe = True
1channel = True
Subscr!be = True
				
			

How to Assign Multiple Values in Python Variable?

The Python language allows to assign multiple values to multiple variables, in just one single line.

				
					My_name, channel_name, website_url = "Deepak", "codingstreets", "codingstreets.com"

print(My_name)
print(channel_name)
print(website_url)

				
			
				
					#Output:
Deepak
codingstreets
codingstreets.com
				
			

Note: It is compulsory to have an equal number of variables and values, else Python will return an error.

What’s Your Task?

Now it’s your call… Imagine your variable is a magic box 🪄 Let’s write what secret stuff you stored in the box.

You can write your secret stuff stored in the box on the YouTube Channel’s video Variables in Python.

Conclusion – Python Variable

So, did you enjoy hiding your secret stuff in the box? Ohh! I meant to say, that it would be a great experience to declare your first variable in Python, right? From Python variable names to variable rules, we covered everything along with various examples and valid & invalid Python variables examples.

Recent Articles