Skip to content

Variables


In Python, a variable is a container that stores a value. In other words, variable is the name given to a value, so that it becomes easy to refer a value later on.

Unlike C# or Java, it’s not necessary to explicitly define a variable in Python before using it. Just assign a value to a variable using the = operator e.g. variable_name = value. That’s it. =``variable_name = value The following creates a variable with the integer value.

num = 10

num = 10Try it In the above example, we declared a variable named num and assigned an integer value 10 to it. Use the built-in print() function to display the value of a variable on the console or IDLE or REPL. numprint()REPL In the same way, the following declares variables with different types of values.

num = 10 #integer variable
amount = 78.50 #float variable
greet='Hello World' #string variable
isActive = True #boolean variable

num = 10 #integer variable amount = 78.50 #float variable greet='Hello World' #string variable isActive = True #boolean variableTry it

You can declare multiple variables and assign values to each variable in a single statement, as shown below.

x, y, z = 10, 20, 30
print(x, y, z) #10 20 30

x, y, z = 10, 20, 30 print(x, y, z) #10 20 30Try it In the above example, the first int value 10 will be assigned to the first variable x, the second value to the second variable y, and the third value to the third variable z. Assignment of values to variables must be in the same order in they declared. 10 You can also declare different types of values to variables in a single statement separated by a comma, as shown below.

x, y, z = 10, 'Hello', True
print(x, y, z) #10 Hello True

x, y, z = 10, 'Hello', True print(x, y, z) #10 Hello TrueTry it Above, the variable x stores 10, y stores a string ‘Hello’, and z stores a boolean value True. The type of variables are based on the types of assigned value. x``10``y``'Hello'``z``True Assign a value to each individual variable separated by a comma will throw a syntax error, as shown below.

x = 10, y = 'Hello', z = True

x = 10, y = 'Hello', z = TrueTry it

Variables in Python are objects. A variable is an object of a class based on the value it stores. Use the type() function to get the class name (type) of a variable. type()

num = 10
type(num) #<class 'int'>
amount = 78.50
type(amount) #<class 'float'>
greet='Hello World'
type(greet) #<class 'str'>
isActive = True
type(isActive) #<class 'bool'>

`num = 10 type(num) #<class ‘int’>

amount = 78.50 type(amount) #<class ‘float’>

greet=‘Hello World’ type(greet) #<class ‘str’>

isActive = True type(isActive) #<class ‘bool’>[Try it](/codeeditor?cid=go-3z7dhuhv7) In the above example, num is an object of the int class that contains integre value 10. In the same way, amount is an object of the float class, greet is an object of the str class,isActive is an object of the bool class. numint10amountfloatgreetstrisActivebool` Unlike other programming languages like C# or Java, Python is a dynamically-typed language, which means you don’t need to declare a type of a variable. The type will be assigned dynamically based on the assigned value.

x = 100
print(type(x)) #<class 'int'>
x = 'Hello World!'
print(type(x)) #<class 'str'>

`x = 100 print(type(x)) #<class ‘int’>

x = ‘Hello World!’ print(type(x)) #<class ‘str’>[Try it](/codeeditor?cid=go-3z7fvreq8) The + operator sums up two int variables, whereas it concatenates two string type variables. +`

x = 100
print(x + 10) #110
x = 'Hello'
print(x + ' Python') #Hello Python

`x = 100 print(x + 10) #110

x = ‘Hello’ print(x + ’ Python’) #Hello Python`Try it

Each object in Python has an id. It is the object’s address in memory represented by an integer value. The id() function returns the id of the specified object where it is stored, as shown below. id()

x = 100
id(x)
greet='Hello'
id(greet)

`x = 100 id(x)

greet=‘Hello’ id(greet)`Try it Variables with the same value will have the same id.

x = 100
y = x;
z = 100
print(id(x), id(y), id(z))

`x = 100 y = x; z = 100

print(id(x), id(y), id(z))`Try it Thus, Python optimize memory usage by not creating separate objects if they point to same value.

Any suitable identifier can be used as a name of a variable, based on the following rules:

  1. The name of the variable should start with either an alphabet letter (lower or upper case) or an underscore (_), but it cannot start with a digit.
  2. More than one alpha-numeric characters or underscores may follow.
  3. The variable name can consist of alphabet letter(s), number(s) and underscore(s) only. For example, myVar, MyVar, _myVar, MyVar123 are valid variable names, but m*var, my-var, 1myVar are invalid variable names. myVar``MyVar``_myVar``MyVar123``m*var``my-var``1myVar1. Variable names in Python are case sensitive. So, NAME, name, nAME, and nAmE are treated as different variable names. NAME``name``nAME``nAmE1. Variable names cannot be a reserved keywords in Python. keywords