Intro to Computing & Programming
Lecture # 13
Python Data Types
Dr. Fahad Rehman Abro
Introduction
Data types are the classification or categorization of data items. It represents the
kind of value that tells what operations can be performed on a particular data.
everything is an object in Python programming, data types are classes
Variables are instances of these classes.
Types
The following are
the standard or
built-in data types
in Python:
Numeric
Sequence Type
Boolean
Set
Dictionary
Binary Types
Defining the Type
To define the values of various data
types and check their data types
we use the type() function.
Function Type
Numeric Data Type
Numeric Data Type
The numeric data type in Python represents the data that has a numeric
value. A numeric value can be an integer, a floating number, or even a
complex number.
Integers – This value is represented by int class. It contains positive or
negative whole numbers (without fractions or decimals).
Float – This value is represented by the float class. It is a real number
with a floating-point representation. It is specified by a decimal point.
Complex Numbers – Complex number is represented by a complex
class. It is specified as (real part) + (imaginary part)j. For example – 2+3j
Example
Sequence Data
Type
Sequence Data Type in Python
The sequence Data Type in Python is the ordered collection of similar or
different data types.
Sequences allow storing of multiple values in an organized and efficient
fashion.
There are several sequence types in Python
Python String
Python List
Python Tuple
String Data Type
String Data Type
A string is a collection of one or more characters put in a
single quote, double-quote, or triple-quote.
In python there is no character data type, a character is a
string of length one. It is represented by str class.
Strings in Python can be created using single quotes or
double quotes or even triple quotes
String Data Type
String Data Type
String Data Type
String Data
Type
String Data
Type
List Data Type
List Data Type in Python
Mutable ordered sequence of elements. This
means that you can add, remove, and change the
elements of a list at any time.
Lists can store elements of any type, and the
elements can be different types.
To create a list in Python, you use square brackets
([]) and enclose the elements of the list inside the
square brackets.
For example, the following code creates a list of
the numbers 1, 2, 3, and 4:
Examples of
List
Examples of
List
List Data Type in Python
To access the list items, refer to the index
number.
Use the index operator [ ] to access an item in a
list.
Negative sequence indexes represent positions
from the end of the array.
Negative indexing means beginning from the
end, -1 refers to the last item, -2 refers to the
second-last item, etc.
Tuple Data Type
Tuple Data Type
Just like a list, a tuple is also an ordered
collection of Python objects.
The only difference between a tuple and a
list is that tuples are immutable i.e. tuples
cannot be modified after it is created.
In Python, tuples are created by placing a
sequence of values separated by a ‘comma’
with or without the use of parentheses for
grouping the data sequence.
Tuples can contain any number of elements
and of any datatype (like strings, integers,
lists, etc.).
Dictionary Data
Type
Introduction
A dictionary in Python is an unordered
collection of data values
Unlike other Data Types that hold only a single
value as an element
Dictionary holds a key: value pair. Key-value is
provided in the dictionary to make it more
optimized.
Each key-value pair in a Dictionary is separated
by a colon : , whereas each key is separated by
a ‘comma’.
Creating a Dictionary
Create a Dictionary
In Python, a Dictionary can be created by placing a sequence of
elements within curly {} braces, separated by ‘comma’.
Values in a dictionary can be of any datatype and can be
duplicated, whereas keys can’t be repeated and must be
immutable.
The dictionary can also be created by the built-in function dict().
An empty dictionary can be created by just placing it in curly
braces{}.
Note – Dictionary keys are case sensitive, the same name but
different cases of Key will be treated distinctly.
Examples
Set Data Type
Introduction
In Python, a Set is an unordered collection of data types that
is iterable, mutable and has no duplicate elements.
This means that the elements of a set are unique, and the
order in which the elements are stored in the set is not
guaranteed.
Sets can be used to store any type of data, including
numbers, strings, and objects. Sets can also be used to store
mixed types of data.
Creating a set
curly braces ({}) and enclose the elements of the set inside the curly braces.
For example, the following code creates a set of the numbers 1, 2, 3, 4.
you can also create an empty set using curly braces.
Set items cannot be accessed by referring to an index, since sets are unordered the items has
no index.
But you can loop through the set items using a for loop or ask if a specified value is present
in a set, by using the in the keyword.
Example of creating a set
Example of Iteration
Boolean Data Type
Boolean Data Type
Data type with one of the two built-in values, True or False.
Boolean objects that are equal to True are truthy (true), and
those equal to False are falsy (false).
But non-Boolean objects can be evaluated in a Boolean
context as well and determined to be true or false. It is
denoted by the class bool.
Note – True and False with capital ‘T’ and ‘F’ are valid
Booleans otherwise python will throw an error.
Example
13- Data and Its Types presentation kafss

