You are on page 1of 54

ITEP 206

INTEGRATIVE PROGRAMMING
TECHNOLOGIES 1
LESSON 3
Different Types of Objects
in Python
In Python, the term “object” is
quite the catch-all; including
numbers, strings of characters,
lists, functions - a Python object
is essentially anything that you
can assign to a variable.
Different Types of Object in Python

• File Method
• Set Method
• Tuple Method
• Dictionary Method
• List Method
Number Types

Python has three basic types of numbers:


integers, “floating-point” numbers, and
complex numbers. Familiar mathematical
symbols can be used to perform arithmetic
on all of these numbers (comparison
operators like “greater than” are not defined
for complex numbers):
x+y

Sum of Two Numbers

x-y

Difference of Two Numbers


x*y

Product of Two Numbers


x/y

Quotient of Two Numbers

x // y

Quotient of Two Numbers, returned as an integer

x*y

x modulo y
The remainder of x/y for positive x,y
x ** y

x raised to the power y

-x

A negated number

Abs(x)

The Absolute Value of a Number


X == y

Check if two numbers have the same value

x != y

Check if two numbers have different values

x>y

Check if x is greater than y


x >= y

Check if x is greater than or equal to y

x<y

Check if x is less than y

x <= y

Check if x is less than or equal to y


These operations obey the
familiar order of operations
from your mathematics
class
(PEMDAS)
Pythons Math Module

The standard library’s math module provides


us with many more mathematical functions,
like logarithms and trigonometric functions.
Integers

As in traditional mathematics, an integer is


any “whole” number:
Integers

You can create as large an integer as you’d like;


Python will allocate as much memory as needed
(and ultimately, as is available) to store an
integer’s exact value:

Integers have some built-in functions available to them, which


are detailed in the official documentation. The utility of these
will likely be quite obscure to new programmers.
Floating Point Numbers

A “floating-point” number is a number with a


decimal point. Referred to as a “float” for
short, this can be used to represent any
number, up to a limited number of digits.

These objects belong to the built-in type


float, which can be used to convert objects
to floats:
Example
Scientific Notation

A float can also be created using familiar


scientific notation. The character e is used to
represent x10, and the proceeding number is the
exponent. Here are some examples of traditional
scientific notation, and their corresponding
representation in Python:
Scientific Notation

Python will automatically display a float that


possesses many digits in scientific notation:
Understanding Numerical Precision

Whereas a Python integer can be made to be as large as you’d like, a


floating-point number is limited in the number of digits it can store. That is,
your computer will only use a set amount of memory - 8 bytes (64 bits) on
most machines - to store the value of a floating-point number.

In effect, this means that a float can only be represented with a numerical
precision of approximately 16 decimal places, when that number is written
in scientific notation. The computer will not be able to reliably represent a
number’s digits beyond those accounted for by the allotted 8 bytes. For
instance, the following Python integer is defined with 100 digits, but when
this number is converted to a float, it only retains 15 decimal places in
scientific notation:
Understanding Numerical Precision
Understanding Numerical Precision

The computer cannot keep track of those last 84


decimal places because doing so would require more
than 8 bytes of memory to store the entire value of that
float. If you had been diligently counting stars in the sky
(perhaps across many universes, this number far
exceeds the estimated number of stars in our universe),
you would have just lost track of over
of them simply by converting your integer count to a
float!
Method Description
close() Closes the file
detach() Returns the separated raw stream from the
buffer
fileno() Returns a number that represents the
stream, from the operating system's

File Method in Python


perspective

flush() Flushes the internal buffer


isatty() Returns whether the file stream is
interactive or not

Python has a
read() Returns the file content
readable() Returns whether the file stream can be
read or not

set of methods
readline() Returns one line from the file
readlines() Returns a list of lines from the file

seek() Change the file position

available for seekable() Returns whether the file allows us to


change the file position

the file object. tell()

truncate()
Returns the current file position

Resizes the file to a specified size

writable() Returns whether the file can be written to


or not
write() Writes the specified string to the file

writelines() Writes a list of strings to the file


File Method in Python

In Python, we need to open a file first to perform any operations


on it—we use the open() function to do so. Let's look at an
example:

Suppose we have a file named file1.txt.


File Method in Python

To open this file, we can use the open() function.

Here, we have created a file object named file1. Now, we can


use this object to work with files.
Reading Files in Python

After we open a file, we use the read() method to read its


content. For example,

Suppose we have a file named file1.txt.


Reading Files in Python

Now, let's read the content of the file.


Output

In the above example, the code file1.read() reads the content of


the file and stores it in the read_content variable.
Writing to Files in Python
To write to a Python file, we need to open it in write mode using
the w parameter.

Suppose we have a file named file2.txt. Let's write to this file.

When we run the above code, we


will see the specified content
inside the file.
Opening a Python File Using with...open

In Python, there is a better way to open a file using with...open.


For example,
Output

Here, with...open automatically closes the file, so we don't have


to use the close() function.
Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference
between two or more sets

Set Method in Python difference_update() Removes the items in this set that are also
included in another, specified set
discard() Remove the specified item
intersection() Returns a set, that is the intersection of two
or more sets

Python has a intersection_update()

isdisjoint()
Removes the items in this set that are not
present in other, specified set(s)
Returns whether two sets have a intersection

set of methods issubset()


or not
Returns whether another set contains this
set or not

