Home »
Python
Python Variables: An Easy Guide With Examples
By IncludeHelp Last updated : December 25, 2023
Python Variables
The Python variables are the containers (names of the memory blocks) to store the data.
Creating Python Variables
Just like other programming languages, there is no such command to create a variable. In Python, you can create a variable by assigning the value. Just take a variable and assign a value to it. Python variables are case-sensitive, so you have to take care of them while writing and using variable names.
Example
Create a variable and print its value.
# Creating a variable
name = "Bharat"
# Printing
print("Value of name is:", name)
Output
Value of name is: Bharat
Printing Python Variables
To print Python variable, use the print() method.
Getting Type of Python Variables
To get the type of variables, use the type() method.
Example
Create variables, print their values and types.
x = "Okay!"
y = 10
z = 108.08
# Printing variables and their types
print("x:", x)
print("y:", y)
print("z:", z)
print("Type of x:", type(x))
print("Type of y:", type(y))
print("Type of z:", type(z))
Output
x: Okay!
y: 10
z: 108.08
Type of x: <class 'str'>
Type of y: <class 'int'>
Type of z: <class 'float'>
Python Variables Casting
To cast a value in a specific type, you can do it by using methods like str(), int(), and float().
Example
Create variables by casting the values.
x = str(10)
y = int(10)
z = float(10)
# Printing variables and their types
print("x:", x)
print("y:", y)
print("z:", z)
print("Type of x:", type(x))
print("Type of y:", type(y))
print("Type of z:", type(z))
Output
x: 10
y: 10
z: 10.0
Type of x: <class 'str'>
Type of y: <class 'int'>
Type of z: <class 'float'>
Python Numeric Variables
To create numeric variables in Python, you need to assign the numeric type values to the variables.
Example to create numeric variables
Python program to create and print three numeric variables by assigning the integer, float, and complex type values.
# Python code to create numeric variables
x = 108
y = 108.1008
z = 108 + 1.08j
# Printing variables and their types
print("x:", x)
print("y:", y)
print("z:", z)
print("Type of x:", type(x))
print("Type of y:", type(y))
print("Type of z:", type(z))
Output
x: 108
y: 108.1008
z: (108+1.08j)
Type of x: <class 'int'>
Type of y: <class 'float'>
Type of z: <class 'complex'>
Python String Variables
To create string variables in Python, you need to assign a string value either in single quotes, double quotes, or triple quotes.
Single and double quotes assign a single-line string, and triple quotes assign a multiline string.
Note: You can also use triple-double quotes to create a string variable with a multiline string.
Example to create string variables
Python program to create and print three string variables using single, double, and triple quotes.
# Python code to create string variables
str1 = '''Hello, world!'''
str2 = "Hello, world!"
str3 = '''Hello, world!
How are you?
Welcome to Python Variables Tutorial...'''
# Printing variables and their types
print("str1:", str1)
print("str2:", str2)
print("str3:", str3)
print("Type of str1:", type(str1))
print("Type of str2:", type(str2))
print("Type of str3:", type(str3))
Output
str1: Hello, world!
str2: Hello, world!
str3: Hello, world!
How are you?
Welcome to Python Variables Tutorial...
Type of str1: <class 'str'>
Type of str2: <class 'str'>
Type of str3: <class 'str'>
Python Sequence Types Variables
List, tuple, and range are the sequences in Python. To create sequence type variables, you need to assign the sequence type values in it.
Example to create sequence types variables
Python program to create and print three sequence types variables.
# Python code to create sequence types variables
list_var = [10, 20, 30]
tuple_var = (10, 20, 30)
range_var = range(10)
# Printing variables and their types
print("list_var:", list_var)
print("tuple_var:", tuple_var)
print("range_var:", range_var)
print("Type of list_var:", type(list_var))
print("Type of tuple_var:", type(tuple_var))
print("Type of range_var:", type(range_var))
Output
list_var: [10, 20, 30]
tuple_var: (10, 20, 30)
range_var: range(0, 10)
Type of list_var: <class 'list'>
Type of tuple_var: <class 'tuple'>
Type of range_var: <class 'range'>
Python Dictionary Variables
Python dictionary is a mapping type of data type, to create a dictionary variable, you need to assign a dictionary (the key and values separated by commas inside the curly braces {}).
Example to create a dictionary variable
Python program to create and print a dictionary variable
# Python code to create a dictionary variable
dic = {"name": "Bharat", "Age": 21}
# Printing variable and its
print("dic:", dic)
print("Type of dic:", type(dic))
Output
dic: {'name': 'Bharat', 'Age': 21}
Type of dic: <class 'dict'>
Python Set Types Variables
Set and frozenset are the set types in Python, to create set and frozenset variables, you need to assign sets (values separated by the commas within the curly braces {}), and for the frozenset use the frozenset() method with the set.
Example to create set types variables
Python program to create and print set types variables.
# Python code to create set types variables
set_var = {"India", "USA", "UK"}
frozenset_var = frozenset({"India", "USA", "UK"})
# Printing variable and its
print("set_var:", set_var)
print("frozenset_var:", frozenset_var)
print("Type of set_var:", type(set_var))
print("Type of frozenset_var:", type(frozenset_var))
Output
set_var: {'UK', 'USA', 'India'}
frozenset_var: frozenset({'UK', 'USA', 'India'})
Type of set_var: <class 'set'>
Type of frozenset_var: <class 'frozenset'>
Python Boolean Types Variables
True and False are the Boolean values. To create Python Boolean variables, you need to assign either True or False to them.
Example to create Boolean types variables
Python program to create and print Boolean types variables.
# Python code to create Boolean types variables
x = True
y = False
# Printing variables and their types
print("x:", x)
print("y:", y)
print("Type of x:", type(x))
print("Type of y:", type(y))
Output
x: True
y: False
Type of x: <class 'bool'>
Type of y: <class 'bool'>
Python Binary Types Variables
bytes, bytearray, and memoryview are the Binary values. To create Python binary variables, you need to assign the appropriate values to them.
Example to create binary types variables
Python program to create and print binary types variables.
# Python code to create binary types variables
x = b"Hello"
y = bytearray(5)
z = memoryview(bytes(5))
# Printing variables and their types
print("x:", x)
print("y:", y)
print("z:", z)
print("Type of x:", type(x))
print("Type of y:", type(y))
print("Type of z:", type(z))
Output
x: b'Hello'
y: bytearray(b'\x00\x00\x00\x00\x00')
z:
Type of x: <class 'bytes'>
Type of y: <class 'bytearray'>
Type of z: <class 'memoryview'>
Python None Type Variables
To create a none-type variable in Python, you need to assign None.
Example to create a none type variable
Python program to create and print a none type variable.
# Python code to create a none type variable
x = None
# Printing variable and its type
print("x:", x)
print("Type of x:", type(x))
Output
x: None
Type of x: <class 'NoneType'>