13- Data and Its Types presentation kafss

  • 1.
    Intro to Computing& Programming Lecture # 13 Python Data Types Dr. Fahad Rehman Abro
  • 2.
    Introduction Data types arethe classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. everything is an object in Python programming, data types are classes Variables are instances of these classes.
  • 3.
    Types The following are thestandard or built-in data types in Python: Numeric Sequence Type Boolean Set Dictionary Binary Types
  • 4.
    Defining the Type Todefine the values of various data types and check their data types we use the type() function.
  • 5.
  • 6.
  • 7.
    Numeric Data Type Thenumeric data type in Python represents the data that has a numeric value. A numeric value can be an integer, a floating number, or even a complex number. Integers – This value is represented by int class. It contains positive or negative whole numbers (without fractions or decimals). Float – This value is represented by the float class. It is a real number with a floating-point representation. It is specified by a decimal point. Complex Numbers – Complex number is represented by a complex class. It is specified as (real part) + (imaginary part)j. For example – 2+3j
  • 8.
  • 9.
  • 10.
    Sequence Data Typein Python The sequence Data Type in Python is the ordered collection of similar or different data types. Sequences allow storing of multiple values in an organized and efficient fashion. There are several sequence types in Python Python String Python List Python Tuple
  • 11.
  • 12.
    String Data Type Astring is a collection of one or more characters put in a single quote, double-quote, or triple-quote. In python there is no character data type, a character is a string of length one. It is represented by str class. Strings in Python can be created using single quotes or double quotes or even triple quotes
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
    List Data Typein Python Mutable ordered sequence of elements. This means that you can add, remove, and change the elements of a list at any time. Lists can store elements of any type, and the elements can be different types. To create a list in Python, you use square brackets ([]) and enclose the elements of the list inside the square brackets. For example, the following code creates a list of the numbers 1, 2, 3, and 4:
  • 20.
  • 21.
  • 22.
    List Data Typein Python To access the list items, refer to the index number. Use the index operator [ ] to access an item in a list. Negative sequence indexes represent positions from the end of the array. Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second-last item, etc.
  • 23.
  • 24.
    Tuple Data Type Justlike a list, a tuple is also an ordered collection of Python objects. The only difference between a tuple and a list is that tuples are immutable i.e. tuples cannot be modified after it is created. In Python, tuples are created by placing a sequence of values separated by a ‘comma’ with or without the use of parentheses for grouping the data sequence. Tuples can contain any number of elements and of any datatype (like strings, integers, lists, etc.).
  • 27.
  • 28.
    Introduction A dictionary inPython is an unordered collection of data values Unlike other Data Types that hold only a single value as an element Dictionary holds a key: value pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon : , whereas each key is separated by a ‘comma’.
  • 29.
    Creating a Dictionary Createa Dictionary In Python, a Dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be repeated and must be immutable. The dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing it in curly braces{}. Note – Dictionary keys are case sensitive, the same name but different cases of Key will be treated distinctly.
  • 30.
  • 31.
  • 32.
    Introduction In Python, aSet is an unordered collection of data types that is iterable, mutable and has no duplicate elements. This means that the elements of a set are unique, and the order in which the elements are stored in the set is not guaranteed. Sets can be used to store any type of data, including numbers, strings, and objects. Sets can also be used to store mixed types of data.
  • 33.
    Creating a set curlybraces ({}) and enclose the elements of the set inside the curly braces. For example, the following code creates a set of the numbers 1, 2, 3, 4. you can also create an empty set using curly braces. Set items cannot be accessed by referring to an index, since sets are unordered the items has no index. But you can loop through the set items using a for loop or ask if a specified value is present in a set, by using the in the keyword.
  • 34.
  • 35.
  • 36.
  • 37.
    Boolean Data Type Datatype with one of the two built-in values, True or False. Boolean objects that are equal to True are truthy (true), and those equal to False are falsy (false). But non-Boolean objects can be evaluated in a Boolean context as well and determined to be true or false. It is denoted by the class bool. Note – True and False with capital ‘T’ and ‘F’ are valid Booleans otherwise python will throw an error.
  • 38.