Python Variable

In this tutorial, we will learn about python variable, data types, and their use cases.

As we learn Keywords and identifiers in previous tutorials now, we will learn about how to use them using variables.

Python Variable

What is PYTHON VARIABLE

Python Variable are used to store values in reserved memory locations. Variables are created at the moment when they are first declared at runtime. It does not need any keywords or any particular data type to declare it.  Variables are declared based on identifiers.

ASSIGNING THE PYTHON VARIABLE

Variables are assigned based on the data types. It will automatically assign a value to a variable when we use an equal sign (=) to the variable.

Declaring And Assigning Value To A Variable

Example 1: Declaring a single Python variable.

software = "Python"
print(Software)

Output:

Python

As you can see, we have stored a string in a variable call software, and then we are printing the variable.

Example 2:

X = "Python" # X is a String
Y = 2 # Y is an Integer
Z = 3.14 # Z is a float
print(X)
print(Y)
print(Z)

Output:

Python
2
3.14

In the example, we are declaring string, Integer, and float to variables.

ASSIGNING MULTIPLE VARIABLES

We can assign multiple variables in different methods, just like assigning multiple variables in statements and multiple variables with multiple values.

Method 1 : Multiple Variables With A Single Value.

x = y = z = 10
print(x)
print(y)
print(z)

Output:

10
10
10

Here we are assigned an int value in more than one variable in one statement.

Method 2 : Multiple Variables And Multiple Values.

x , y , z = 10, 11, 12
print(x)
print(y)
print(z)

Output:

10
11
12
x ,y, z = 2021 , 3.7 , "Python"
print(x)
print(y)
print(z)

Output:

2021
3.7
Python

In this example, we are assigning multiple values in multiple variables.

Changing The Value Of A Variable

We can change the value of the variables at the time of runtime when we reassign them.

X = 9
print(X)
X = "Python"
print(X)

Output:

9
Python

As you change, see in the example first we are assigning the int value to the variable X, and then after that, we are reassigning variable X with a String, and this is how we can reuse the same variable multiple times.

Casting Of Variables

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

X = str(3)
Y = int(3)
Z = float(3)
print(X)
print(Y)
print(Z)

Output:

3
3
3.0

RIGHT WAY TO ASSIGN VARIABLE IN PYTHON

To assign a variable in python or any other language, we need to follow specific rules to declared variables as per data type.

Rule 1: A variable always starts with a letter or an underscore (_).

Example  _x1 = “Pycharm” , _1 = 11  is allowed but 1x = 2 is not allowed.

1x = 2
_x1 = "Pycharm"
_1 = 11

Output:

File "<string>", line 1
 1x = 2
 ^
SyntaxError: invalid syntax</string>

Rule 2: When we assign a string in a variable, it should be Single to Double quotes; they both are the same.

Example:

X = "Pytho"
Y = 'Python3'
print(X)
print(Y)

Output:

Python

Python3

Rule 3: Python is a Case-Sensitive Programming language that means

X = 3 and x = “Python” is completely different.

Example:

X = 3
# This both are different
x = "Python"
print(X)
print(x)

del Function in Python

The Python del keyword is used to del objects like variables and functions m classes. We will see the del keyword in future tutorials as it is going to be used in all other python tutorials. In

Python everything is an object, so the del keyword can also be used to delete tuple, lists, or parts of a list, etc.

Example :

X = "Python"
del X
print(X)

Output :

Traceback (most recent call last):
 File "Del_Keyword.py", line 4, in <module>
print(X)
NameError: name ‘'X' is not defined</module>

As you can see in the example, we have implemented a string variable, and then using the del keyword, we are deleting the variables. Then we are printing the same variable. As we have deleted the variable we got, the error with “X is not defined” means the Python interpreter could not find the variable.

TYPE() FUNCTION IN PYTHON

Python has a lot of built-in functions, and the type() is one of them.

The type() function is used to get the type of the data type of that particular object.

Example 1:

X = 1
print(type(X))

Output:

<class 'int'>

As we can see in the above example, we assign an integer value to a variable, and then we print that variable inside the type function. We are getting the output of that particular variable as data types.

Example 2:

X = 1
Y = 3.7
Z = "Python"
print(type(X))
print(type(Y))
print(type(Z))

Output:

<class 'int'>
<class 'float'>
<class 'str'>

In this example, we are assigned an integer, float, and a string, then we are printing their type, and as we can see, we got the output of their types.

We can also check the type of all the objects and functions on python using the type() function. As an example, we will take the print function.

Example:

print(type(print))

Output:

<class 'builtin_function_or_method'>

Tips for Python Variables

  1. Variable names should have a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).
  2. Create a name that makes sense. For example, computers make more sense than C.
  3. If you are creating a variable name having two words, use underscore to separate them.
  4. Use capital letters possible to declare a constant.
  5. Never use special symbols like !, @, #, $, %, etc.
  6. Do not start a variable name with a digit.
  7. Only use the del function if necessary.
  8. Try to practice type functions in all other python functions you learn in the future.

FAQs

How do you declare a variable in Python?

In Python, you can declare a variable by simply assigning a value to a name. For example, to declare a variable named “x” with a value of 5, you would write: x = 5.

What types of values can be stored in a variable in Python?

Python is dynamically typed, which means that variables can hold values of any type. Some common types of values include integers, floats, strings, lists, and dictionaries.

Can a variable be re-assigned to a different type of value in Python?

Yes, since Python is dynamically typed, a variable can be re-assigned to a different type of value at any time. For example, you can assign an integer to a variable, and then later assign a string to the same variable.

What is the scope of a variable in Python?

The scope of a variable in Python determines where in the code it can be accessed. Variables declared inside a function are only accessible within that function (local scope), while variables declared outside of any function can be accessed from anywhere in the code (global scope).

Can variables be accessed across different modules in Python?

Yes, variables can be accessed across different modules in Python by importing the module that contains the variable. However, the variable must be defined in the module at the global level, and not within a function or class.