0% found this document useful (0 votes)
7 views40 pages

CHAPTER II - Data Tyes and Operators

Chapter II covers data types and operators in Python, including numerical types (int, float, complex), text data types (string), and boolean values. It explains how to handle user input and errors, as well as various built-in string methods and operators for arithmetic, comparison, logical operations, and more. The chapter concludes with a practice activity to create a calculator using the discussed concepts.

Uploaded by

JoeMart Medrano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

CHAPTER II - Data Tyes and Operators

Chapter II covers data types and operators in Python, including numerical types (int, float, complex), text data types (string), and boolean values. It explains how to handle user input and errors, as well as various built-in string methods and operators for arithmetic, comparison, logical operations, and more. The chapter concludes with a practice activity to create a calculator using the discussed concepts.

Uploaded by

JoeMart Medrano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

CHAPTER II

Data types and Operators


Recap of Previous Topics:
Python Variables: Storing values in memory.
Print Function (print()): Displaying output.
Input Function (input()): Getting user input.
Formatted String using f”” and .format().
String alignment which are center align :^, left align :< , right align :>
Data type – string, float, integer, boolean.
now let’s continue……
COMMENTS
Used to explain code and make it more readable. They are ignored by
the Python interpreter so it can used to prevent code to execute.
Single line comments
Use the # symbol to write a comment on a single line.

Multi-line comments
Use the """ """ symbol to write a comment on a multi line.
DATA TYPES
NUMERICAL DATA TYPES
1. int (Integer) – int()
2. float (Floating Point) – float()
3. complex (Complex Number) – complex()

Note: you can use the type() function to check the


type of any object.
DATA TYPES
INTEGER – type()
int (integer) is a whole number that can be positive, negative, or zero.
It does not have decimals and has unlimited length, meaning it can
grow as large as the memory allows.
DATA TYPES
INTEGER – int(input())
input() always returns a string, so you need to use int() to convert it to
an integer.
DATA TYPES
INTEGER – int(input())
If the user enters a non-numeric value, it will raise a ValueError.
DATA TYPES
INTEGER – int(input())
To handle this use, try and except
The try block lets you test a block of code for errors.
The except block lets you handle the error.
DATA TYPES
FLOAT – type()
float (floating point number) is a number that contains a decimal
point. It can be positive, negative, or even scientific notation.
DATA TYPES
FLOAT – float(input())
input() always returns a string, so you need to use float() to convert it
to a float.
DATA TYPES
FLOAT – float(input())
If the user enters a non-numeric value, it will raise a ValueError.
DATA TYPES
FLOAT– float(input())
To handle this use, try and except
The try block lets you test a block of code for errors.
The except block lets you handle the error.
DATA TYPES
COMPLEX – type()
A complex number consists of a real part and an imaginary part.
In Python, you can use the .real and .imag attributes to access the real
and imaginary parts of a complex number.
DATA TYPES
COMPLEX – complex(input())
To take a complex number as input in Python, you can use the
complex() function to convert user input into a complex number.
DATA TYPES
TEXT DATA TYPE
1. string – str()
A string is a sequence of characters enclosed in single ('), double (")
quotation.
DATA TYPES
String
➢Indexing: It is accessing characters in a string using an index. The
index starts at 0.
DATA TYPES
String
➢Slicing is used to extract a portion of the string by specifying a start
index and an end index. Slicing syntax is [start:end].
DATA TYPES
String
➢Step size in slicing: In Python, when you slice a string, you can specify
a step size (also called a stride) that allows you to select every n-th
character from the string. Syntax is like this string[start:end:step]
DATA TYPES
String
➢Reverse String: You can use slicing with `[::-1]`, which means start at
the end of the string and move backwards, picking each character in
reverse order.
DATA TYPES
String - features
➢Immutable: Once a string is created, its content cannot be modified
directly.
➢Strings can be accessed using indexing and slicing.
DATA TYPES
String – Built-in-methods
➢upper() – Converts string to uppercase
➢lower() – Converts string to lowercase
➢capitalize() – Capitalizes the first letter
➢title() – Capitalizes the first letter of each word
DATA TYPES
String – Built-in-methods
➢ upper() – Converts string to uppercase
➢ lower() – Converts string to lowercase Note: You can also use the following method to
➢ capitalize() – Capitalizes the first letter manipulate user’s input.
➢ title() – Capitalizes the first letter of each word
DATA TYPES
String – Built-in-methods
➢.replace(old, new) – Replaces a substring.
➢.replace(old, new, count) – limit the number of replacement.
DATA TYPES
String – Built-in-methods
➢len() – to get the length of the string.
➢count(substring) – Counts occurrences of a substring.
➢find(substring) – Finds the index of the first occurrence
DATA TYPES
String – Built-in-methods
➢startswith(prefix) – Checks if a string starts with a given prefix.
➢endswith(suffix) – Checks if a string ends with a given suffix.
➢isalpha() – Checks if all characters are letters.
➢isdigit() – Checks if all characters are digits.
➢isalnum() – Checks if all characters are alphanumeric (letters or digits).
➢isspace() – Checks if the string consists only of spaces
DATA TYPES
String – Built-in-methods
➢center(width, char) – Centers the string with padding.
➢ljust(width, char) – Left aligns the string with padding.
➢rjust(width, char) – Right aligns the string with padding.
DATA TYPES
String – Built-in-methods
➢center(width, char) – Centers the string with padding.
➢ljust(width, char) – Left aligns the string with padding.
➢rjust(width, char) – Right aligns the string with padding.
DATA TYPES
Boolean – bool()
A boolean is a data type that can only have one of two values: True or
False. These are used to represent the truth values of logical
expressions.
OPERATORS

It is a special symbol or keyword that is used to perform operations on


values or variables. These operations can range from basic arithmetic,
logical comparisons, and data manipulation. Operators are
fundamental to programming because they allow us to work with
different types of data in meaningful ways.
OPERATORS
. perator Type
O Operators Example Use Case

Arithmetic +, -, *, /, //, %, ** Mathematical operations (addition, subtraction, etc.)

Comparison ==, !=, >, <, >=, <= Comparing values (equality, order, etc.)

Logical and, or, not Combining conditions in if statements

Assignment =, +=, -=, *=, /= Assigning values to variables

Membership in, not in Checking if an item is in a collection (list, string, etc.)

Identity is, is not Comparing object references (memory locations)


OPERATORS
Arithmetic Operators on String
(+) – use to concatenate strings.
(*) – use to multiply string characters.
OPERATORS
Arithmetic Operators on numerical data
OPERATORS
Comparison Operators on String
(==) –Returns True if both strings are equal.
(!=) –Returns True if both strings are not equal.
OPERATORS
Comparison Operators on numerical data
OPERATORS
Logical Operator
and: Returns True if both conditions are True.
or: Returns True if at least one condition is True.
not: Reverses the result of the condition (returns True if the condition
is False and vice versa).
OPERATORS
Assignment Operator
(=) – use to assign value to a variable.
(+=,*=,-=,/=,%=,//=) – used to update variables after performing arithmetic
operations.
OPERATORS
Membership Operator
in: Returns True if the value is found in the sequence.
not in: Returns True if the value is NOT found in the sequence.
OPERATORS
Identity Operator
check whether two variables refer to the same object in memory.
Practice Activity
Directions:
Create your own calculator using all operators and data types
discussed.
Thank You !!
Next topic: Flowchart and Pseudocode for Basic Programs

You might also like