Skip to main content

Python Variables

Variables are used to store information (data). This data are related to string (text), number and group of data. That are useful to perform specific task. Variable is an identifiers. This is provide an interaction to deal with data and access this data in our program.

Very decent manner python variable are stored data information. There are no need to defined any data type to those variable. When assigning the value of variable then it will allocated and creates. Variable is like a placeholder (tag) of memory location that are managed this memory space. That value are possible to change variable value in during throughout execution of program. That are declared using following syntax.

variable_name = variable_value

python are internally traced of variable data type when assign that its value. For example created few variables.

# bookName are name of variable 
# And there type are string
# "Learn Python" is value 
bookName = "Learn Python"

# bookType      : Variable name
# "programming" : value of variable
# Data Type     : string
bookType = "Programming"

# priceValue    : Variable name
# 594.24        : value of variable
# data type     : Float
priceValue = 594.24

# totalPage   : Variable name
# 400         : value of variable
# data type   : integer

totalPage=400

#boolean values
availableType=True

#display variable values
print(bookName)
print(bookType)
print(priceValue)
print(totalPage)
print(availableType)
Output
Learn Python
Programming
594.24
400
True

Equal (=) operator are used to assign value of variable. Observe that when assigning value of variable. Then interpreter are defined variable scopes and accessibility and also allocates associated memory. If variable are defined and not assign any value then interpreter are not create that variable. See example.

info #uninitialized variable
print(info)
Output
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    info
NameError: name 'info' is not defined

Note that info variable not defined in given scope so avoid this situation and providing initial value of variable.

Python Variable Naming Convention

The name of variable in python programming are support the rule of c, c++, and java programming. There are rule as follows.

1) Name of variable are case-sensitive. For example

#case-sensitive variables
code = "python"
Code = "c"
CODE = "java"
#display value
print(code)
print(Code)
print(CODE)
Output
python
c
java

Note that in given program there are defined 3 different variable (code, Code, CODE).

2) Name of variable is combination of alphabets, numbers and underscores. the name of variable must be start with underscore and alphabets. Number is not allowed at start position. But number can use after that. For example.

# valid variable name
auxiliary=10
Age =18
totalBalance=9000007 #camel case example
_main_acount=007
student1="foo"
department009=11

3) Name of variable is should be not a keyword of python.

4) Spaces are not allowed within the variable name. most of programmer are preferred to separation of variable by underscore and camel-case format (like student Records as studentRecords). This way is provide better readability.

Try to provide meaningful name of variables. Because that are provides more readability and clarity of code that can useful for other programmers.

How to use python variable

Variable are provide availability and reusability. There are possible to modifying variable value. See example.

#initial value of variable
message = "I am happy"

print(message)

#update or modify the value
message ="You are happy?"

print(message)
Output
I am happy
You are happy?

Note that in this example only one variable(message). this are initial value is ("I am happy") after that this value are change by ("you are happy?"). This is a way to change variable value.

Python is allowed to assign multiple variable with multiple value in single statements. Ssee example.

# variable : x,y,z
# values   : 10 20 30
x,y,z=10,20,30
print(x,y,z)
Output
10 20 30

Note that variable are separated by comma (,) operator and associate value are also separated by comma. And only one assignment operator are capable to assign this all variable value. And there are possible to assign multiple variable of single value using following statement

# variable : x,y,z
# value   : 10 
x=y=z=10

#same as
#x=10
#y=10
#z=10

print x,y,z
Output
10 10 10

In this program are using of equal operator to assign the single value to variable.

Literals and variable type

Literals are constant and row data. That are provide a behaviours of variables like that what is type and and what does the meaning of values. There are following category.

Boolean Literals

boolean Literals are have two values (True, False). when perform mathematical operator of this boolean value that True are contain positive integer one (1). And False are contain zero (0). This are mostly used for comparison and conditional statements.

status=True #True Literals
virus=False #False Literals
lollipop=0  #integer Literals

#status=1
candy=status+10 

if(virus==0):
  lollipop=1

print(" status is: ",status)
print(" virus is : ",virus)
print(" candy is : ",candy)
print(" lollipop is : ",lollipop)
Output
 status is:  True
 virus is :  False
 candy is :  11
 lollipop is :  1

Number Literals

data=12 #decimal Literalsv

print(data,type(data))

data=0b00011 #binary Literals
print(data,type(data))

data=19.33
print(data,type(data)) #floating point Literals
Output
12 <class 'int'>
3 <class 'int'>
19.33 <class 'float'>

Basic of variable

Get address of variables: Variable is used to stored value of any data type in python programming. So variable are have an address. and it can be possible to arising question your mind how to get the address of variable in python programming.

myData="info"
print("Address of mydata : {0}".format(hex(id(myData))))
Output
Address of mydata : 0x7fa0d16e2390

Compare single value to multiple variables: There is many way to compare multiple variable into single values. Here given few examples.

#define variable (objects)
alexaAge=13
finchAge=12
childAge=12
#when every variable value are same
if alexaAge==12 and finchAge == 12 and childAge == 12:
    print("Test Case 1 true")
else:
    print("Test Case 1 False")

#When anyone is 12 in this case check using tuples
if 12 in (alexaAge,finchAge,childAge):
    print("Test Case 2 true")

#When anyone is 12 in this case check using sets
if 12 in {alexaAge,finchAge,childAge}:
    print("Test Case 3 true")

Output

Test Case 1 False
Test Case 2 true
Test Case 3 true

Tuples and set elements we can check element is exist or not by using of in operator.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment