UX Python Top Advertisment

Python Variable Types

Last Update : 14 Aug, 2022 Python, Programming

In this tutorial, we will learn about Python variable types.

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.


Assigning Values to Python Variables

Python does not need an explicit declaration to reserve memory space for variables. When You assign a value to a variable, the declaration happens automatically. Python use equal sign (=) to assign values for variables.

The operand which the left of the = sign is the variable name. Also, the operand on which the right of the = sign is the assigned value to the variable.

An example of assigning values to Python variables is as follows.

weight = 125       # An integer value assignment
height = 158.54    # A floating point assignment
site = "uxPython"  # A string 

print(weight)
print(height)
print(site)

Here, 125, 158.54, and "uxPython" are the values assigned to weight, height, and site variables, respectively. The following result produces by this example.

125
158.54
uxPython

 

Multiple Variables Assignment

We can assign a single value to multiple Python variables simultaneously. An example of assigning multiple values is as follows.

w = x = y = x = "Python Programming"

print(w)
print(x)
print(y)
print(z)

Here, a string object is created with the value of "Python Programming", and all four variables are assigned to the same memory location.

Also, We can assign multiple values to multiple variables. An example of assigning multiple values to multiple variables is as follows.

w, x, y, z = "UX Design", "uxPython", 404, 101

print(w)
print(x)
print(y)
print(z)

Here, two string objects with values "UX Design" and "uxPython" are assigned to variables w and x respectively. Also, two integer objects with values 101 and 404 are assigned to variables y and z.

 

Python Standard Data Types

The data can exist in many types. These data types can store in computer memory.

For example, When considering a Book, the book name is stored as alphanumeric characters and, the book price is stored as a numeric value.

There are various standard data types available in Python. The followings are the five standard data types of Python.

  • String
  • Numbers
  • List
  • Dictionary
  • Tuple

 

Python Strings Data Type

Python string is surrounded by either double quotation marks ( " " ) or single quotation marks ( ' ' ).

The string "UXPython" is the same as 'UXPython'.

Also, We can use the print() function to display Python string literals. 

For example -:

print("UXPython")
print('UXPython')

This program produces the following result -:

UXPython
UXPython

 

Python Numbers Data Type