Python Data Types and Variables
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
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'>
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:
In Python, the data type is set when you assign a value to a variable: