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

THOR311

ceri188

ceri188

ceri188

KUPU178

HOKI311

RATU311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

ratu311

mix parlay

ceri188

slot online

kupu178

KUPU178

RATU311

THOR311

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

kupu178

hoki311

THOR311

hoki311

togel online

kupu178

slot mahjong

ceri188

slot server thailand

ratu311

ceri188

ceri188

RATU311

punjabi virasat

mimcord.com

Centrum Medyczne

www.vrcorporate.in

phbalance.vn

KUPU178

daftar slot gacor

hoki311

togel hongkong

kupu178

slot mahjong

ratu311

slot gacor

kupu178

kupu178

ceri188

kupu178

ceri188

KUPU178

KUPU178

KUPU178

RATU311

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

ratu311

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

RATU311

toto macau

KUPU178

slot gacor

HOKI311

sabung ayam online

RATU311

SLOT LUCKY NEKO

CERI188

Slot Gacor

KUPU178

RATU311

RATU311

Link Spaceman

CERI188

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

Live Casino

HOKI311

CERI188

CERI188

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

CERI188

HOKI311 SLOT

TOTO TOGEL TOKYO

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

Toto Togel

KUPU178

Slot Mahjong

CERI188

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

SLOT ONLINE

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 10 GAME SLOT TERBAIK

KUPU178

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

SITUS BOLA

CERI188

KUPU178

RATU311

THOR311

HOKI311

KUPU178

RATU311

THOR311

CERI188

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

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

RATU311

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311 PRO

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

RATU311

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

situs judi bola

HOKI311

RATU311

THOR311

HOKI311

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

slot thailand

judi bola

KUPU178

slot gacor

slot gacor

yakuza303

slot gacor

RATU311

THOR311