python-variables

Introduction to Python Variables Concept

In This Article, You Will Learn About Python Variables.

Python Variables – In Python, variable is the location where a programmer or developer store value.  In another words, variables are firstly created when you assign a value to it. Unlike other programming languages, Python has no command for declaring a variable.

Python can accept following type of variable –

1. Letter can be upper case (A-Z) and lower case (a-z). It should be noted that variable must be begin with letters. It can also have underscore (_).

2. Value can be string, number (int and float) and Boolean value (True and False).

Note: Python variable case is sensitive be careful regarding it. There are some variables name or reserved words which has already been reserved by Python so we can’t use them.

The basic syntax of Python Variable is –

<variable> = <expression>

Creating Variables

Int_number = 1
string = "codingstreets"
float_number = 1.2
Boolean = True

print(Int_number)
print(string)
print(float_number)
print(Boolean) 

Note: Where, equal sign (=) is used to assign a value (right side) to a variable name (left side).

python-variables
As it is shown clearly that all statements have printed.

In Python, A variable can have short name like a and h and it can have a long or descriptive name such as mystery, qualifications and my_name etc.  In nut shell, a variable name must be started with a letter (a,b) or the underscore character (my_name) and it contains only alpha numeric characters and underscores (A-z, 0-9, and _ ).

Example –

myname = "Alex"
Myname = "Alex"
my_name = "Alex"

print(myname)
print(Myname)
print(my_name) 
python-variables
As it is shown clearly that variable can have letter, alpha numeric and underscore character name.

Here are some examples based on variable which can be used to assign a value to a variable name.

NameExample
Uppercase()
Lowercase()
Uppercase with underscore(_)
Lowercase with underscore(_)
String Integer or Int Float (generally in mathematical term it is referred as decimal number)
My
my
My_file
my_file “X”, “Alex”, “hello python world”, “z” etc. 1,27,6,5 etc. 65.7, 4.0, 6.1 etc.

Single or Double Quotes

In Python, variable can be written in both single quote (‘’) and double quote (“”) and value of variable will be same in both quotes. 

Example –

Str = "codingstreets"
# meaning of the variable will be same
Str = 'codingstreets'
print(Str) 
python-variables
As it is shown clearly that value of variable is same.

Get Types

In Python, a variable type can get by using type() function.

Example –

Int = 2
Str = "codingstreets"
print(type(Int))
print(type(Str)) 
python-variables
As it is clearly shown that type of variable is changed.

As Python considers the latest value of a variable therefore, a variable with same name’s value gets change or update automatically. In other words, changing a variable data type into other data type.    

Example –

Int_number = 1 # Int_number is int.
Int_number = "codingstreets" # Int_number is string.
print(Int_number) 
python-variables
As it is shown clearly that value of variable is changed into new value and printed that.

Casting

Specifying a data type of variable is known as Casting.

Example –

Str = str(3)     # Str will be '3'
Int = int(3)     # Int will be 3
Float = float(3) # Float will be 3.0
 
print(Str)
print(Int)
print(Float) 
python-variables
As it is clearly shown that type of each variable is specified with particular data type.

Case-Sensitive

In Python, variables name is case sensitive.

Example –

x = "codingstreets"
print(X)
python-variables
As it is shown clearly that variable was assigned with lowercase x but print statement got uppercase X, hence Python will give us an error.

Note: Be careful! Python Variable case is sensitive. If lowercase variable (add) change into uppercase variable (Add) and capitalization (ADD) then Python will give us an error.

Assign Value to Multiple Variables

In Python, you can assign values to multiple variables in one line:

Example –

x,y,z = "Alex", "Sara", "Thomas"
print (x)
print (y)
print (z) 
python-variables
As it is shown that multiple values can be assigned to variables in a single line.

Variable combination

In Python, to combine variable you can use (+) operator.

Example –

x = "programmer"
print("I am a " + x )
python-variables
As it is shown clearly that a variable can be combined with (+) operator.

(+) operator also can be used to add variables.

Example –

x = "I like to read articles"
z = "on codingstreets.com" 

print(x+z)
python-variables
As it is shown clearly that two string variables can be added with (+) operator.

Example –

x = 20
z = 5

print(x+z)
python-variables
As it is shown clearly that two integer variables can be added with (+) operator.

Note: Python will give us an error, if you try to add string and number.

Example –

name = "Alex"
age = 20

print(name+age) 
python-variables
As it is shown clearly that python shows an error as we try to add string and integer together.

Global Variables – Variable which are created outside the function it’s called global variables. Like in above all examples variable was created outside the function.

Global variables can be used in both inside and outside of functions.

Example –

Create a variable outside of a function, and use it inside the function.

name = "I am Alex."
def myfunc():
     print(name)
myfunc() 
python-variables
As it is shown clearly that variable is called from outside function.

If you create a variable with the same name inside a function, then variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

Example –

Create a variable inside a function, with the same name as the global or outside variable.

name = "I am Alex."
def myfun():
     name = "I am Elisa."
     print(name)
myfunc()
print(name) 
python-variables
As it is shown clearly that as variable called from inside function, variable valued changed in local variable value.

The global Keyword – Normally, when a variable is created inside function, it works as local variable and can be used only inside the function. To use variable as global inside function, use global keyword.

Example –

Use the global keyword, the variable belongs to the global scope:

def myfunc():
     global name
     name = "I am Elisa."
myfunc()
print(name) 
python-variables
As it is shown clearly that global keyword changed local variable to global variable.

Also, use the global keyword to change a global variable inside a function.

name = "I am Alex."
def myfunc():
     global name
     name = "I am Elisa."
myfunc()
print(name) 
python-variables
As it is shown clearly that global keyword changed local variable in global variable.

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Follow Us

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on