Variables in Python with Examples

Variables in Python with Examples

Variables in Python are declared using some set of rules as given below

  • 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.

Also read, Comments in Python 3

Below are some examples of variables in Python

Example 1:-

a = 5
b= 10
print(a)
print(b)

Output:-

5
10

Example 2:-

a = 5
a = 5.3
print(a)

Output:-

5.3

Example 3:-

name = Tom
Name = Dick
NAME = Herry
print(name)
print(Name)
print(NAME)

Output:-

File “d:\python\python_programming\01_python.py”, line 38, in
name = Tom
NameError: name ‘Tom’ is not defined

So, the correction of the above example 3 is as shown below

name = "Tom"
Name = "Dick"
NAME = "Herry"
print(name)
print(Name)
print(NAME)

Output:-

Tom
Dick
Herry

Also, we can display the multiple variables inside the print() function using a comma as shown below

Example 4:-

a = "Python is"
b = "high level"
c = "programming"
d = "language"
print(a,b,c,d)

Output:-
Python is high level programming language

Also, we can display the above output with the help of the (+) operator as shown below

Example 5:-

a = "Python is"
b = "high level"
c = "programming"
d = "language"
print(a+b+c+d)

Output:- Python ishigh levelprogramminglanguage

You have noticed that the output does not contain the whitespaces. Follow the below example for the correct output.

a = "Python is "
b = "high level "
c = "programming "
d = "language"
print(a+b+c+d)

Output:- Python is high level programming language.

Now, the output is correct. So do not forget to include the spaces after each value of variables except the last variable.

Assign values to Python variables

We can assign more than one value in multiple variables as shown below

Example 6:-

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

Output:-
Book
Pen
Bags

Also, we can assign a single value to multiple variables

Example 7:-

a=b=c="Book"
print(a)
print(b)
print(c)

Output:-
Book
Book
Book

Unpacking of Python variables

Sometimes variables consist of multiple values and then we can assign that variable to multiple variables. Now, if you output the variables using the print() function then you can see, that each variable will produce the values of the first variable.

Example 8:-

cars = ["maruti","BMW","Toyota"]
a,b,c = cars
print(a)
print(b)
print(c)

Output:-

maruti
BMW
Toyota

Global variables in Python 3

When a variable is declared outside the function then it is considered a global variable. We can use this variable inside the function as shown below

Example 9:-

a = "High level programming language"
def myfunction():
   print("Python is "+a)
myfunction()

Output:-

Python is High level programming language

Local variables in Python 3

When a variable is declared inside the function then it is considered a local variable. We can display the values of the local variable and global variable together as shown below

Example 10:-

a = "High level programming language"
def myfunction():
   a = "Python is easy to learn"
   print(a)
myfunction()
print("Python is "+a)

Output:-

Python is easy to learn
Python is High level programming language

Global keyword in Python 3

The global keyword is used to make a variable global which means we can change the local variable into global and we can access the local variable outside the function also.

Example 11:-

def myfunction():
   global a 
   a = "Programming Language"
myfunction()
print("Python is a high level "+a)

Output:-

Python is a high level Programming Language

If you want to change the value of a global variable inside a function then you can do so with the help of a global keyword as shown below

Example 12:-

a = 10
def myfunction():
   global a
   a = 20
myfunction()
print("The value of a is ",a)

Output:-

The value of a is 20

Please note that we can concatenate strings and numbers together with the help of comma (,)

Also you can download the latest version of Python from here

Conclusion:- I hope this tutorial will help you to understand the overview of Python variables. If there is any doubt then please leave a comment below

Leave a Comment

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

HOKI311

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

RATU311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

KUPU178

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

KUPU178

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311