available for issuperset()

pop()
Returns whether this set contains another
set or not
Removes an element from the set

the set object. remove()


symmetric_difference()
Removes the specified element
Returns a set with the symmetric differences
of two sets
symmetric_difference_update() inserts the symmetric differences from this
set and another
union() Return a set containing the union of sets

update() Update the set with another set, or any other


iterable
Set Method in Python

A Set in Python is a collection of unique


elements which are unordered and
mutable. Python provides various
functions to work with Set. In this article,
we will see a list of all the functions
provided by Python to deal with Sets.
Adding and Removing Elements

We can add and remove elements form the set with the
help of the below functions –

• add(): Adds a given element to a set


• clear(): Removes all elements from the set
• discard(): Removes the element from the set
• pop(): Returns and removes a random element from the
set
• remove(): Removes the element from the set
Adding and Removing Elements (Example)
Tuple Method in Python

Method Description
count() Returns the number of times a specified value
occurs in a tuple
index() Searches the tuple for a specified value and returns
the position of where it was found
Tuple Method in Python

Python Tuples is an immutable collection of that are


more like lists. Python Provides a couple of methods to
work with tuples. In this article, we will discuss these two
methods in detail with the help of some examples.

Syntax:
tuple.count(element)
Tuple Method in Python Example
Tuple Method in Python Example

Index() Method
The Index() method returns the first occurrence of the given
element from the tuple.
Syntax:
tuple.index(element, start, end)

Parameters:
•element: The element to be searched.
•start (Optional): The starting index from where the searching is started
•end (Optional): The ending index till where the searching is done
Tuple Method in Python Example

Index() Method Example


Method Description

clear() Removes all the elements from the


dictionary

copy() Returns a copy of the dictionary

Dictionary Method in Python


fromkeys() Returns a dictionary with the specified keys
and value

get() Returns the value of the specified key

items() Returns a list containing a tuple for each key

Python has a
value pair

keys() Returns a list containing the dictionary's

set of built-in
keys

pop() Removes the element with the specified key

methods that popitem() Removes the last inserted key-value pair

you can use on


setdefault() Returns the value of the specified key. If the
key does not exist: insert the key, with the
specified value

dictionaries. update() Updates the dictionary with the specified


key-value pairs

values() Returns a list of all the values in the


dictionary
Dictionary Method in Python

Python Dictionary is like a map that is


used to store data in the form of a
key: value pair. Python provides
various built-in functions to deal with
dictionaries.
Dictionary Clear() Method

The clear() method in Python is a built-in method


that is used to remove all the elements (key-value
pairs) from a dictionary. It essentially empties the
dictionary, leaving it with no key-value pairs.
Dictionary Get() Method

In Python, the get() method is a pre-built


dictionary function that enables you to obtain the
value linked to a particular key in a dictionary. It is
a secure method to access dictionary values
without causing a KeyError if the key isn’t present.
Dictionary Items() Method

In Python, the items() method is a built-in dictionary


function that retrieves a view object containing a list of
tuples. Each tuple represents a key-value pair from the
dictionary. This method is a convenient way to access
both the keys and values of a dictionary simultaneously,
and it is highly efficient.
Dictionary Keys() Method

The keys() method in Python returns a view object with


dictionary keys, allowing efficient access and iteration.
Dictionary Values() Method

The values() method in Python returns a view object


containing all dictionary values, which can be accessed
and iterated through efficiently.
Dictionary Update() Method

Python’s update() method is a built-in dictionary function


that updates the key-value pairs of a dictionary using
elements from another dictionary or an iterable of key-
value pairs. With this method, you can include new data
or merge it with existing dictionary entries.
Dictionary Pop() Method

In Python, the pop() method is a pre-existing dictionary


method that removes and retrieves the value linked with
a given key from a dictionary. If the key is not present in
the dictionary, you can set an optional default value to
be returned.
Dictionary PopItem() Method
In Python, the popitem() method is a dictionary function that eliminates and
returns a random (key, value) pair from the dictionary.

As opposed to the pop() method which gets rid of a particular key-value pair
based on a given key, popitem() takes out and gives back a pair without
requiring a key to be specified.
Dictionary PopItem() Method
In Python, the popitem() method is a dictionary function that eliminates and
returns a random (key, value) pair from the dictionary.

As opposed to the pop() method which gets rid of a particular key-value pair
based on a given key, popitem() takes out and gives back a pair without
requiring a key to be specified.
Method Description

append() Adds an element at the end of the list

clear() Removes all the elements from the list

List Method in Python


copy() Returns a copy of the list

count() Returns the number of elements with


the specified value

Python has a
extend() Add the elements of a list (or any
iterable), to the end of the current list

set of built-in
index() Returns the index of the first element
with the specified value

methods that
insert() Adds an element at the specified
position

you can use on


pop() Removes the element at the specified
position

list.
remove() Removes the first item with the
specified value

reverse() Reverses the order of the list

sort() Sorts the list


List Append() Method

Adds element to the end of a list.


List Insert() Method

Inserts an element at the specified position.

Note: The position mentioned should be within the range of List, as in this
case between 0 and 4, else wise would throw IndexError.
List Extend() Method

Adds items of an iterable(list, array, string , etc.) to


the end of a list
List Extend() Method

Adds items of an iterable(list, array, string , etc.) to


the end of a list
LABORATORY TIME

You might also like