Programmer Coding

Python Variables

Python Variables?

Python variables are reserved memory locations used to store values ​​in Python programs. This means that when you create a variable you will save some space in memory. The Python interpreter allocates memory based on the data type of the variable and determines what can be stored in memory. Therefore, by assigning data variables to Python variables, you can store numbers, numerals, or symbols in these variables.

Creating Python Variables

To save memory, Python variables do not need to be declared explicitly, or you can say you want to create a variable. Python variables are created when you assign them a value. The equal sign (=) is used to assign a value to a variable.

The operand to the left of the = operator is the name of the variable, and the operand to the right of the = operator is the value stored in the variable.

Integer variable

x = 10 # Creates an integer variable

Float variable

pi = 3.14 # Creates a floating point variable

String Variable

name = "programmercoding.com" # Creates a string variable

Boolean variable

is_active = True # Creates a boolean variable

Printing Python Variables

As soon as we create a Python variable and assign a value to it, we are able to print it using print() feature. Following is the extension of preceding instance and suggests the way to print one-of-a-kind variables in Python:

x = 10 # Creates an integer variable pi = 3.14 # Creates a floating point variable is_active = True # Creates a boolean variable print(x) print(pi) print(is_active)

Getting Type of a Variable

You can get the data type of a Python variable using the python built-in function type() as follows.

x = 10  # Creates an integer variable

pi = 3.14  # Creates a floating point variable

is_active = True  # Creates a boolean variable

print(type(x))  # Output: <class 'int'>

print(type(pi))  # Output: <class 'float'>

print(type(is_active))  # Output: <class 'bool'>

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top