Variables in Python with Examples

  • When you declare a variable in python then it must start with a letter or underscore character.
  • You can not start with numbers or digit while declaring python variables.
  • The variables consist of alphabets and numbers i.e. alphanumeric characters and underscore such as [A-Z, a-z, 0–9, _].
  • There is no keyword or command for creating python variables like other programming languages. We can directly put the variable name such as x, y, etc.
  • Python variables are case sensitive as ( phone, Phone, and PHONE) are three different variables.

Below are some examples of variables in Python

a = 5 
b= 10
print(a) print(b)
name = Tom 
Name = Dick
NAME = Herry
print(name)
print(Name)
print(NAME)
name = "Tom"
Name = "Dick"
NAME = "Herry"
print(name)
print(Name)
print(NAME)
a = "Python is" 
b = "high level"
c = "programming"
d = "language"
print(a,b,c,d)
a = "Python is" 
b = "high level"
c = "programming"
d = "language"
print(a+b+c+d)
a = "Python is " 
b = "high level "
c = "programming "
d = "language"
print(a+b+c+d)

Assign values to Python variables

a, b, c = "Book", "Pen", "Bags" 
print(a)
print(b)
print(c)
a=b=c="Book" 
print(a)
print(b)
print(c)

Unpacking of Python variables

cars = ["maruti","BMW","Toyota"] 
a,b,c = cars
print(a)
print(b)
print(c)
a = "High level programming language" 
def myfunction():
print("Python is "+a)
myfunction()
a = "High level programming language" 
def myfunction():
a = "Python is easy to learn"
print(a)
myfunction()
print("Python is "+a)
def myfunction(): 
global a
a = "Programming Language"
myfunction()
print("Python is a high level "+a)
a = 10 
def myfunction():
global a
a = 20
myfunction()
print("The value of a is ",a)

--

--

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bipsmedium

Bipsmedium

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.