0% found this document useful (0 votes)
3 views117 pages

Python PDF

The document is a comprehensive syllabus for a Python programming course aimed at beginners, covering topics such as installation, data types, operators, control structures, and object-oriented programming. It emphasizes Python's readability and efficiency, detailing various features, applications, and methods associated with the language. Additionally, it includes practical exercises and examples to reinforce learning.
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)
3 views117 pages

Python PDF

The document is a comprehensive syllabus for a Python programming course aimed at beginners, covering topics such as installation, data types, operators, control structures, and object-oriented programming. It emphasizes Python's readability and efficiency, detailing various features, applications, and methods associated with the language. Additionally, it includes practical exercises and examples to reinforce learning.
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/ 117

PROGRAMMING WITH PYTHON

A Beginner-Friendly Programming Language


SYLLABUS
Introduction to Python
Features of Python
Applications of Python
Installation and Environment Setup of python
PyCharm Installation
Operators
Variables
Data Types
List Vs Tuples Vs Set Vs Dictionary
SYLLABUS
String Methods
List Methods
Tuple Methods
Set Methods
Dictionary Methods
User Input
Comments
Conditional Statements
Loops
SYLLABUS
Break, Continue, Pass
Nested Loops
Functions
OOP(Object Oriented Programming) Concepts
PIP
Mini Project
INTRODUCTION TO PYTHON
Python is a widely used general-purpose, high level programming language. It was
created by Guido van Rossum in 1991 and further developed by the Python
Software Foundation. It was designed with an emphasis on code readability, and its
syntax allows programmers to express their concepts in fewer lines of code.
Python is a programming language that lets you work quickly and integrate systems
more efficiently.
There are two major Python versions: Python 2 and Python 3. Both are quite
different.
FEATURES OF PYTHON
APPLICATIONS OF PYTHON
STRUCTURE OF PYTHON INTERPRETER
INSTALLATION AND ENVIRONMENT SETUP OF
PYTHON
PYCHARM INSTALLATION
OPERATORS
Operators are special symbols in python that carry out arithmetic or logical computation.
Python classifies the operators into following:
1. Arithmetic Operators.
2. Assignment Operators.
3. Comparison Operators.
4. Logical Operators.
5. Identity Operators.
6. Membership Operators.
7. Bitwise Operators.
ARITHMETIC OPERATORS
Arithmetic operators includes the following:
ASSIGNMENT OPERATORS
Assignment operators includes the following:
COMPARISON OPERATORS
Comparison operators includes the following:
LOGICAL OPERATORS
Logical operators includes the following:
IDENTITY OPERATORS
Identity operators includes the following:
MEMBERSHIP OPERATORS
Membership operators includes the following:
BITWISE OPERATORS
Bitwise operators include the following.
Operator Description Example
& Performs Bitwise AND 5&4
Operation on given values
| Performs Bitwise OR 6|5
Operation on given values
~ Performs Bitwise NOT ~9
Operation on given values
^ Performs Bitwise XOR 7^6
Operation on given values
For more details refer to the following page. Python Bitwise Operators
OPERATORS(EXERCISE)
1. 5+4
2. 2*9
3. 3**3
4. 3-9
5. 9%2
6. 15/4
7. 15//4
8. 15>>4
9. 15<<2
VARIABLES
A Python variable is a reserved memory location to store values. In other words, a
variable in a python program gives data to the computer for processing.
For better understanding, a variable is just like a name for the person.
Creating variables in Python is simple, you just have write the variable name on the
left side of = and the value on the right side.
Ex: a=5
RULES FOR DECLARING VARIABLES
A variable can have alphabets, digits, and underscore.
A variable name can start with the alphabet, and underscore only. It can't start with
a digit.
No whitespace is allowed within the variable name.
A variable name must not be any reserved word or keyword, e.g. int, class , etc.
DATA TYPES IN PYTHON
Data types specifies what type of data that can be stored in a variable.
In python the data type of variable can be automatically decided by the interpreter
based on the value stored in it.
There are 9 data types in python.
DATA TYPES
int: It is a data type which is used hold the perfect values.
Ex: 1,5,9,…..
float: It is a data type which is used to hold the values with decimal points.
Ex: 1.5, 2.9, 3.7,……..
complex: It is a data type which is used to hold the complex values. Complex values
are more useful in mathematical analysis. Complex variables are ends with letter “J”.
Ex: 3J, 9.5J,……..
string: It is a data type which contains set of characters enclosed by single-quotation
marks or doble-quotation marks.
Ex: ‘text’ (or) “text”
DATA TYPES
Boolean: Boolean is a data type which holds only 2 values. They are:
1)True.
2)False.
List: List is a ordered collection of values which holds multiple values under a single name
instead of declaring separate variable for each value. A list can be declared by using square
brackets.
Ex: a=[1, 2.5, 6J, True, ‘text’)
Tuple: Tuple is a ordered collection of values which holds multiple values under a single name
instead of declaring separate variable for each value. A tuple can be declared by using
round brackets.
A tuple cannot be changed after the declaration.
Ex: a=(1, 2.5, 6J, True, ‘text’)
DATA TYPES
Set: Set is a unordered collection of values which holds multiple values under a single name
instead of declaring separate variable for each value. A set can be declared by using curly
brackets.
Ex: a={1, 2.5, 6J, True, ‘text’}
A set cannot be accessed with index values like we do in list or tuple.
A set generates random order every time we print the set
Dictionary: Dictionary is a unordered collection of values where the data in dictionary
will be kept in a key-value pair format.
Ex: a={1:”text”,2:”sample”}
In dictionary we can’t access with index values to access values we need to use key names.
DATA TYPES IN PYTHON
Data Type Category Example
Integer A=3
Float Numeric Data Types B=3.5
Complex E=4J
String String Data Type C=‘sample’’, D=“sample”
Boolean Boolean Data Type X=True
List Sequence Data Type F=[1,3.5,”a”,’krish’,[1,5,3,9],True,3J]
Tuple G=(1,3.5,”a”,’krish’,[1,5,3,9],True,3J),
G=1,3.5,”a”,’krish’,[1,5,3,9],True,3J
Set Set Data Type H={1,3.5,”a”,’krish’,[1,5,3,9],True,3J}
Dictionary Mapping Data Type I={1:”test”,2:”well”,3:”done”}
LIST VS TUPLE VS SET DICTIONARY
List Tuple Set Dictionary
A dictionary is
A tuple is
A list is a collection A set is an unordered collection
an ordered collection of
of ordered data. an unordered collection. of data that stores data
data.
in key-value pairs.
Sets are mutable and Dictionaries are mutable
Lists are mutable. Tuples are immutable. have no duplicate and keys do not allow
elements. duplicates.
Dictionaries are
Lists are declared with Tuples are enclosed Sets are represented in enclosed in curly
square braces. within parenthesis. curly brackets. brackets in the form of
key-value pairs.
LIST VS TUPLE VS SET DICTIONARY
List Tuple Set Dictionary
The append() method
The update() method
adds a single item at An element cannot be The set add() method
updates the dictionary
the end of the list added to the tuple as it adds a given element to
with the specified key-
without modifying the is immutable. a set.
value pairs
original list.
The pop() method
The pop() method The pop() method
removes the item at the
Tuples are immutable. removes a random item removes the specified
given index from the list
from the set. item from the dictionary.
and returns it.
The sort() method sorts
the elements of a given Though tuples are Elements in the set sorted() method is used
list in a specific ordered, the elements cannot be sorted as they to sort the keys in the
ascending or descending cannot be sorted. are unordered. dictionary by default.
order.
LIST VS TUPLE VS SET DICTIONARY
List Tuple Set Dictionary
index() searches for a
given element from the Searches the tuple for a The index of a
The get() method returns
start of the list and specified value and particular element is not
the value of the item
returns the lowest index returns the position of retrieved as they are
with the specified key.
where the element where it was found. unordered.
appears.
The count() method
The count() method There are no count()
returns the number of The count() method is not
returns the number of methods in sets as they
times the specified defined in the
times a specified value do not allow any
element appears in the dictionary.
occurs in a tuple. duplicates.
list.
The sets are unordered, The elements cannot be
The reverse() method The reverse() method is
which refrains from reversed, as the items in
reverses the elements of not defined in tuples, as
applying the reverse() the dictionary are in the
the list. they are unchangeable
method form of key-value pairs
LIST/TUPLE SLICING
To access the values of list or tuple we need to use the following syntax:
print(variable[index])
Ex: a=[1,2,3,5.5,6J]
print(a[:]) #Output=[1,2,3,5.5,6J]
print(a[2:]) #Output=[3,5.5,6J]
print(a[1:3]) #Output=[2,3] it takes 1 less than the ending value.
print(a[-1:]) #Output=[6J]
LIST/TUPLE SLICING
print(a[-1:-3]) #Output=[]
print(a[:-2]) #Output=[1,2,3]
print(a[:-2:2]) #Output=[1,3]
print(a[1:-2:2]) #Output=[2]
ACCESSING ELEMENTS OF A DICTIONARY
To access elements of dictionary we can use normal method like accessing list or tuple
Ex: a={1:”first”,”second”:2}
print(a[“second”]) #Output: 2
But this method has a downside i.e., if we try to access any key which is not present in the
dictionary then you will get a error to avoid this we can use a method called get().
Syntax: dictionary_name.get(key, message)
Ex: a={1:”first”,”second”:2}
 print(a[“three”]) #Output: Error
