Python variables

python programming language | codigence

Python variables Codigence

What are Python variables?

  • Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
  • Python has no command for declaring a variable.
  • In Python, variables are created when you assign a value to it:

Every variable consists of three components:

Identity
It is the object's address in memory and does not change once it has been created.can be known using id(object).
Type – (data type)
It is a set of values, and the allowable operations on those values.It can be checked using type(object).
Value
A value given to the variable.

Creating Variables

A variable is created the moment you first assign a value to it.
Variables must be assigned a value before using them in expression,
Variables refer to an object and are never declared ahead of time.

Example :


x = 20
y = "Hello world!"
print(x)
print(y)

Variables do not need to be declared with any particular type, and can even change type after they have been set.

Example :


x = 10 # x is of type int
x = "how" # x is now of type str
print(x)

Declaring Variables with data type

If you want to specify the data type of a variable, this can be done with casting.

Example :


x = str(5) # x will be '5'
y = int(5) # y will be 5
z = float(5) # z will be 5.0

Getting the data type of a variable

You can get the data type of a variable with the type() function.

Example :


x = 10
y = "ram"
print(type(x))
print(type(y))

Using Single or Double Quotes?

String variables can be declared either by using single or double quotes:

Example :


x = "Ram"
# is same as
x = 'Ram'

Case-Sensitive in Python

Variable names are case-sensitive. It means 'Ram' and 'ram' are different variables.

Example :


a = 10
A = 10
# both variable are different

Assigning a value to multiple variables

Python allows you to assign a single value to several variables simultaneously.

Example :


a = b = c = 1
print(a,b,c)

Assigning multiple variables in single line.

Python allows you to assign several variables in a single line seperated by comma(,).

Example :


a,b,c = 1,2,"Ram"
print(a,b,c)

Mutable and Immutable Variables

A mutable variable is one whose value may change in place, whereas in an immutable variable change of value will not happen in place. Modifying an immutable variable will rebuild the same variable.

Example :


x=5 #Will create a value 5 referenced by x
y=x #This statement will make y refer to 5 of x
x =x+y

Explanation:

In the first line, as x being integer (immutable type) has been rebuild.
In the last statement, expression on RHS will result into value 10 and when this is assigned to LHS (x), x will rebuild to 10.

Rules for naming a variable:

Programmers choose the names of the variable that are meaningful. A variable name:

  • Can be of any size
  • Have allowed characters, which are a-z, A-Z, 0-9 and underscore (_)
  • should begin with an alphabet or underscore
  • should not be a keyword

It is a good practice to follow these identifier naming conventions::

  • Variable name should be meaningful and short
  • Generally, they are written in lower case letters

To learn python basics see Python Basics. To learn next see Python Data Types