Python Variables

What is a Variable?

A variable is used to store information, and it can be manipulated. It can also refer to something.

A variable is a container that can store values. It can be identified by its name because there are multiple variables available in the program.

Using its name, we can identify it.

Variables can be stored with different types of values, like strings, numerics, floating values, etc.

It is useful to think of variables as holders that hold data.

Their sole purpose is to name and store information in memory.

This information can, at that point, be utilised all through your program.

Whenever we develop any program, we need to manage the values, so that's why we need a variable.

Python Variable

A Python variable is a container that stores values.

Python does not have any commands to declare variables.

In Java, C++, or C#, first we have to define a variable and fix its data type. like int a, float b, string str, etc.

However, in Python, we are not concerned with data types because variables choose their data types when we apply a value to them.

Example

First = "Hello World"
Second = 25
print(First)
print(Second)

Output

Hello World

25

In Python we can change variables data type at any moment, first we apply string type and after that we want to give numeric value that its possible in pyhton.

Example

First = "Hello World"
print(First)

First = 25
print(First)

First = "Again String"
print(First)

Output

Hello World

25

Again String

In the above example, you see that first we assign the string value "Hello World". After that, we assign integer value 25 and again we assign the string "again string" as the value string. As a result, it can be easily applied to any data type at any time throughout the program. 

Variable Naming Rules

  • A variable name must start with a letter or the underscore.
  • A variable name can not start with a number.
  • A variable name is case-sensitive. means Total and total are different.
  • A variable only contains the alphabet, numbers, and underscore.
  • The keyword Reserved cannot be used as a variable name. 

Variable Type

We can know the datatype of a variable by using the type() function in Python.

Example

First = "Hello World"
Second = 25
print(First)
print(Second)
print(type(First))
print(type(Second))

Output

Hello World
25
<class 'str'>
<class 'int'>

Summary

A variable is a container to store different types of data.


Similar Articles