In this case if we use a.get(“three”,”Not Available”) the message “Not Available” will be
printed.
HOW TO GET DATA TYPE OF VARIABLE?
For example let’s consider a variable with the following value:
X = [1,2,5,9J,’test’]
If you don’t know what type of variable it is then we can use a built-in function called
type().
So, if you use print(type(X)) then you will get the type of variable.
STRING METHODS
Method Description
capitalize() Converts the first character to upper case
count() Returns the number of times a specified value occurs in a string
index() Searches the string for a specified value and returns the position of where it was
found
join() Converts the elements of an iterable into a string
lower() Converts a string into lower case
upper() Converts a string into upper case
split() Splits the string at the specified separator, and returns a list
replace() Returns a string where a specified value is replaced with a specified value
format() Formats specified values in a string
FORMATTED STRINGS
Formatted strings are also known as f-strings these are used in the case of values of
variables to be printed in the string.
Syntax:
a=“James”
print(f”Hello {a}”)
Output:
Hello James
LIST METHODS
TUPLE METHODS
SET METHODS
SET METHODS
DICTIONARY METHODS
USER INPUT
The significance of user input in a program is to make the program respond for
different inputs. For example if we are designing a program for calculator with the
fixed values then the program will only work for those values but this is not the case in
real-time environment. In real-time the user will give different values every time they
run program.
To solve this problem python has function called “input()” to accept the input the user.
The default data type our input function accepts is “string”.
ACCEPTING DIFFERENT INPUTS FROM USER
Accepting integer input from user,
a=int(input())
Accepting float input from user,
a=float(input())
Accepting string input from user,
a=input()
We will see how to accept list and tuple from user later.
COMMENTS
Comments are used for documentation purpose.
These are used to give description about code segments for better readability and
understanding of code.
These are not only helpful for the developer who is developing currently it will also
helpful for the next developers who will take-up the project.
There are 2 types of comments. They are:
1)Single-Line Comment.
2) Multi-Line Comment.
TYPES OF COMMENTS
A single-line comment can be written with ‘#’ followed by your text.
Ex: #This is a single-line comment
A multi-line comment can be written in between three sets of single-quotation marks
or double-quotation marks.
Ex: ‘‘‘This is a multiline comment’’’
CONCEPT OF INDENTATION
Indentation plays a major role in specifying the scope of the conditional statement or
looping statement or function or a class.
Because like other languages in python we don’t have any curly braces to specify the
scope of the conditional statement/looping statement/function/class.
For example let’s take a conditional statement.
if condition:
#Your code starts here.
Inde-
ntati-
on
CONDITIONAL STATEMENTS
Conditional Statements are the statements which are used to change the flow of
execution of the program.
Python supports the following conditional statements. They are:
1) If statement.
2) If-else statement.
3) if-elif ladder.
4)Nested if.
IF STATEMENT
An if-statement is a conditional statement block where the statements inside the block
will get executed only under the condition is true.
Syntax:
if condition:
#Your Code starts here
IF STATEMENT
IF STATEMENT(EXAMPLE)
IF-ELSE STATEMENT
An if-else statement is an extension of if-statement the statement in if block will be
executed if the condition is satisfied or if it doesn’t satisfy then the code in else block
will be executed.
Syntax:
if condition:
#Your Code starts here
else:
#Your Code starts here
IF-ELSE STATEMENT
IF-ELSE STATEMENT EXAMPLE
IF-ELIF LADDER
If-elif ladder is conditional statement which can be used to check multiple conditions.
If if-elif ladder if the condition in if block satisfies then code inside if block will be
executed and stops there
 Else it will go to elif block if the condition in this block satisfies then code inside this
