You are on page 1of 4

Python Data Types and Variables

Variables in Python
Variables and data types in python as the name suggests are the values that vary. In a programming
language, a variable is a memory location where you store a value. The value that you have stored
may change in the future according to the specifications.

A Python Variable is created as soon as a value is assigned to it. It does not need any additional
commands to declare a variable in python.
There are a certain rules and regulations we have to follow while writing a variable, lets take a
look at the variable definition and declaration to understand how we declare a variable in python.
Variable Definition & Declaration
Python has no additional commands to declare a variable. As soon as the value is assigned to it,
the variable is declared.
1- x = 10
2- #variable is declared as the value 10 is assigned to it.
There are a certain rules that we have to keep in mind while declaring a variable:
1- The variable name cannot start with a number. It can only start with a character or an
underscore.
2- Variables in python are case sensitive.
3- They can only contain alpha-numeric characters and underscores.
4- No special characters are allowed.
Example:

name = "Devansh"
age = 20
marks = 80.50

print(name)
print(age)
print(marks)

Output:
Devansh
20
80.5

Multiple Assignment

Multiple assignments, also known as assigning values to multiple variables in a single


statement, is a feature of Python.
1. Assigning single value to multiple variables

Eg:
x=y=z=50
print(x)
print(y)
print(z)

Output:
50
50
50
2. Assigning multiple values to multiple variables:

Eg:
a,b,c=5,10,15
print a
print b
print c

Output:
5
10
15 (The values will be assigned in the order in which variables appear)
Python Data Types

Every value has a datatype, and variables can hold values. Python is a powerfully composed
language; consequently, we don't have to characterize the sort of variable while announcing it. The
interpreter binds the value implicitly to its type.

a=5

We did not specify the type of the variable a, which has the value five from an integer. The Python
interpreter will automatically interpret the variable as an integer.

We can verify the type of the program-used variable thanks to Python. The type() function in
Python returns the type of the passed variable.

Consider the following illustration when defining and verifying the values of various data types.

a=10
b="Hi Python"
c = 10.5
print(type(a))
print(type(b))
print(type(c))
Output:
<type 'int'>
<type 'str'>
<type 'float'>

Built-in Data Types

Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:

Text Type: str

Numeric Types: int, float, complex

Sequence Types: list, tuple, range

Mapping Type: dict

Set Types: set, frozenset


Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

None Type: NoneType

Setting the Data Type

In Python, the data type is set when you assign a value to a variable:

Example Data Type


x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = ("apple", "banana", "cherry") tuple
x = range(6) range
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set
x = frozenset({"apple", "banana", "cherry"}) frozenset
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType

You might also like