Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Smart Engineer Babu

Python Programming
Unit-4(Sieve of Eratosthenes and File I/O)
Full Explana on

Topic-1: Sieve of Eratosthenes.

Explana on:
1. Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any
given limit.
2. If you don’t know what prime numbers are then first, we are going to take a look on
prime numbers.
3. Prime Numbers- A number that can be divided exactly only by itself and 1 called prime
number.
4. From 1 to 100 prime numbers are - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
53, 59, 61, 67, 71, 73, 79, 83, 89, 97.
5. Working-
a. Create a list of consecu ve integers from 2 to n: (2, 3, 4, …, n).
b. Ini ally, let p equal 2, the first prime number.
c. Star ng from p2, count up in increments of p and mark each of these numbers
greater than or equal to p2 itself in the list. These numbers will be p(p+1), p(p+2),
p(p+3), etc.
d. Find the first number greater than p in the list that is not marked. If there was no
such number, stop. Otherwise, let p now equal this number (which is the next prime),
and repeat from step c.
Example: Suppose we have to find the prime numbers between 1 to 50 then,

Topic-2: File I/O or File Input Output in python.

Explana on: Before moving towards File I/O firstly we are going to
talk about file.

File-
1. A file in a computer is a loca on for storing some related data.
2. It has a specific name.
3. The files are used to store data permanently on to a non-vola le memory (such as hard
disks).
4. Vola le memory – Ram(Primary memory)
Non-vola le memory – Hard Disk(Secondary memory)

Opening a file func on in python-


1. Python has a built-in open () func on to open files from the directory.
2. Two arguments that are mainly needed by the open () func on are :
a. File name: नाम
b. Access mode: Read-Write-Append
3. Syntax:

Closing a file func on in python-


1. जब हमारा काम िनकल जाता है तो हम फाइल को बदं कर देते है- that means once our work is done with the
file we close the file.
2. Python comes with a garbage collector responsible for cleaning up the unreferenced
objects from the memory, we must not rely on it to close a file.
3. Proper closing of a file frees up the resources held with the file.
4. The closing of file is done with a built-in func on close ().
5. It is like once you are done scrolling reels you close instagram and if you do not close it
properly it will rely on your memory and do not free it.
6. Syntax:

Wri ng to a file opera on-


1. A er opening a file, we have to perform some opera ons on the file. Here we will
perform the write opera on.
2. In order to write into a file, we have to open it with w(write) mode or a(append) mode,
on any wri ng-enabling mode.
Reading from a file opera on-
1. In order to read from a file, we must open the file in
the reading mode (r mode).
2. We can use read (size) method to read the data
specified by size.
3. If no size is provided, it will end up reading to the
end of the file.
4. The read() method enables us to read the strings
from an opened file.
5. Syntax: file object. read ([size])
6. For example:
#open the file

Topic-3: Asser ons.

Explana on:
1. Python Asser ons in any programming language are the debugging tools that help in
the smooth flow of code.
2. They are used just before the statement we want to check/test.
3. If the statement is wrong then asser ons throws an error and stops the program
execu on.
4. If the statement is true then the program works without any break.

Topic-4: Excep ons and Excep on handling.

Explana on:
1. While wri ng a program, we o en end up making some errors. There are many types of
error that can occur in a program.
a. Compile me errors that occurs during compila on me and they mostly are syntax
errors.
b. Run me errors that occurs at run me and these errors are known as Excep ons.
2. There are various run me errors in python- FileNotFoundError, ZeroDivisionError and
ImportError.
3. Python creates an excep on object for every occurrence of these run me errors.
4. The user must write a piece of code that can handle the error.
5. If it is not capable of handling the error, the program prints a statement back to that
error along with the details of why the error has occurred.
Excep on Handling:
1. Whenever any error occurs during the execu on of program then the program stops
execu ng.
2. It is not a good coding standard that one error interrupts the execu on of whole
program.
3. That’s why there are some blocks which are used to handle that excep on.
a. Try...except –
 Python provides a try statement for handling excep ons.
 An opera on in the program that can cause the excep on is placed in the try
clause while the block of code that handles the excep on is placed in the
except clause.
 The block of code for handling the excep on is wri en by the user and it is for
him to decide which opera on he wants to perform a er the excep on has
been iden fied.

b. try finally –
 The try statement in Python has op onal finally clause that can be associated
with it.
 The statements wri en in finally clause will always be executed by the
interpreter, whether the try statement raises an excep on or not.
 With the try clause, we can use either except or finally, but not both.
 We cannot use the else clause along with a finally clause.

4. There are some built-in excep ons wri en below-


