Chapter 2 - Data Types

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Primitive

and Compound
Data Types

Chapter 2

Deanship of Preparatory Program

A. Alshaibani
Ø Data Types:
• Numeric: Integer, Float, Complex No.
• Dictionary.
• Boolean.
• Set
• Sequence Type: Strings, List, Tuple.

Ø Relational Operators.
Ø Logical Operators.

A. Alshaibani
Primitive and Compound Data Types

A. Alshaibani
Python Data Types
v What is Data Types?
It is the type of data that the variable can hold.

v Examples:
• String can hold a “text” • List
• Numeric can hold a “number” • Set Those data types look
• Boolean hold one of the possible values: “True or False” • Dictionary like a list but they are
• Tuple slightly different.

Ø These data types are called “ Primitive” Ø These data types are called “ Compound”

A. Alshaibani
• Numeric data type represent the data which has numeric value.
• Numeric value can be integer, floating number or complex numbers.

A. Alshaibani
v Integer Data types:
Integers:
• This value is represented by int class.
• It contains positive or negative whole numbers (without fraction or decimal).
• In Python there is no limit to how long an integer value can be.

Float:
• This value is represented by float class.
• It is a real number.
• It is specified by a decimal point.

Complex Numbers:
• Complex number is represented by complex class.
• It is specified as (real part) + (imaginary part)j. For example – 2+3j

A. Alshaibani
2. :
Ø Dictionary in Python is an unordered collection of data values.
Ø Used to store data values like a map.
Ø Unlike other Data Types that hold only single value as an element.
Ø Dictionary holds key: value pair.

Creating Dictionary:

Ø place a sequence of elements within curly {} braces, separated by ‘comma’.


Ø Values in a dictionary can be of any datatype and can be duplicated.
Ø keys can’t be repeated and must be immutable.
Ø Dictionary can be created by the built-in function dict().
Ø An empty dictionary can be created by just placing it to curly braces{}.

A. Alshaibani
A. Alshaibani
o Accessing elements of Dictionary:
Ø In order to access the items of a dictionary refer to its key name.
Ø Key can be used inside square brackets.
Ø Use a method called get() to access the element from a dictionary.

A. Alshaibani
Ø Data type with one of the two built-in values, True or False.
Ø Normally, used for comparison of two things.

Note – True and False with capital ‘T’ and ‘F’ are valid Booleans otherwise python will throw an error.

A. Alshaibani
Ø Set is an unordered collection of data type that is iterable, mutable and has
no duplicate elements.

Ø Creating Set can be used by using the built-in function called “ Set”

A. Alshaibani
o Accessing elements of Sets:

Ø Set items cannot be accessed by referring to an index, since sets


are unordered the items has no index.

Ø 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 keyword.

A. Alshaibani
Sequence
Type

String List Tuple

A. Alshaibani
• 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 string class.

A. Alshaibani
• To demonstrate a single quote, double-quote or triple quote: • The Output:

A. Alshaibani
of
q In Python, individual characters of a String can be accessed by
using the method of Indexing.
q If negative number used, it will access the characters
backward.

A. Alshaibani
A. Alshaibani
b. :
q List are just like arrays in other languages which is an ordered collection of data.
q List is very flexible as the items in a list do not need to be of the same type.
q List is mutable.

v Creating List and Access the elements in the list:


Ø Lists in Python can be created by just placing the sequence inside the square brackets[].

Note: “we will learn how to access elements in list by using Loop statements later in this course”
A. Alshaibani
q Just like list, tuple is also an ordered collection of Python objects.
q The only difference between tuple and list is that tuples are immutable.

Creating Tuple
In Python, tuples are created by placing a sequence of values separated by ‘comma’
with or without the use of parentheses for grouping of the data sequence.

A. Alshaibani
• List is mutable • Tuples is immutable.
• Place the values inside square brackets[]. • Place the values inside the parentheses

A. Alshaibani
Results of the previous Code: List VS Tuple

Ø List accepted the changes to Ø Tuple did not accept changes


happen to occur

A. Alshaibani
A. Alshaibani
Ø Relational and Logical operators are required In every programming languages in order to manage
the flow of the program.

Relational Operators

Logical Operators

A. Alshaibani
Since 12 is bigger than 10 then it will return True.
When use less than operators it will return False

AND operator, if and only if all of its operands are


True will return True.

OR operator, if one or more is true then will


return True.

NOT is the operator that reverse the logic.

A. Alshaibani
Resources:

N. (Ed.). (2020, July 29). Python Data Types. Retrieved January 24, 2021, from https://www.geeksforgeeks.org/python-
data-types/#numeric

A. Alshaibani

You might also like