Python PPT LESSON 3 Understanding Data
Python PPT LESSON 3 Understanding Data
• We’ve already set up our computers to run Python and PyCharm. In this lesson, we
will be discussing fundamental concepts in programming, particularly how data
works in Python.
Objectives
After studying this lesson, you should be able to:
• learn how data is represented in Python,
• list the common data types,
• specify data, and
• perform basic operations.
Launching IDLE opens up the Python Shell.
Using the
Interpreter
To write effective code, you need to know how the programming language handles and
manages data.
Python follows this “data as objects” analogy Try this out in the
and treats data accordingly. Each object bears interpreter. Enter each
the following information: of the following lines:
• an identity (or ID), which tells it apart from
other objects (usually the object’s location in
the computer’s memory)
• a type, which determines what kind of value
it can have
• a corresponding value
id() function When using functions, we place the object we want to be processed
within the parentheses.
Data Types
The type() function
You can use the type() function to check an object's data type.
Python should return the data types of each i.e. x is a string and y is an integer.
Example 1: Example 2:
What is MUTABILITY?
Python is quite strict about what you can do with data types. You may have observed that
one of the columns asks the question, “Is Mutable?
Let’s check the IDs of the following: We may have been able to
id(x) change to which data or object
a variable may be assigned (x is
id(2)
now associated with 2), but the
id(y) object (1) remains unchanged
id(1) after it is created within the
program.
Now let’s try doing something similar to a mutable data type such as a list.
• Check the value of both a and b variables, then check their IDs.
print(a)
print(b) See how they both still
id(a) have the same value and
id(b) refer to the same object
in the memory.
Why does mutability matter?
help(“keywords”)
• To assign a variable to an object, we use the equal sign (=).
• When creating variables consisting of
multiple words, many Python
programmers opt to use the snake_case,
where the underscore separates the
lowercase words. What this does
is it creates an
In the interpreter, enter the following lines: object. In this
x = “Jose” case, it is the
print(x) string “Jose”.
id(x) We associate
id(“Jose”) the variable
type(x) name x to the
type(“Jose”) object.
• We can also reassign variable names.
x = 5
print(x)
id(x)
id(5) Notice how the variable
type(x) will now refer to a
type(5) different object ID
compared to when we
ran it before.
END OF SLIDES