VARIABLES & TYPES
A variable is a container that stores values that we can access or change.
A variable has been derived from a word “vary” which means it keeps changing
during the execution of a program.
A variable is stored in the computer's memory. Every variable has a type,
and this type defines the format and behaviour of the variable.
For example,
x=3
it will automatically assign a value of 3 to a variable named x, which is an
integer.
A variable has three main components:
● Identity of the variable
● Type of the variable
● Value of the variable
Identity of the variable:
It refers to the variable’s address in the memory which doesn’t change once it
has been created. The address of an object can be checked using the method
id(variable).
Syntax: >>>id(variable)
Type of a variable:
By type we refer to the data type of the variable.
Python can easily differentiate one data type from another when we write and
run the statement.
Types of data type:
1. Number: Number data type stores numerical value. Python supports
three built-in numeric data types: - integer/long, floating point number
and complex number.
, 2. Int(integer): Integer represents the whole number without any fractional
part. They can be positive or negative and have unlimited size in python.
Example:- -6,567,4,0 etc
3. Float(floating point number) : floating point numbers are written with a
decimal point that separates the integer from the fractional number.
Example:- 3.14, 6.0, -45.3,2887.6 etc.
4. Complex numbers: complex numbers in python are made up of pairs of
real and imaginary numbers. They take the form ‘x+yj’ where ‘x’ and ‘y’
are the real part and j represents the imaginary part. Example:- 2+5j print
(x.real , x.imag).
5. str (string) : A string is a sequence of characters that can be a
combination of letters, numbers and special symbols , enclosed within
quotation marks, single or double or triple (‘ ‘ or “ “ or ‘“ “‘) . Example :-
str=“ Hello Python”
print(str)
Output - Hello Python
6.bool(Boolean) : Boolean data type reprints one of the two possible values,
True or False . A Boolean true value is non-zero, non-null and non-
empty.
Example:- bool_1= (6>10)
Print(bool_1) output - false
7. None: This is a special data type with a single value . Python doesn't
display anything when we give a command to display the value of a variable
containing value as None. On the other hand, None will be displayed by
printing the value of the variable containing None using print( ) statement.
type( )
To determine the type of a variable , i.e., what type of value does it hold then
type( ) function is used.
A variable is a container that stores values that we can access or change.
A variable has been derived from a word “vary” which means it keeps changing
during the execution of a program.
A variable is stored in the computer's memory. Every variable has a type,
and this type defines the format and behaviour of the variable.
For example,
x=3
it will automatically assign a value of 3 to a variable named x, which is an
integer.
A variable has three main components:
● Identity of the variable
● Type of the variable
● Value of the variable
Identity of the variable:
It refers to the variable’s address in the memory which doesn’t change once it
has been created. The address of an object can be checked using the method
id(variable).
Syntax: >>>id(variable)
Type of a variable:
By type we refer to the data type of the variable.
Python can easily differentiate one data type from another when we write and
run the statement.
Types of data type:
1. Number: Number data type stores numerical value. Python supports
three built-in numeric data types: - integer/long, floating point number
and complex number.
, 2. Int(integer): Integer represents the whole number without any fractional
part. They can be positive or negative and have unlimited size in python.
Example:- -6,567,4,0 etc
3. Float(floating point number) : floating point numbers are written with a
decimal point that separates the integer from the fractional number.
Example:- 3.14, 6.0, -45.3,2887.6 etc.
4. Complex numbers: complex numbers in python are made up of pairs of
real and imaginary numbers. They take the form ‘x+yj’ where ‘x’ and ‘y’
are the real part and j represents the imaginary part. Example:- 2+5j print
(x.real , x.imag).
5. str (string) : A string is a sequence of characters that can be a
combination of letters, numbers and special symbols , enclosed within
quotation marks, single or double or triple (‘ ‘ or “ “ or ‘“ “‘) . Example :-
str=“ Hello Python”
print(str)
Output - Hello Python
6.bool(Boolean) : Boolean data type reprints one of the two possible values,
True or False . A Boolean true value is non-zero, non-null and non-
empty.
Example:- bool_1= (6>10)
Print(bool_1) output - false
7. None: This is a special data type with a single value . Python doesn't
display anything when we give a command to display the value of a variable
containing value as None. On the other hand, None will be displayed by
printing the value of the variable containing None using print( ) statement.
type( )
To determine the type of a variable , i.e., what type of value does it hold then
type( ) function is used.