Python PPT Lesson 4
Python PPT Lesson 4
• Data are essential building blocks of any computer program. In this lesson, we will
be exploring numerical values, Boolean values, text strings, lists, tuples, and sets.
Objectives
After studying this lesson, you should be able to:
• list the number data types in Python;
• work with text strings; and
• create sets and lists.
Numbers
The most common numerical data types used in programming are integers and floating-point
numbers.
Integers => are made up of whole numbers, their negative counterparts, and zero.
=> are composed of a sequence of digits. Simply entering any digit specifies an
integer value.
In previous versions of Python, integer sizes were limited by the operating system, whether it
is 32- or 64-bit. In Python 3, integers can be as large as possible. However, there some rules
you have to follow when using integers.
INTEGERS
Rules to follow when using integers:
• Negative integers are specified by starting the sequence with a minus sign.
• You can’t start with a zero (digit) otherwise, it will return a SyntaxError message.
Example:
• You also will not be able to include commas in your specification. If you try to enter
commas in a sequence of digits, you will get a tuple.
Example:
• You can insert underscores within the sequence as digit separators, and they will be
ignored by the interpreter.
Example:
INTEGERS
You can perform mathematical operations on integers.
INTEGERS
Enter the following lines in the interpreter and see their results. The floating-point
division will result in a float value (with a decimal point) if the result is not an exact
integer. Floor (or integer) division basically leaves out the decimal values. Modulo (%)
returns the remainder of a division
Note that Python arithmetic follows PEMDAS precedence rules. However, know that there
are also other operators (bitwise and logical) that can be used in programming and may
affect precedence.
Before entering the following code in the interpreter, try to figure out what the result will be.
FLOATING-POINT
Floating-point => numbers or floats are numerical values with decimal points.
=> To specify a float value, include a decimal point (.) to a
digit or sequence of digits.
=> You don’t even have to include a preceding or succeeding
zero to the decimal point.
FLOATING-POINT
Floating-point => can also take underscores to separate the digits
Example:
Like integers, you can perform mathematical operations on floats and the variables
assigned to them.
Example:
FLOATING-POINT
• When you perform an operation between a float and an integer value, the result will
be a float.
Python code using IDLE Shell:
Example:
Example:
Python code using IDLE Shell:
Python code: Output:
FLOATING-POINT
Let’s try converting a variable’s referenced value from float to integer.
Example:
Python code using IDLE Shell:
Python code: Output:
FLOATING-POINT
Note that these functions do not affect an object’s mutability. The specified values do
not change IDs.
Python code using IDLE Shell:
Example:
Python code: Output:
BOOLEAN
A Boolean value is either True or False. Try these lines in the interpreter:
Example 2:
When you want to include quotation
marks as part of your string, you can
use the single quotes within double
quotes and vice versa.
STRINGS
Multiline Strings Example 1
You can also specify multi-line
strings using three quotation
marks. To move to the next
line in the interpreter, press
Shift + Enter.
Example:
STRINGS
Offset and Slice
• You can also get a specific character within a string based on its position in the
sequence using offset. Offset is represented by a bracket symbol ( [] ).
• Keep in mind that the index starts at 0, so if you want to get the third character, you
use 2 as offset.
Python code:
Output:
STRINGS
Offset and Slice
[start :] - gets all the characters from the [start : end] - gets characters from the
start point of the slice until the end of start until before the endpoint
the sequence Example:
Example:
STRINGS
• Slice
You can also get a particular segment using slice. Like offset, you can define a slice
using brackets ( [] ) and colon ( : ).
[start: end: step] - gets characters from start until before the endpoint, skipping
every number of characters set by the step
Example:
STRINGS
• Slice
Example:
Python code:
Output:
STRINGS
• Slice
Example:
Python code using IDLE Shell:
STRINGS
▪ Length
Python also has other built-in functions that can be used to manipulate and
gather information from string values. For example, you can get the length of a
string using the len() function.
len() function counts the number of characters and returns the result as an integer value.
Examples:
Note that the len() function can also be used with other data types such as tuples, list,
sets, arrays, and dictionaries.
STRINGS
▪ STRIP
You can “clean up” a string from excess whitespace characters using the strip()
method. Methods are like functions that are associated with the object.
Let’s try this example with different whitespace characters and see how the
split() method will turn the string value into a list.
Example:
STRINGS
• Split and Join
You can also bring together list items to form a string value using the join() method.
For example, we will join the items in the grocery list using a comma (,) .
Python code:
Output:
If you do not set the optional parameter, it will replace all instances it can find.
STRINGS
• REPLACE
Please note that like the previous methods, replace() method does not alter the
original value.
Example:
STRINGS
• CASE
We can also change the case of the characters in the string using the following
methods:
Example:
• capitalize() - Capitalizes the first word of the string
• title() - Capitalizes the first letter of each word
• upper() - Capitalizes all the letters in the string
• lower() - Converts the letters to lowercase
• swapcase() - Changes the case from upper to
lowercase and vice versa
• The input() function allows us to show a prompt and have the user provide or
type some information that goes in a Python variable.
• The parameter to input() is a prompt string that prints out, prompting the user
what they are supposed to type. Adding a space at the end of the prompt string
looks best.
SYNTAX: input(prompt)
Output:
THE input() FUNCTION
• Using string, int and float for the user’s input
Python code: Output:
END OF SLIDES