a. Excep on LookupError :

b. TypeError :

c. Excep on Arithme cError :

d. Excep on Asser onError :

e. Excep on A ributeError :
Topic-5: Modules.

Explana on:
1. A module is a file containing Python defini ons and
statements. A module can define func ons, classes
and variables. Python Definitions,
2. It allows us to logically organize our Python code. Statements,
3. The file name is the module name with the suffix Functions, Classes,
.py appended. Variables
4. A module can also include runnable code. Grouping
related code into a module makes the code easier
to understand and use.
5. Defini ons from a module can be imported into other modules or into the main module.

import statement-
1. The import statement is the most common way of running the import process of any
module.
2. An import statement is made up of the import keyword along with the name of the
module.
3. The import statement combines two opera ons;
 It searches for the named module
 It binds the results of that search to a name in the local scope.

Topic-6: Abstract Data Types.

Explana on:
1. Can also be reffered as ADTs.
2. ADTs are also similar to other Data types that means they store some kind of value
inside them.
3. The only difference is that in ADTs the opera ons of ADTs are defined but the internal
working is not.
4. Its like you know how to drive car and do not care about how the internal process is
going on.
5. There are three ADTs-
a. List ADT –

b. Stack ADT –

c. Queue ADT –

Topic-7: Class, Objects.

Explana on:
1. Class –
a. A class can be defined as a blue print or a previously
defined structure from which objects are made.
b. Classes are defined by the user.
c. In Python, a class is defined by a keyword Class.
Syntax: class class_name;
For example : Fruit is a class, and apple, mango and
banana are its objects. A ribute of these objects are color, taste, etc.
2. Object –
a. An object is an instance of a class that has some a ributes and behaviour.
b. The object behaves according to the class of which it is an object.
c. Objects can be used to access the a ributes of the class.
d. The syntax of crea ng an object in Python is similar to that for calling a func on.

Note: It is like when you fill the form with your details then it’ll become your form that means it
becomes an instance.

Topic-8: __init__ , __str__ , comparison and arithme c method.

Explana on:
__init__ method –
1. The __init__ func on is a reserved func on in classes in Python which is automa cally
called whenever a new object of the class is instan ated.
2. As a regular func on, the init func on is also defined using the def keyword. These
types of func ons are called constructors.
3. We use constructors to ini alize variables.
4. We can pass any number of arguments while crea ng the class object as per the
defini on of the __init__ func on.

__str__ method –
1. __str__method is the “informal” or nicely printable string representa on of an object.
This is for the end user.
2. It is called by str(object) and the built-in func ons such as format() and print().
3. The return value of this method is a string object.
For example :

Comparison methods –
1. The comparison methods are used whenever , <=, >=, !=, == are used with the object.
2. The comparison methods are also called ‘rich comparison methods’ or ‘comparison
magic methods’.
3. Following are the comparison methods : _it_, _le_, _eq_, _ne_, _gt_, _ge_.

4. The most common usage is to return False or True when using one of the comparison
methods, but we can actually return any value.

Arithme c methods –

Topic-9: Object-oriented programming, inheritance, method overriding,


polymorpihsm, encapsula on, data hiding.

Explana on:
Object-oriented programming –
1. The object-oriented programming approach mainly focuses on the object and classes
while procedural programming focuses on the func on and methods.
2. The object is an instance of class.
3. Class is set of a ributes, which can be data members and methods members.

Inheritance –
1. Inheritance is a procedure where one class can access the proper es of another class.
2. It is similar like a child have some quali es of his/her parents.

Class A Base Class

Class B Derived Class

Types of inheritance –

Hybrid Inheritance

Method Overriding –
1. Method overriding means that the method of parent class can be used in the subclass
with different or special func onality.
Polymorphism –
1. The word ‘Poly’ means ‘many’.
2. Polymorphism means an object have many forms.
Phone

Feature Phone Touch Screen Phone Smart Phone

Data Encapsula on –
1. In Python programming language, encapsula on is a process to restrict the access of
data members. This means that the internal details of an object may not be visible from
outside of the object defini on.
2. The members in a class can be assigned in three ways i.e., public, protected and private.
3. If the name of a member is preceded by single underscore(_), it is assigned as a
protected member.
4. If the name of a member is
preceded by double
underscore(__), it is assigned as
a private member.
5. If the name is not preceded by
anything then it is a public member.
Data Hiding –
1. In Python programming, there might be some cases when we intend to hide the
a ributes of objects outside the class defini on.
2. To accomplish this, use double score (__) before the name of the a ributes and these
a ributes will not be visible directly outside the class defini on.

You might also like