This document discusses Python data types. It begins by explaining that data types determine the type of value stored in a variable, the operations that can be performed on the data, and provides an example. It then outlines some key properties of Python data types - that Python is dynamically typed, type can change during execution, data types are classes and variables are instances, and Python is strongly but not weakly typed. The document proceeds to explain how to get the data type using type() and categorizes main Python data types into scalars, sequences, mappings and sets.
4. Python Programming Specialization
Python Programming Data Structures
Data Type
Data type determines:
the type of value stored in a variable/object
the operations that can be performed on the data
Ex: s = Python
The above statement indicates that s is a string, and it is not valid to perform
operations like arithmetic and bit-wise operations on the variable s
5. Python Programming Specialization
Python Programming Data Structures
Data Type
Data type determines:
the type of value stored in a variable/object
the operations that can be performed on the data
Ex: a = 100
The above statement indicates that a is an integer , and it is valid to perform
operations like arithmetic, relational and bit-wise operations on the variable a
7. Python Programming Specialization
Python Programming Data Structures
Python Data Types: Property1
Python is a dynamically typed language
Advance Declaration of variables is not required in Python
Depending on the value assigned to the variables, their type is decided
Examples:
>>> a = 10 # int
>>> b = 10.3 # float
>>> c = Python # string
>>> d = [10, 20, 30, 40] # list
8. Python Programming Specialization
Python Programming Data Structures
Python Data Types: Property2
Type of the variable can be changed during the execution
Examples:
>>> x = 5 # int
>>> x = 0.3 # float
>>> x = Hello python # string
>>> x = ( 1, 2, 3, 4 ) # tuple
Note: A variable can not belong to multiple data types at a time
9. Python Programming Specialization
Python Programming Data Structures
Python Data Types: Property3
Every data item is treated as an object in Python
Data types are treated as classes
Variables are the instances (objects) of the classes
Example:
>>> A = 100
100A
object
Variable
Instance of
Integer class
10. Python Programming Specialization
Python Programming Data Structures
Python Data Types: Property4
In a weakly typed language a compiler / interpreter will sometimes change the
type of a variable
Ex: Hello + 5 is valid in weakly typed languages
Such Conversions are not valid in Python
Python is a strongly typed language
Unexpected changes to value/variable type is not valid in Python
Ex: Hello + 5 #invalid
11. Python Programming Specialization
Python Programming Data Structures
Python Data Types: Properties
Python is a dynamically typed language
Data type of a Variable can be changed during execution
Python Data types are classes
Python is a strongly typed language
13. Python Programming Specialization
Python Programming Data Structures
Python Data Types: type()
Method type() returns the type of an object
Example:
>>> A = 100
>>> print( type ( A ) )
Output: <class 'int'>
15. Python Programming Specialization
Python Programming Data Structures
Python Data Types
Python supports numerous data types to support storing variety of data
Data Types
Scalar Sequence Mapping Set
16. Python Programming Specialization
Python Programming Data Structures
Python Data Types
Data Types
Scalar Data
int
float
complex
boolean
Sequence Data
List
Tuple
String
Mapping Type
Dictionary
Set Type
Set
Frozen Set