CPE0007L
MODULE 3
EXERCISE 3
NUMBER, CASTING, BOOLEANS
STUDENT NAME: Daniel Rudiger P. Joseph
STUDENT NUMBER:202210991
DATE:09/28.2024
SIGNATURE:
I. OBJECTIVES
At the end of this exercise, students must be able to:
• Understand the numeric types in Python
• Discuss Python casting
• Understand Python Booleans
II. BACKGROUND INFORMATION Python Numbers
• int, or integer, is a whole number, positive or negative, without decimals, of
unlimited length. • float, or “floating point number” is a number, positive or negative,
containing one or more decimals.
• complex numbers are written with a “j” as the imaginary part.
Type of Conversion
Python allows you to convert the data type from one type to another with the int(), float(),
and complex() methods.
Casting in python is therefore done using constructor functions:
• int() - constructs an integer number from an integer literal, a float literal (by rounding
down to the previous whole number), or a string literal (providing the string
represents a whole number)
• float() - constructs a float number from an integer literal, a float literal or a string
literal (providing the string represents a float or an integer)
• str() - constructs a string from a wide variety of data types, including strings, integer
literals and float literals
Python Booleans
Booleans represent one of two values: TRUE or FALSE, when you compare two values, the
expression is evaluated and Python returns the Boolean answer
III. LABORATORY ACTIVITY
Directions: Determine the numeric type of the following numbers.
Int 1. a = 900
Float 2. b = 98.99
Complex 3. c = 20j
Float 4. x = 1.11
Float 5. y = -56.59
Float 6. z = 2.0
Complex 7. var1= 5+5j
Complex 8. var2=8j
Complex 9. var3= -20j
Int 10. num1= 10
Directions: Convert the following numbers
Given
x=1 #int
y = 2.8 #float
z = 1j #complex
1. Convert x from int to float.
2. Convert y from float to int.
3. Convert x from int to complex
x=1
print("The given type _number - float(x)
•type (*))
print( "Value."
Munumben
print("The given converted
atype (nei_nunber) )
The given type Sa: celass "int*>
Value: 1.0
The given converted to: class float ›
y - 2.8
print("The ziven type 1a:*
, type (y))
new-nunber ™ int(y)
print (*yaLue:"ne_nunber)
print("The given converted to: ", type(new_nunber))
The given type as: «class "float›
Value: 2
The given converted to: ‹class ant'›
print( The given type Sa:*,type(x))
new nunter complex (x)
print(*Valuest, nee_number)
print(*The siven converted to: *,type(new_nunber))
The given type la: ‹class "int*>
VAlue: (1403)
The given converted tot
‹class complext»
Directions: Determine the output or result after casting
a = int (1) # a will be 1
b = int (2.8) # b will be 2.8
c = int ("3”) # c will be 3
x = float (1) # x will be 1.0
y = float (5.8) # y will be 5.8
z = float ("5") # z will be 5.0
w = float("6.2") # w will be 6.2
x = str("s2”) # x will be s2
y = str(3) # y will be 3
z = str(3.2) # z will be 3.2
V. QUESTION AND ANSWER
1. Discuss briefly the following String Methods. Define and give example to each
method. String methods are built-in methods that can be used on string values.
a. strip()
b. lower()
c. upper()
d. replace()
e. split()
2. Define the following.
a. Check String
utilizing the in or not in operators in Python to determine whether a given substring is
present within a string.
b. String Concatenation
using the + operator or other techniques like join() to join two or more strings together.
c. String Format
using Pythons f-strings (formatted string literals) the format() method
or placeholders (like {}) to insert variables or expressions into a string.
3. List down the escape characters that are being used in Python, give the description
of each escape character.
\’ Inserts single quotation on print
\” Inserts double quotation
\\ Inserts a single backslash on print
\n It moves the text to the next line
\t Inserts a tab horizontally
\r It moves the cursor to the first position of the same line
\v It moves the cursor one down vertical
\0 It nulls character
\f Form Feed is page breaking ASCII control character
\ooo Octal Value in Python
\xhh Hex Value in Python