IDENTIFYING AND CONVERTING DATA TYPES
COMPUTER | Third Monthly | Chapter 3 | 10 – St. Ezekiel Moreno
Data Types
Every value in Python has a data type.
Since everything is an object in Python,
data types are actually classes, and
variables are instances (object) of
these classes
Numbers in Python can either be plain
integers, long integers, floating point
numbers, or complex.
They are defined as int, float, and
complex class.
Integers can be of any length limited by
the memory available
Float defines a variable by fractional
value. Numbers created using a float
variable declaration will have digits on
both sides of a decimal point.
The type() function is used to know
which class a variable or a value belongs
to
The instance() function is used to check
if an object belongs to a particular class. Output:
A floating point is accurate up to fifteen
(15) decimal places.
Integer and floating points are
separated by decimal points.
A list is an ordered sequence of items.
Items separated by commas are A tuple is an ordered sequence of
enclosed within brackets. items, same as a list. It is immutable
Index starts from zero (0). and defined within parentheses where
Example of a program: items are separated by commas.
Remove – removing the value by A tuple, once created, cannot be
stating the name of the value. modified. It is used to write project
Append – adding a value at the last data.
of the list. It is defined within parentheses ()
Insert –adding a value at the desired
index.
Pop – removing the value by using
the index of the desired value.
Input:
A string is a sequence of characters. Example: >>> int (10.6)
Multiline strings can be denoted using 10
triple quotes. >>> int (-10.6)
Like list and tuple, slicing operator -10
[] can be used with string. Conversion to and from string must
contain compatible values
A set is an unordered collection of Example: >>> float('2.5')
unique items. It is defined by values 2.5
separated by comma inside braces. >>> str (25)
Conversion of Data types '25'
>>> int('1p')
We can convert between different data Traceback (most recent call last):
types by using different type conversion File "<pyshell#5>", line 1, in <module>
functions like: int(), float(), str(), etc. int('lp')
Converting integer to float: ValueError: invalid literal for int() with
Example: >>> float (5) base 10: '1p' >>>
5.0
Conversion of a float() to int() will
truncate the value.
USING OPERATORS
COMPUTER | Third Monthly | Chapter 3 | 10 – St. Ezekiel Moreno
Operators are special symbols in Python
Comparison Operators
that perform arithmetic or logical
computation. Operands are the values
used by operators. Comparison operators compare values
from left to right. They either return
Arithmetic Operators
True or False according to the condition
Arithmetic operators perform Operator Description Example
mathematical operations. > True if the x>y
Operator Description Example Greater than left operand
+ It adds x+y is greater
Plus operands. than the
- It subtracts x-y right
Hyphen operands. < True if the x<y
* It multiplies x*y Less than left operand
Asterisk operands. is less than
/ It divides x/y the right
Forward operands, it == True if both x == y
slash always Equal the operands
results into are equal.
float
% It is the x%y != True if the x != y
Percent modulus; it is Exclamation operands are
the and equal not equal.
remainder of >= True if the x >= y
the division. Greater left operand
than and is greater
// It is the floor x//y equal than or equal
Double slash division, it to the right
returns the <= True if the x <= y
whole Less than left operand
number part and equal is less than or
of the equal to the
quotient right.
** It is the x**y
Double exponent: Logical Operators
asterisk the left
operand is Logical operators are and, or, and not
raised to the Operator Description Example
power of and True if both x and y
right the operands
operand. are true.
or True if at least x or y
one (1) of the
operands is not identical
true. (does not
not True if the not x refer to the
operand is same object)
false
Conditional Control Stucture
(complements
the operand). The conditional control structure is
Assignment Operators used in programs to perform
statements based on the conditions.
Assignment operators assign values to This structure adopted how we humans
variables. x=5 is a simple assignment think. We set our goals, then we create
operator that assigns the value 5 on the and choose the best option to attain
right to the left is a simple the variable x them.
on. Example: Create a Python program that accept
Operator Example Equivalent AGE as input and will determine if you are
To eligible to vote (18 and above) or not (below
= x=5 x=5 18). Use IF-Else Statements.
----------------------------------------------------------------
+= x+=5 x=x+5 ‘’’
-= x-=5 x=x-5 (comments)
*= x*=5 x=x*5 ‘’’
/= x/=5 x = x/5
legal=18
%= x%=5 x=x%5 age = int (input("Please enter age: "))
//= x//=5 x=x//5 if age>=legal:
&= x&=5 x=x&5 print ("You are eligible to vote!")
else:
|= x|=5 x = x|5
print ("You are NOT eligible to vote!")
^= x^=5 x = x^5
----------------------------------------------------------------
>>= x>>=5 x=x>>5
<<= X<<=5 x = x<<5
Identity Operators
Identity operators are is and is not.
They check if two (2) values or variables
are stored in the same part of the
memory. Two (2) variables that are
equal does not imply that they are
identical.
Operator Description Example
is True if the x is True
operands are
identical
(refers to the
same object).
is not True if the x is not True
operands are