3 - Variables and Data Types
3 - Variables and Data Types
• What is a Variable?
• Declaration of variable
• Variable assignment
• Data types in Python
• Checking Data type
• Data types Conversion
What is a Variable?
• Generally, while doing programming in any
programming language, you have to utilise
different variables to store different data.
Variables are only hold memory areas to store
values. This implies, when you make a variable
you hold some space in memory.
• Variables are nothing but reserved memory
locations to store values. This means when you
create a variable, you reserve some space in
memory.
What is a Variable?
• You may jump at the chance to store data of
different information types like character, wide
character, integer, floating point, double floating
point, Boolean and so on. In view of the data
type of a variable, the working framework
(Operating system) assigns memory and
chooses what can be put away in the saved
memory.
Declaration of Variable
• Variables are containers for storing data values.
Unlike other programming languages,
Python has no command for declaring
a variable. A variable is created the moment
you first assign a value to it.
• Python is completely object oriented, and not
"statically typed". You do not need to declare
variables before using them, or declare their
type. Every variable in Python is an object.
Assigning Values to Variables
Python variables do not need explicit declaration to
reserve memory space. The declaration happens
automatically when you assign a value to a
variable. The equal sign (=) is used to assign values
to variables.
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.
Assigning Values to Variables
For example :
Age = 20 # An integer assignment
Percentage = 71.58 # A floating point
Name = “Rossum" # A string
print (Age)
print (Percentage)
print (Name)
String
Python List
standard
data types
Dictionary
Tuple
Data Types in Python
int
float
Python data
types-
numeric
complex
long
Data Types in Python
Numeric
Numeric
Numeric
Numeric
Using function,
type()
Using function,
a=100 type()
print(type(a))
b=10.235
print(type(b))
Output:
<class 'int'>
<class 'float'>
Data Type Conversion
Python defines type conversion functions to
directly convert one data type to another
which is useful in day to day and competitive
programming.
There are several built-in functions to
perform conversion from one data type to
another. These functions return a new object
representing the converted value.
Data Type Conversion
1. int(a,base) : This function converts any
data type to integer. ‘Base’ specifies the base
in which string is if data type is string.
2. float() : This function is used to
convert any data type to a floating point
number
3. ord() : This function is used to convert
a character to integer.
Data Type Conversion
4. hex() : This function is to convert integer
to hexadecimal string.
5. oct() : This function is to convert integer
to octal string.
6. tuple() : This function is used to convert
to a tuple.
7. set() : This function returns the type after
converting to set.
8. list() : This function is used to convert any
data type to a list type.
Data Type Conversion
9. dict() : This function is used to convert a
tuple of order (key,value) into a dictionary.
10. str() : Used to convert integer into a
string.
11. complex(real,imag) : This
function converts real numbers to
complex(real,imag) number.
12. chr(number) : This function converts
number to its corresponding ASCII character.
Thank you