block will be executed and stops there or else it will go to the another elif block if it
is available or it will stop there.
If there is else block then the code in else block will execute. The else block will be
executed only if all the above blocks are failed.
IF-ELIF LADDER
Syntax:
if condition:
#Your Code Here
elif condition:
#Your Code Here
elif condition:
#Your Code Here
elif condition:
#Your Code Here
else:
#Your Code Here
IF-ELIF LADDER
IF-ELIF STATEMENT(EXERCISE)
NESTED IF STATEMENT
An if statement which is written inside another if statement.
Syntax:
if condition:
if condition:
#Your Code Here
NESTED IF STATEMENT
NESTED-IF STATEMENT(EXAMPLE)
EXERCISE(CONDITIONAL STATEMENTS)
Refer the following link to get exercises on conditional statements.
Python Conditional Statements
LOOPING STATEMENTS
Loops are used to do a task repeatedly.
For example, if you are writing a program that prints hello world for 10 times then
you will use, print(“Hello World”) for 10 times but what if we need to print 1,00,000
times at that time if we use print() statement for 1,00,000 times the program will
become very lengthy.
To solve this problem we will use the concept of loops.
Python supports 2 types of looping statements.
1)While loop.
2)For loop.
WHILE LOOP
A while loop is a looping statement which is used to repeat a code for the specified
number of times.
Syntax:
initialization
while condition:
# Your Code Here
increment/decrement
A while loop can be used in a situation where the no. of iterations are unknown.
WHILE LOOP(EXAMPLE)
Below program will print numbers from 1 to 10.
FOR LOOP
A for loop is a looping statement which is a simplified version of while loop which is
used to repeat a code for the specified number of times.
Syntax:
for loop_variable in range(start, end, step):
# Your Code Here
A for loop can be used in a situation where the no. of iterations are known.
In a for loop the end value is mandatory whereas the start and step arguments are
optional.
FOR LOOP(EXAMPLE)
Below program will print numbers from 1 to 10.
CONCEPT OF INFINITE LOOP
An infinite loop is a type of loop where it executes forever and its condition will
always evaluates to true.
Example:
BREAK
“Break” is a keyword which is used to terminate the execution of the block of the
code.
Example:
CONTINUE
Continue is a keyword which is used to skip iteration.
Example:
PASS
Pass is a keyword which is used keep space in block of code for future development.
Example:
CONCEPT OF NESTED LOOPS
A nested loop is a loop which is written inside another loop.
A nested-loop can be understood by a simple real-life example:
For example take a clock let’s take the minute’s hand and second’s hand into
consideration. For every 60 steps(one complete revolution) of second’s hand the
minute’s hand will move 1 step.
In the same way the outer loop will not get incremented/decremented until the inner
loop completes all of its iterations.
CONCEPT OF NESTED LOOPS(EXAMPLE)
A = [[1,5,9],[2,3],[8],[6,2,7],[4]]
for a in A:
for b in a:
print(b, end=“ ”)
print()
OUTPUT:
159
23
8
627
4
NESTED LOOP(EXERCISES)
Print the following patterns:
THINGS TO REMEMBER WHILE USING LOOPS
Use an appropriate condition.
Don’t forget to give either increment/decrement.
Use appropriate increment/decrement.
If you forgot these things the compiler may raise an error or leads to an infinite loop.
LOOPS(EXERCISE)
Program to calculate sum of numbers in a given range.
Program to calculate XY value.
Program to print factorial of the given value.
Generate numbers from 1 to 100.
Print all even numbers in range of 1-100.
Generate numbers from 100 to 1.
Print multiplication table of a given numbers up to 20 terms.
Print factorial of given number range.
LOOPS(EXERCISE)
Reverse the given number.
Palindrome Number.
Fibonacci Series.
Write a program to accept only 1 as the input.
Generate an infinite sequence of numbers.
FUNCTIONS
For example, if you are developing a software which contains some lakhs of lines of
code. In that if a particular task is repeated for multiple times write all the code
related to the task every-time whenever it required it will make our code very larger
and as well as time consuming.
Even though we know it is the same part of code but the compiler don’t know this and
will treat it as new part of code. So, this will make our code slow and lengthy to solve
this problem we will use the concept of functions in our code.
APPROACH OF FUNCTIONS
Function is technique of programming where the repeated lines of code is written in
separate block and can be used anywhere we want in our code.
Here whenever we use a function the control will be transferred to the block where it
is defined and executes its task and it will come back to where it left the main
program.
ADVANTAGES OF USING FUNCTIONS
Reduces the size of our program.
Makes our code much more readable.
Reusable code.
Reduces the use of memory.
Reduces the execution time.
TERMS RELATED TO FUNCTIONS
Return: “return” is a keyword which is used to take out a value from the function and
transfers the control to where the function is called.
Parameters: The variables which are used in function prototype are called
“parameters”.
Arguments: The variables which are used in function call are called “arguments”.
SYNTAX FOR WRITING AND USING A FUNCTION
A function in python can be written with a keyword called def followed by name of
the function.
Syntax:
def function_name():
# Your Code here
function_name()
FUNCTIONS(EXAMPLE)
CONCEPT OF ANONYMOUS FUNCTIONS
An anonymous function is a type of function which doesn’t have any name and can’t
be recognized as function by the python interpreter but will work just like a normal
function.
An anonymous function can be declared with the help of keyword of lambda.
Example:
GLOBAL VARIABLES AND LOCAL VARIABLES
A global variable is a type of variable which can be accessed any where in the
program.
A local variable is type of variable which can be accessed only within the scope it is
defined.
In a function if there is a variable having same name as a global variable then the
function will consider only local variable.
Because in a function local variables have more priority than global variables
GLOBAL VARIABLE AND LOCAL
VARIABLE(EXAMPLE)
FUNCTIONS(EXERCISE)
Write a function to print a factorial of all numbers in the given range.
Write a function to print all the palindrome numbers in the given numbers.
Write a function to print the reverse the given number.
Write a function to anonymous function to print the square of given number.
OOP (OBJECT ORIENTED PROGRAMMING) WITH
PYTHON
Object-Oriented Programming is a approach where a group of values will work
under a single entity named “class” to achieve a particular task.
A class is an entity which contains multiple variables/methods which helps to design a
software.
Advantages of OOP:
1) Modularity for easier troubleshooting
2) Reuse of code through inheritance
3) Flexibility through polymorphism
4) Effective problem solving
THE 4 PILLARS OF OOP
Inheritance
Polymorphism
Abstraction.
Encapsulation.
TERMS RELATED TO OOP
•Class − A user-defined prototype for an object that defines a set of attributes that
characterize any object of the class. The attributes are data members (class variables and
instance variables) and methods, accessed via dot notation.
•Class variable − A variable that is shared by all instances of a class. Class variables are
defined within a class but outside any of the class's methods. Class variables are not used as
frequently as instance variables are.
•Data member − A class variable or instance variable that holds data associated with a class
and its objects.
•Function overloading − The assignment of more than one behavior to a particular function.
The operation performed varies by the types of objects or arguments involved.
•Instance variable − A variable that is defined inside a method and belongs only to the
current instance of a class.
TERMS RELATED TO OOP
•Inheritance − The transfer of the characteristics of a class to other classes that are
derived from it.
•Instance − An individual object of a certain class. An object obj that belongs to a
class Circle, for example, is an instance of the class Circle.
•Instantiation − The creation of an instance of a class.
•Method − A special kind of function that is defined in a class definition.
•Object − A unique instance of a data structure that's defined by its class. An object
comprises both data members (class variables and instance variables) and methods.
SYNTAX FOR DECLARING A CLASS AND OBJECT TO
USE IT
Syntax:
class className:
#Your Code Here
objectName = className()
objectName.MethodName()
objectName.Variable
SELF
In OOP, while using a class with methods you need to define an extra parameter as
the first parameter.
The object which is created for the class will passed in the place of “self”
parameter.
Note: The self is not a fixed name we can give whatever the name we want. But it is
followed as the development standard among the developers of python.
INHERITANCE
Inheritance is technique which is used in OOP Methodology to use the properties of a
class in another class called “Derived class/Child Class”. And the class which it
inherits from is called “Base Class/Parent Class”.
It is especially used to develop updates for the application in real-time.
By using this we can modify the complete application or part of the application. With
the same functionality or feature extension.
INHERITANCE(SYNTAX)
Syntax:
class One:
#Your Code Here
class Two(One):
#Your Code Here
INHERITANCE(EXAMPLE)
To declare a method inside the class we need to pass atleast one parameter even
though our function doesn’t need arguments. Because the object of the class will be
passed as the parameter.
TYPES OF INHERITANCE
Single-Level Multi-Level Multiple Hierarchical Hybrid
class One: class One: class One: class One: class One:
pass pass pass pass pass
class Two(One): class Two(One): class Two: class Two(One): class Two(One):
pass pass pass pass pass
class Three(Two): class Three(One,Two): class Three(One): class Three(Two):
pass pass pass pass
class Four(Three):
pass
class Five(Three):
pass
FLOW OF INHERITANCE
Consider the following example for now.
FLOW OF INHERITANCE
If you consider the above example, we have created an object for class “Two”.
Now we are calling add() method using the object which we have created for class
“Two”.
As you see method add() is not available in class ‘Two’ it will search in class ‘One’ if
it is not available there also it will give an error.
POLYMORPHISM
Polymorphism: The word polymorphism means “one with many forms”.
It means same code can be utilized in different parts of your code in different way
but with the same name.
Polymorphism is sub-divided into 2 concepts. They are:
1)Method Overloading.
2)Method Overriding.
METHOD OVERLOADING(WITH EXAMPLE)
Method Overloading: It means the method which contains same name is declared
multiple times in the program with different no of parameters each time.
In this case, the interpreter will call the most recent method which is available with
that name.
METHOD OVERRIDING(WITH EXAMPLE)
Method Overriding: It is defined as the method with the same name is available in
both parent class and child class then the method in parent class will be overridden
by the child class.
ABSTRACTION
Abstraction is the process of hiding the internal functionality of the function from the
users. The users only interact with the basic implementation of the function, but the
inner working is hidden. User is familiar with “What a function does” but they don’t
what it does”.
For example, we all are familiar with TV but we don’t know how will it work
internally.
To implement the abstraction we need the concept of abstract classes.
CONCEPT OF ABSTRACT CLASS
Abstract class is template class for other classes. This used follow a standard way of
design the application with some mandatory features.
For example, let’s consider a mobile to make your mobile work you need an
operating system let’s consider android OS for this moment every mobile comes with
android OS but with a different UI(User Interface).
Abstract classes are type of classes which contains at least one abstract method.
A class can be made abstract by inheriting the abstract base class(ABC).
Note: 1)You cannot create object for abstract class.
2)The class which inherits an abstract class should implement all the abstract
methods which are in the abstract class. Or else the inherited class is also
treated as abstract class and you can’t use it.
HOW TO DEFINE ABSTRACT METHOD
An abstract method is class method which contains only methods name but not
implementation.
An abstract method in a class is defined according to the own need of the child class
in its body through inheritance.
A method can be made abstract by using a decorator called “@abstractmethod”.
ABSTRACT CLASS &ABSTRACT METHOD(EXAMPLE)
__INIT__() METHOD
__init__() method in python is called as constructor.
A constructor is a special method which will be called automatically when we create
an object for the class.
It is especially used to initialize some values for operation when we create an object
for the class.
For example, if you are opening an account in the bank then you are asked to
maintain minimum balance in the account.
__INIT__() METHOD(EXAMPLE)
ENCAPSULATION
Encapsulation is a mechanism of wrapping the data (variables) and code acting on
the data (methods) together as a single unit. In encapsulation, the variables of a class
will be hidden from other classes, and can be accessed only through the methods of
their current class.
Let’s say we have a company selling courses to students, engineers and professionals.
The different sections of this company include, operations, finance, accounts, sales, etc.
Now, if an employee from the accounts section needs the records of sales in 2022,
then he/ she cannot directly access it.
To access, the employee of the account sections needs to get permission from the
sales section team member. Therefore, the sales data is hidden from other
departments, In the same way, the financials of the company is accessible to only the
finance data and is hidden from other sections. The accounts, sales, finance,
operations, marketing, etc. data is hidden from other sections
ACCESS SPECIFIERS
Before seeing the example for encapsulation we need to know what are access
specifiers.
Access specifiers are used to limit the access of a class member. It is mainly used for
security purpose.
There are 3 access specifiers in python.
1)Public.
2)Protected.
3)Private.
ACCESS SPECIFIERS
Public: Methods or variables with this access specifiers can be used anywhere in our
program.
By default all the variables or methods declared inside a class are public.
Protected: Methods or variables with this access specifiers can be used inside a class
where it is defined and its child classes.
A method or variable can be made protected by using underscore before its name.
Private: Methods or variables with this access specifiers can be used only inside the
class where it is defined but cannot be accessed in its child class or outside of the
class.
A method or variable can be made private by using double underscore before its
name.
ENCAPSULATION (EXAMPLE)
OOP(EXERCISE)
Follow the below link to access the questions on OOP with python.
Python OOP Practice Questions
PIP
PIP is a tool to install python packages from internet.
A python package is a file where ready-made code is provided for developing more
complex applications with python where the manual implementation of every time
makes our code very larger.
To solve this problem python provides a way called “packaging”.
So by using a package we don’t need to write the whole code from scratch.
HOW TO USE A PACKAGE?
A package can be used with the following syntax,
import packageName
packagName.class.function()
And to import a specific class into your file you need to use the following syntax.
from packageName import class1.function(), class2.function(), …
PIP COMMANDS
Here is the list of some commonly used pip commands
To install a package,
pip install packageName
To uninstall a package,
pip uninstall packageName
To upgrade a package,
pip install –upgrade packageName
PIP COMMANDS
To get all the list of packages you have installed on system,
pip list (or) pip list installed
To get the outdated packages list,
pip list –outdated
To check the details of a package,
pip show packageName
HOW TO MAKE YOUR PYTHON PROJECT AS A
INSTALLABLE SOFTWARE
First you need to convert your python project into an exe file(on windows) using any
one of the following tool.
Pyinstaller or auto-py-to-exe.
Then make your project as a zip file which also includes your exe file.
Then install a software called NSIS from internet.
Open NSIS and select “Installer based on .zip file”.
Upload your file and click “generate” after that process has completed click close.
Now go to your project folder you can see a setup file which can be installed on any
system.

You might also like