8 - Python Data Types
8 - Python Data Types
Introduction to Programming
1
PYTHON – DATA TYPES
Lecture Goals
4. Built-in data types:
1. What is a Data Type in I. Numeric types,
python? II. bool data type (boolean data type),
III. None data type,
2. type(p) function
IV. Sequences in Python,
3. Different types of data types i. string data type,
ii. list data structure,
iii. tuple data structure,
iv. set data structure,
v. dictionary data structure,
vi. range data type,
5. User defined data types
2
Dr. Quadri Noorulhasan Naveed 2
1. What is a Data Type in python?
✓A data type represents the type of the data / values stored into a
variable or memory.
3
Dr. Quadri Noorulhasan Naveed 3
Data Type in python
4
Dr. Quadri Noorulhasan Naveed 4
Data Type in python example
5
Dr. Quadri Noorulhasan Naveed 5
2. type(p) function
✓type(p) is predefined function in python.
✓By using this we can check the data type of each variables.
6
Dr. Quadri Noorulhasan Naveed 6
3. Different types of data types
7
Dr. Quadri Noorulhasan Naveed 7
4. Built-in data types
✓The data types which are already existing in Python are called built-in
data types.
9
Dr. Quadri Noorulhasan Naveed 9
int data type example
Example 1: Example 2:
10
Dr. Quadri Noorulhasan Naveed 10
2. float data type
✓The float data type represents a number with decimal values.
11
Dr. Quadri Noorulhasan Naveed 11
II. bool data type (boolean data type)
✓bool data type represents boolean values in python.
✓bool data type having only two values those are,
oTrue
oFalse
Make a note
oTrue as 1
oFalse as 0
12
Dr. Quadri Noorulhasan Naveed 12
bool data type (boolean data type) example
13
Dr. Quadri Noorulhasan Naveed 13
III. None data type
✓None data type represents an object that does not contain any value.
✓If any object having no value, then we can assign that object with None data type.
Note
✓A function and method can
return None data type.
✓This point we will understand
more in function’s chapter.
14
Dr. Quadri Noorulhasan Naveed 14
IV. Sequences in Python
✓Sequence means an object.
Make a note
✓Sequence object can store a
group of values, ✓Regarding sequences like string, list,
tuple, set and dict we will discuss
i. string more in upcoming chapters
▪ Python String chapter
ii. list ▪ Python List Data Structure chapter
▪ Python Tuple Data Structure
iii.tuple chapter
▪ Python Set Data Structure chapter
iv.set ▪ Python Dictionary Data Structure
v. dict chapter
vi.range
15
Dr. Quadri Noorulhasan Naveed 15
IV. Sequences in Python
✓Sequence means an object.
✓Sequence object can store a group of values,
i. string
ii. list
iii.tuple
iv.set
v. dict
vi.range
16
Dr. Quadri Noorulhasan Naveed 16
i. string
✓A group of characters enclosed within single quotes or double quotes, or
triple quotes is called as string.
17
Dr. Quadri Noorulhasan Naveed 17
ii. list data structure
✓ We can create list data structure by using square brackets []
✓ list can store a group of values.
18
Dr. Quadri Noorulhasan Naveed 18
iii. tuple data structure
✓We can create tuple data structure by using parenthesis symbol ()
✓tuple can store a group of values.
19
Dr. Quadri Noorulhasan Naveed 19
iv. set data structure
✓We can create set data structure by using curly braces {}
✓set can store a group of values.
20
Dr. Quadri Noorulhasan Naveed 20
v. dictionary data structure
✓We can create dictionary data structure by using curly braces {}
✓Dictionary can store a group of values in the form of key value pair.
21
Dr. Quadri Noorulhasan Naveed 21
vi. range data type
✓range is a data type in python.
✓Generally, range means a group of values from starting to ending.
Creating range of values
1. range(p) function
✓As discussed, we can create range of values by using range(p)
function.
✓Here p should be integer, otherwise we will get error.
22
Dr. Quadri Noorulhasan Naveed 22
range data type example
Note:
✓If we provide range(5), then range object holds the values from 0 to 4
23
Dr. Quadri Noorulhasan Naveed 23
range(start, end) function
✓As discussed, we can create range of values by using range(start, end)
function.
✓Here start means starting value and end means till to end-1 value
Note:
✓If we provide range(1, 10), then range object holds the values from 1 to 9
24
Dr. Quadri Noorulhasan Naveed 24
Accessing range values by using for loop
✓We can access range values by using for loop.
25
Dr. Quadri Noorulhasan Naveed 25
5. User defined data types
✓ Data types which are created by programmer.
26
Dr. Quadri Noorulhasan Naveed 26
1312 / 121 CCS-3
Introduction to Programming
Questions?
27