0% found this document useful (0 votes)
52 views

Python PPT LESSON 3 Understanding Data

Uploaded by

printerkey12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Python PPT LESSON 3 Understanding Data

Uploaded by

printerkey12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

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

In PyCharm, the Python


Shell can be accessed
readily from within
PyCharm through the
Python Console tool
window.

To restart the console, press Ctrl + F6 in IDLE or press Ctrl + F5 in PyCharm.


Data as Objects

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.

Run the following lines: The interpreter should return an


integer value for each, signifying
their location in your memory.
Here is a list of the common data types and their respective uses.

Data Types
The type() function
You can use the type() function to check an object's data type.

In the interpreter, enter:

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?

Try the following code in the Sample Output:


Mutability interpreter to see what it means to
refers to whether the value of be immutable:
the object can be changed x = 1
(mutable) or not (immutable). y = x
Integer, float, string, Boolean,
tuple, and frozenset data are Then check the id of both x,y and 1.
immutable.
id(x)
id(y)
id(1)
Notice how x, y, and 1 refer to the same object?
Let’s try changing the data of one of the variables. Enter the following code.
Upon entering this line, we have changed the data to which
the variable x is assigned.
Check what data x contains using the print() function.

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.

• Create a simple list and assign it to a variable.

• Check the IDs of both a and b.

Notice how they refer to the same object, which is


the list
Let’s try changing the value of the list by “popping” one element out of it with
the pop() method.

This effectively removed the last item from the list


and returned its value.

• 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?

It is good to know how these properties can affect the


program. For instance, immutable objects are
considered “thread-safe” in cases when the computer is
capable of multithreading or running multiple processes
at once. This ensures that the code will work even if the
objects are passed on across threads. Immutability also
promotes clarity and exactness in the code, as every
immutable object is guaranteed to remain the same as
the program runs.
Specifying Data Values
We can specify data values in Python through either literal values or variables.

• A literal is a direct way to specify a value.


Specifying Data Values
We can specify data values in Python through either literal values or variables.

• Variables are among the fundamental programming concepts that you


must grasp. In Python, variables are labels that we can assign to a
particular object or data stored in the computer’s memory.

• We do not have to declare a variable’s data type explicitly when


creating one in Python. The data type is already borne by the object
to which the variable is assigned.

• Python has several rules on how you can name a variable:


Specifying Data Values
• Variables
We can create variables by choosing a name and assigning it to an object.
Python has several rules on how you can name a variable:
✓ Must begin with a letter or underscore and not a digit
✓ May only contain the following characters
• Lowercase letters (a-z)
• Uppercase letters (A-Z)
• Digits (0-9)
• Underscore (_)
✓ Is case-sensitive, meaning var, VAR, and Var would be considered
unique names
✓ Can’t be one of Python’s keywords
To get a list of Python’s keywords, run help(‘keywords’) in the interpreter.

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.

Try this in the interpreter:

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

You might also like