Python Variables

Last Update : 20 Aug, 2022 Python, Programming

In this tutorial, You will learn about Python variables and the usage of the variables.

The meaning of a variable is reserved memory location to store values. Also, a variable in a Python program provides data for processing the computer.

The Python interpreter allocates memory to variables and decides what can be stored in the reserved memory based on the data type of a variable. Therefore, You can store decimals, characters, or integer values by assigning different data types to variables.


Creating Python Variables

There is no specific command for declaring a variable in Python. When a variable is created moment, you first assign a value to that variable.

Python uses the value assigned to a variable to determine the data type of that variable.

Steps of declaring a variable in Python.

  1. Name the variable.
  2. Assign the variable the required value.
  3. You do not need to declare variable type. The data type of the variable will be automatically determined from the value assigned.

An example of creating a Python variable is as follows.

x = 125  # int type variable
y = "Nirosh" # srt type variable
z = 5.24 # float type variable


Python Casting

Casting is the way of converting the data type of Python variable into a certain data type in order to do the operations to be performed by users.

For example -:

x = str(101)    # x variable value will be '101'
y = int(224)    # y variable value will be 224
z = float(87)  # z variable value will be 87.0


Get the Variable Type

To get the data type of any object, You can use the type() function.

This function returns the data type of the object passed to it as an argument.

For example -:

# Print the data type of the variables x and y.
x = " Hello UXPython"

y = 87

This program produces the following result -:

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


Python String with Single or Double Quotes

You can represent a Python string in two ways. It can be identified as a string enclosed either with single quotes or double quotes.

Both single and double quotes are correct. Also, you can use them according to your requirement.

For example -:

x = "Nirosh"
x = 'Nirosh'

In some cases, You have to use these single quotes and double quotes together in the same string. In those cases, you can use single and double quotes alternatively so that they can be distinguished.

For example -:

# This gives an error
print('It's UXPython!')

The above program gives a Python syntax error. Python interpreter considers the single quote after “it” as the end of the string. Also, the rest part is not considered as the part of a string.

The above program can be corrected as follows.

print("It's UXPython!")

This program produces the following result -:

It's UXPython!


Is Python Case-Sensitive?

Python is a case-sensitive programming language which means that a variable named A and a variable named a are two separate variables.

The meaning of case-sensitive is having differentiation between lowercase and uppercase letters.

For example, the variable name which you are comparing should exactly be the same as a variable name that is to be compared. But, both variable names can be either in upper case or lower case.

# A will not overwrite a and, this will create two variables.
a = 404
A = "Nirosh"



This tutorial discusses the basics of python variables, such as creating Python variables, Python casting, getting the variable types, using single quotes and double quotes with strings, and case sensitivity of Python programming language. Also, you can obtain more tutorials about Python strings in future articles. 

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.