Professional Documents
Culture Documents
ON
PYTHON PROGAMMING LANGUAGE
[SUBMITTED IN PARTIAL FULFILLMENT OF B.VOC-SOFTWARE DEVELOPMENT
(2018- 2021)]
AS A PART OF THE CURRICULUM OF BACHELOR OF VOCATION
[B.VOC-(SD)]
FROM
1. Python introduction
1.1 Beginning Python Programming
1.2 Python history and version
1.3 Python feature
1.4 Python application
2. How to install Python
2.1 installation on window
2.2 Unix and Linux installation
3. Python variable
3.1 Type of variables
4. Data types
5. Operators of Python
5.1 Type of operators
6. Decision making
6.1. if statement
6.2 if-else statement
7. Loops in Python
7.1While loop
7.2 For loop
8. Python function
8.1 required argument
8.2 keyword argument
8.3 Default argument
8.4 Variable length argument
9. File handling
10. Python Oops concept
10.1 object
10.2 class
10.3 Inheritance
10.4 Polymorphism
10.5 encapsulation
11. Database in python
11.1 what is database
11.2 RDBMS terminology
11.3 what is MySQL dB
11.4 how do install MySQL dB
11.5 how MySQL works
12. Python module
13. GUI programming (Tkinter) in python
14. Tkinter programming
15. What is pip?
15.1 installation package with pip
19. Project in Python
19.1 Abstract
19.2 Graphical user interface with TK
19.3 Tkinter Programming
20. Requirement Specification
20. 1 Software requirement specification
20.2 Hardware requirement specification
21. Diagram
21.1 0 level DMD
21.2 Usage case diagram
22. Snapshot
22.1 Snapshot
22.2 Snapshot
23. Conclusion
24. Reference
Python
1. Python Language Introduction
Python is a widely used general-purpose, high level programming
language. It was initially designed by Guido van Rossum in 1991 and
developed by Python Software Foundation. It was mainly developed for
emphasis on code readability, and its syntax allows programmers to
express 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.
1) Finding an Interpreter:
Before we start Python programming, we need to have an interpreter to
interpret and run our programs.
Windows: There are many interpreters available freely to run Python
scripts like IDLE (Integrated Development Environment) which is
installed when you install the python software from http://python.org/
Linux: For Linux, Python comes bundled with the Linux.
2) Writing first program:
Following is first program in Python
JI Script Begins
print ("Geeks Quiz”) # Scripts Ends
Output:
Geeks Quiz
1.2 Python History and Versions
Python laid its foundation in the late 1980s.The implementation of
Python was started in the December 1989 by Guido Van Rossum at CWI
in Netherland. In February 1991, van Rossum published the code
(labelled version 0.9.0) to alt. sources. In 1994, Python 1.0 was released
with new features like: lambda, map, filter, and reduce. Python 2.0
added new features like: list comprehensions, garbage collection
system. On December 3, 2008, Python 3.0 (also called "Py3K") was
released. It was designed to rectify fundamental flaw of the language.
ABC programming language is said to be the predecessor of Python
language which was capable of Exception Handling and interfacing with
Amoeba Operating System.
1.3 Python Features
Easy to Learn and Use: Python is easy to learn and use. It is
developer-friendly and high-level programming language.
It is more understandable and readable.
Interpreted Language: Python is an interpreted language i.e.
interpreter executes the code line by line at a time. This makes
debugging easy and thus suitable for beginners.
Cross-platform Language: Python can run equally on different
platforms such as Windows, Linux, Unix and Macintosh etc. “] So,
we can say that Python is a portable language.
Free and Open Source: Python language is freely available at
official. The source-code is also available. Therefore, it is open
source.
Object-Oriented Language: Python supports object-oriented
language and concepts of classes and objects come into
existence.
Extensible: It implies that other languages such as C/C++ can be
used to compile the code and thus it can be used further in our
python code.
Large Standard Library: Python has a large and broad library and
provides rich set of module and functions for rapid application
development.
GUI Programming Support: Graphical user interfaces can be
developed using Python.
Integrated: It can be easily integrated with languages like C, C++,
JAVA etc.
1. Arithmetic operators
Arithmetic operators are used to perform arithmetic operations between
two operands. It includes +(addition), - (subtraction), *(multiplication),
/(divide), %(reminder), // (floor division), and exponent (*”).
2. Comparison operator
Comparison operators are used to comparing the value of the two
operands and returns Boolean true or false accordingly.
3. Python assignment operators
The assignment operators are used to assign the value of the right
expression to the left operand. The assignment operators are described
in the following table.
4. Bitwise operator
The bitwise operators perform bit by bit operation on the values of the
two operands.
5. Logical Operators
The logical operators are used primarily in the expression evaluation to
make a decision. Python supports the following logical operators.
6. Membership Operators
Python membership operators are used to check the membership of
value inside a data structure. If the value is present in the data structure,
then the resulting value is true otherwise it returns false.
7. Identity Operators
Identity operators compare the memory locations of two objects. There
are two Identity operators is or is not.
8. Operator Precedence
The precedence of the operators is important to find out since it enables
us to know which operator should be evaluated first.
6. Decision Making
Decision-making is the anticipation of conditions occurring during the
execution of a programs and specified actions taken according to the
conditions. Decision structures evaluate expressions, which produce
TRUE or FALSE as the outcome. You need to determine which action to
take and which statements to execute if the outcome is TRUE or FALSE
otherwise
6.1 IF Statement
The IF statement is similar to that of other languages. The if statement
contains a logical expression using which the data is compared and a
decision is made based on the result of the comparison.
6.2 if-else statement
An else statement can be combined with an if statement. An else
statement contains a block of code that executes if the conditional
expression in the if statement resolves to 0 or a FALSE value.
7. LOOPS IN PYTHON
Python programming language provides following types of loops to
handle looping requirements. Python provides three ways for executing
the loops. While all the ways provide similar basic functionality, they
differ in their syntax and condition checking time.
7.1 While Loop
In python, while loop is used to execute a block of statements repeatedly
until a given a condition is satisfied. And when the condition becomes
false, the line immediately after the loop in program is executed.
Syntax:
while expression:
statement(s)
Using else statement with while loops: As discussed above, while loop
executes the block until a condition is satisfied. When the condition
becomes false, the statement immediately after the loop is
executed.
The else clause is only executed when your while condition becomes
false. If you break out of the loop, or if an exception is raised, it won’t be
executed.
Single statement while block: Just like the if block, if the while block
consists of a single statement the we can declare the entire loop in a
single line as shown below:
Note: It is suggested not to use this type of loops as it is a never ending
infinite loop where the condition is always true and you have to forcefully
terminate the compiler.
7.2 For in Loop
For loops are used for sequential traversal. For example: traversing a list
or string or array etc.
In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for
in” loop which is similar to for each loop in other languages. Let us learn
how to use for in loop for sequential traversals.
Syntax:
for iterator var in sequence:
statements(s)
It can be used to iterate over iterators and a range.
Using else statement with for loops:
We can also combine else statement with for loop like in while loop. But
as there is no condition in for loop based on which the execution will
terminate so the else block will be executed immediately after for block
finishes execution. A final note on loop nesting is that we can put any
type of loop inside of any other type of loop.
1. Loop Control Statements:
Loop control statements change execution from its normal sequence.
When execution leaves a scope, all automatic objects that were created
in that scope are destroyed. Python supports the following control
statements.
Continue Statement: It returns the control to the beginning of the
loop.
Break Statement: It brings control out of the loop
Pass Statement: We use pass statement to write empty loops.
Pass is also used for empty control statement, function and
classes.
8. Python Functions
Functions are the most important aspect of an application. A function
can be defined as the organized block of reusable code which can be
called whenever required. Python allows us to divide a large program
into the basic building blocks known as function. The function contains
the set of programming statements enclosed by (). A function can be
called multiple times to provide reusability and modularity to the python
program.
Advantage of functions in python program.
We can track a large python program easily when it is divided into
multiple functions.
Reusability is the main achievement of python functions.
However, Function calling is always overhead in a python program.
Types of arguments
There may be several types of arguments which can be passed at the
time of function calling.
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
8.1 Required Arguments
Till now, we have learned about function calling in python. However, we
can provide the arguments at the time of function calling. As far as the
required arguments are concerned, these are the arguments which are
required to be passed at the time of function calling with the exact match
of their positions in the function call and function definition.
8.2 Keyword arguments
Python allows us to call the function with the keyword arguments. This
Lind of function call will enable us to pass the arguments in the random
order.
The name of the arguments is treated as the keywords and matched in
the function calling and definition. If the same match is found, the values
of the arguments are copied in the function definition.
8.3 Default Arguments
Python allows us to initialize the arguments at the function definition. If
the value of any of the argument is not provided at the time of function
call, then that argument can be initialized with the value given in the
definition
8.4 Variable length Arguments
In the large projects, sometimes we may not know the number of
arguments to be passed in advance. In such cases, Python provides us
the flexibility to provide the comma separated values which are internally
treated as tuples at the function call.
Scope of variables
The scopes of the variables depend upon the location where the variable
is being declared. The variable declared in one part of the program may
not be accessible to the other parts.
In python, the variables are defined with the two types of scopes.
Global variables Local variables
The variable defined outside any function is known to have a global
scope whereas the variable defined inside a function is known to have a
local scope.
File handling: -
The key function for working with files in Python is the open () function.
The open () function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:
• "r" - Read - Default value. Opens a file for reading, error if the file does
not exist
• "a" - Append - Opens a file for appending, creates the file if it does not
exist
• "w" - Write - Opens a file for writing, creates the file if it does not exist
• "x" - Create - Creates the specified file, returns an error if the file exists
• In addition, you can specify if the file should be handled as binary or
text mode
• "t" - Text - Default value. Text mode
• "b" - Binary - Binary mode (e.g. images)
9. Python OOPs Concepts
Like other general-purpose languages, python is also an object-oriented
language since its beginning. Python is an object-oriented programming
language. It allows us to develop applications using an Object-Oriented
approach. In Python, we can easily create and use classes and objects.
Major principles of object-oriented programming system are given below.
Object
Class
Method
Inheritance Polymorphism
Data Abstraction
Encapsulation
9.1. Object
The object is an entity that has state and behaviour. It may be any real-
world object like the mouse, keyboard, chair, table, pen, etc.
Everything in Python is an object, and almost everything has attributes
and methods. All functions have a built-in attribute doc which returns
the doc string defined in the function source code.
9.2 Class
The class can be defined as a collection of objects. It is a logical entity
that has some specific attributes and methods. For example: if you have
an employee class then it should contain an attribute and method, i.e. an
email id, name, age, salary, etc.
9.3 Inheritance
Inheritance is the most important aspect of object-oriented programming
which simulates the real-world concept of inheritance. It specifies that
the child object acquires all the properties and behaviours of the parent
object. By using inheritance, we can create a class which uses all the
properties and behaviour of another class. The new class is known as a
derived class or child class, and the one whose properties are acquired
is known as a base class or parent class.
9.4 Polymorphism
Polymorphism contains two words "poly" and "morphs". Poly means
many and Morphs means form, shape. By polymorphism, we understand
that one task can be performed in different ways. For example, you have
a class animal, and all animals speak. But they speak differently. Here,
the "speak" behaviour is polymorphic in the sense and depends on the
animal.
9.5 Encapsulation
Encapsulation is also an important aspect of object-oriented
programming. It is used to restrict access to methods and variables. In
encapsulation, code and data are wrapped together within a single unit
from being modified by accident.
Database in Python: -
The Python standard for database interfaces is the Python DB-API. Most
Python database interfaces adhere to these standards. You can choose
the right database for your application. Python Database API supports a
wide range of database servers such as –
• GadFly
• mSQL
• MySQL
• PostgreSQL
• Microsoft SQL Server 2000
• Informix
• lnterbase
• Oracle
• Sybase
Here is the list of available Python database interfaces: Python
Database Interfaces and APIs. You must download a separate DB API
module for each database you need to access. For example, if you need
to access an Oracle database as well as a MySQL database, you must
download both the Oracle and the MySQL database modules.
The DB API provides a minimal standard for working with databases
using Python structures and syntax wherever possible. This API includes
the following -
Importing the API module.
Acquiring a connection with the database.
Issuing SQL statements and stored procedures.
Closing the connection
We would learn all the concepts using MySQL, so let us talk about
MySQL dB module.
11.1 What is a Database?
A database is a separate application that stores a collection of data.
Each database has one or more distinct APIs for creating, accessing,
managing, searching and replicating the data it holds. Other kinds of
data stores can also be used, such as files on the file system or large
hash tables in memory but data fetching and writing would not be so fast
and easy with those type of systems. Nowadays, we use relational
database management systems (RDBMS) to store and manage huge
volume of data. This is called relational database because all the data is
stored into different tables and relations are established using primary
keys or other keys known as Foreign Keys.
A Relational Database Management System (RDBMS) is a software that
• Enables you to implement a database with tables, columns
and indexes.
• Guarantees the Referential Integrity between rows of various
tables.
• Updates the indexes automatically.
• Interprets an SQL query and combines information from
various tables.
Talking about the features of the Bank Management System, a user can
create an account by providing the name of the account holder, number,
selecting amount type (Saving account or Current account) and
providing an initial amount more than or equal to 500. Then the user can
also deposit and withdraw money just by providing his/her account and
entering the amount. For certain purpose, he/she can also check for the
balance inquiry which displays the account number and amount. He/she
can also view all the account holder’s list. Another feature is that he/she
can modify their account detail and type if they want to.
Fundamental Features:
Source code
#import sys;
class BankAccount:
def __init__(self, accountNumber, name ):
self.__accountNumber = accountNumber
self.__name = name
def get_account_number(self):
return self.__accountNumber
def get_name(self):
return self.__name
def set_account_number(self, value):
self.__accountNumber = value
def set_name(self, value):
self.__name = value
class FixedDeposit(BankAccount):
def __init__(self, accountNumber, name, duration, amount,
rateOfInterest ):
super().__init__(accountNumber,name)
self.__duration = duration
self.__amount = amount
self.__rateOfInterest = rateOfInterest
def get_duration(self):
return self.__duration
def get_amount(self):
return self.__amount
def get_rate_of_interest(self):
return self.__rateOfInterest
def set_duration(self, value):
self.__duration = value
def set_amount(self, value):
self.__amount = value
def set_rate_of_interest(self, value):
self.__rateOfInterest = value
class RecurringDeposit(BankAccount):
def __init__(self, accountNumber, name, duration, monthlyPayment,
rateOfInterest ):
super().__init__(accountNumber,name)
self.__duration = duration
self.__monthlyPayment = monthlyPayment
self.__rateOfInterest = rateOfInterest
def get_duration(self):
return self.__duration
def get_monthly_payment(self):
return self.__monthlyPayment
def get_rate_of_interest(self):
return self.__rateOfInterest
def set_duration(self, value):
self.__duration = value
def set_monthly_payment(self, value):
self.__monthlyPayment = value
def set_rate_of_interest(self, value):
self.__rateOfInterest = value
class BankDemo:
n=0
ch='n'
def bankOptions(self):
print("******************************")
print(" Bank Account Demonstration ")
print("******************************")
print("1. Read & Write Fixed Deposit")
print("2. Read & Write Recurring Deposit")
print("3. Exit")
n=int(input("\nSelect your choice : "))
if n==1:
self.readWriteFixedDeposit()
elif n==2:
self.readWriteRecurringDeposit()
elif n==3:
print("Thank You!!!")
exit()
else:
print("invalid option!\n try again\n")
self.bankOptions()
def readWriteFixedDeposit(self):
print("\nGive Fixed Deposit Details\n")
j=0
while(j==0):
try:
accountNumber = int(input("Enter Account Number: ").strip())
j=1
except:
print("Please try again")
name =input("Enter Account Name: ").strip()
j=0
while(j==0):
try:
duration = int(input("Duration: ").strip())
j=1
except:
print("Please try again")
j=0
while(j==0):
try:
amount = float(input("Amount: ").strip())
j=1
except:
print("Please try again")
j=0
while(j==0):
try:
rateOfInterest = float(input("Rate of Interest: ").strip())
j=1
except:
print("Please try again")
fd = FixedDeposit(accountNumber, name, duration, amount,
rateOfInterest)
print("\nFixed Deposit Details are...\n")
print("\nAccount Number: ", fd.get_account_number())
print("Account Name: ", fd.get_name())
print("Duration: ", fd.get_duration())
print("Amount: ", fd.get_amount())
print("Rate of Interest: ", fd.get_rate_of_interest())
def readWriteRecurringDeposit(self):
print("\nGive Recurring Deposit Details\n")
j=0
while(j==0):
try:
accountNumber = int(input("Enter Account Number: ").strip())
j=1
except:
print("Please try again")
name =input("Enter Account Name: ").strip()
j=0
while(j==0):
try:
duration = int(input("Duration: ").strip())
j=1
except:
print("Please try again")
j=0
while(j==0):
try:
monthlyPayment = float(input("Monthly Payment: ").strip())
j=1
except:
print("Please try again")
j=0
while(j==0):
try:
rateOfInterest = float(input("Rate of Interest: ").strip())
j=1
except:
print("Please try again")
rd = RecurringDeposit(accountNumber, name, duration,
monthlyPayment, rateOfInterest)
print("\nRecurring Deposit Details are...\n")
print("\nAccount Number: ", rd.get_account_number())
print("Account Name: ", rd.get_name())
print("Duration: ", rd.get_duration())
print("Monthly Payment: ", rd.get_monthly_payment())
print("Rate of Interest: ", rd.get_rate_of_interest())
def gotooptions(self):
if self.n!=3:
ch=input("\nDo you wish to continue(y/n)").strip()
if(ch[0]=='y' or ch[0]=='Y'):
BankDemo.bankOptions(self)
else:
print("Thank you!")
exit()
demoObject = BankDemo()
demoObject.bankOptions()
demoObject.gotooptions()
Outputs
Conclusion