Done
Done
Session 2022-23
On
Submitted by: -
Name – Anshul Mehta
Class - CSE-B
Roll.no - 2021/452
Registration no – 200529581405
1|Page
PATIALA-147001
DECLARATION
I, Anshul Mehta (200529581405) hereby declare that the work which is being presented in
the report “Python Programming” is the record of authentic work to be carried out by me in
the partial fulfilment for the award of diploma in Computer Science and Engineering, this
work has not been submitted to any other university/institute for the award of any
degree/diploma.
2|Page
Acknowledgment
I have taken efforts in this project. However, it would not have been possible without the kind
support and help of many individuals and organizations. I would like to extend my sincere
thanks to all of them.
TABLE OF CONTENT
3|Page
DECLARATION…………………………………….…. 2
ACKNOWLEDGE……………………………………....3
CERTIFICATE…………………………………………. 6
1.INTRODUCTION……………………………………. 7
1.1 PYTHON…………………………………………7
1.2 HISTORY…………………………………….….8
1.3 PYTHON FEATURE…………………………....8
1.4 ADVANTAGES…………………………………8
2.OPERATORS………………………………………….9
2.1 TYPES OF OPERATORS……………………....9
3. LOOPS………………………………………………..11
3.1 TYPES …………………………………...…..…11
4. LIST IN PYTHON …………………………………..13
4.1 LIST FUNCTION……………………….………..14
5. TUPLE……………………………………………….15
5.1 TUPLE FUNCTION……………………………..16
6. STRING IN PYTHON……………………………….17
6.1 STRING FUNCTION
TABLE………………………………………………….18
7. FUNCTION IN PYTHON…………………..……….19
7.1 TYPE OF FUNCTION……….………………….19
8 FILE HANDLING………………………………...….21
8.1 SYNTAX………………… .…..…………...….21
9. EXCEPTION HANDLING.……………….……….22
9.1 RAISING EXCEPTION ……………..…….….23
4|Page
10. OOPS IN PYTHON…………………………….…25
10.1 OOPS CONCEPT………………………….….25
10.1.1CLASS…………………………..…..……….26
10.1.2OBJECT……………………………..……….26
10.1.3 INHERITANCE…………………….……….26
10.1.4 INCAPSULATION…………….…………....27
10.1.5 POLYMORPHISM……………………...….27
10.1.6 ABSTRACTION…………………………….28
11 TKTINTER …………………………...…..……….28
11.1 FUNCTION ………………………….……….29
11.2 OUTPUT……………………………..……….30
CERTIFICATE
5|Page
1. INTRODUCTION
6|Page
1.1 PYTHON
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python
is designed to be highly readable. It uses English keywords frequently where as other
languages use punctuation, and it has fewer syntactical constructions than other languages.
• Python is Interpreted − Python is processed at runtime by the interpreter. You do not need
to compile your program before executing it. This is similar to PERL and PHP.
• Python is Interactive − You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
• Python is Object-Oriented − Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
1.2 HISTORY
was developed by Guido van Rossum in the late eighties and early nineties at the National
Research Institute for Mathematics and Computer Science in the Netherlands. Python is
derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, Smalltalk,
and Unix shell and other scripting languages. Python is copyrighted. Like Perl, Python source
code is now available under the GNU General Public License Python is now maintained by a
core development team at the institute, although Guido van Rossum still holds a vital role in
directing its progress.
7|Page
• Easy-to-maintain − Python's source code is fairly easy-to-maintain.
• A broad standard library − Python's bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
• Interactive Mode − Python has support for an interactive mode which allows interactive
testing and debugging of snippets of code.
• Portable − Python can run on a wide variety of hardware platforms and has the same
interface on all platforms.
• Extendable − You can add low-level modules to the Python interpreter. These modules
enable programmers to add to or customize their tools to be more efficient.
• Databases − Python provides interfaces to all major commercial databases.
• Scalable -Python provides a better structure and support for large programs than shell
scripting.
1.4 ADVANTAGES
• It supports functional and structured programming methods as well as OOP.
• It can be used as a scripting language or can be compiled to byte-code for building large
applications.
• It provides very high-level dynamic data types and supports dynamic type checking.
• It supports automatic garbage collection.
• It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Jav
2. OPERATORS
8|Page
Assignment operators:
Assignment operators are used
in Python to assign values to
variables.
a = 5 is a simple assignment
operator that assigns the value
9|Page
5 on the right to the variable a
on
the left.
There are various compound
operators in Python like a += 5
that adds to the variable and
later
assigns the same. It is
equivalent to a = a + 5.
2.1 TYPES OF OPERATORS:
Assignment operators:
Assignment operators are used in Python to assign values to variables. a = 5 is a simple
assignment operator that assigns the value 5 on the right to the variable a on the left. There
are various compound operators in Python like a += 5 that adds to the variable and later
assigns the same. It is equivalent to a = a + 5.
Arithmetic operators: Python arithmetic operators are used to perform addition, subtraction,
multiplication, and division. They act as basic mathematical operations.
Example:
x = 15
y=4
print('x + y =', x + y)
# Output: x + y = 19
print ('x - y =', x-y)
# Output: x - y = 11
10 | P a g e
print('x * y =', x*y)
# Output: x * y = 60
Arithmetic operators: Python arithmetic operators are used to perform addition,
subtraction, multiplication, and division. They act as basic mathematical operations.
Example: x = 15 y = 4
print('x + y =', x + y)
# Output: x + y = 19
print ('x - y =', x-y)
# Output: x - y = 11
print('x * y =', x*y)
# Output: x * y = 60
Bitwise Operators: Bitwise operators act on operands as if they were strings of binary digits.
They operate bit by bit, hence the name.
For example, 2 is 10 in binary and 7 is 111.
Bitwise Operators: Bitwise operators act on operands as if they were strings of binary
digits. They operate bit by bit, hence the name.
For example: 2 is 10 in binary and 7 is 111.
Logical operators: Comparison operators are used to compare values. It returns either True
or False according to the condition.
Example:
x = True
y = False hby
print('x and y is', x and y)
#Output: x and y is False
print('x or y is', x or y)
Identity operators: is and is not are the identity operators in Python. They are used to
check if two values (or variables) are located on the same part of the memory. Two variables
that are equal does not imply that they are identical.
x1 = 5
y1 = 5
print(x1 is not y1)
# Output: False
11 | P a g e
3. LOOPS
3.1 TYPES
The while Loop: With the while loop we can execute a set of statements as long as a
condition is true.
13 | P a g e
Example:
Print i as long as i is less than 6:
i
while i < 6:
print(i)
i += 1
For Loops: A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).This is less like the for keyword in other programming
languages, and works more like an iterator method as found in other object-orientated
programming languages.
EXAMPLE
Print each fruit in a fruit list:
fruits =
["apple", "banana", "cherry"]
for x in fruits:
print(x)
4. LIST IN PYTHON
14 | P a g e
LIST The list is a most versatile datatype available in Python which can be written as a list of
comma-separated values (items) between square brackets. Important thing about a list is that
items in a list need not be of the same type.
For example –
list2 = [1, 2, 3, 4, 5 ]
Lists respond to the + and * operators much like strings; they mean
concatenation and repetition here too, except that the result is a new
list, not a string.
15 | P a g e
List Functions & Methods
Python includes the following
list functions −
4.1 List Functions & Methods
16 | P a g e
insert() Adds an element at the specified position
5. TUPLE
A tuple is a collection of objects which ordered immutable. Tuples are sequences, just like
lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists
and tuples use parentheses, whereas lists use square brackets. Optionally you can put these
comma-separated values between parentheses also. For example − tup1 = ('physics',
'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5 ) tup3 = "a", "b", "c", "d" The empty tuple is
17 | P a g e
written as two parentheses containing nothing − tup1 = ( ) To write a tuple containing a single
value you have to include a comma, even though there is only one value − tup1 = (50,) Like
string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on
18 | P a g e
19 | P a g e
6. STRINGS IN PYTHON
Python string is the collection of the characters surrounded by single quotes, double quotes,
or triple quotes. The computer does not understand the characters; internally, it stores
manipulated character as the combination of the 0's and 1's.
Each character is encoded in the ASCII or Unicode character. So we can say that Python
strings are also called the collection of Unicode characters.
In Python, strings can be created by enclosing the character or the sequence of characters in
the quotes. Python allows us to use single quotes, double quotes, or triple quotes to create the
string.
20 | P a g e
7. FUNCTIONS IN PYTHON
21 | P a g e
Function can be reused by the programmer in a given program infinite number of times
Syntax:
Function Call :-
Whenever we want to call a function, we put that function name followed by parentheses as
follow:
fun()
Where fun is name of the function
Functions with Arguments :- A function can accept some values by putting them in
“( )”
For eg:
22 | P a g e
def greet(name):
gr=(“Hello” +name)
Return gr
a= greet(“world”)
Python has several functions for creating, reading, updating, and deleting files. File handling
is an important part of any web application.
"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 exits
23 | P a g e
8.1 Syntax:-
To open a file for reading it is enough to specify the name of the file:
f = open("demofile.txt")
f = open("demofile.txt", "rt")
Because "r" for read, and "t" for text are the default values, you do not need to specify them.
INTRODUCTION:-
24 | P a g e
Exceptions versus Syntax Errors:-
Syntax errors occur when the parser detects an incorrect statement. Observe the following
example:
>>> print( 0 / 0 ))
File "<stdin>", line 1
print( 0 / 0 ))
^
SyntaxError: invalid syntax
The arrow indicates where the parser ran into the syntax error. In this example, there was
one bracket too many. Remove it and run your code again:
>>> print( 0 / 0)
Instead of showing the message exception error, Python details what type of exception error
was encountered. In this case, it was a ZeroDivisionError. Python comes with various built-in
exceptions as well as the possibility to create self-defined exceptions.
25 | P a g e
A Python program terminates as soon as it encounters an error. In Python, an error can be a
syntax error or an exception.
In this course, you’ll learn what an exception is and how it differs from a syntax error. After
that, you’ll learn about raising exceptions and making assertions. Then, you’ll learn how to
catch exceptions to prevent your program from unintentionally ending and to change the
control flow of your program:
You’ll learn about the basic use of a try … except block, as well as how to extend it
using else and finally.
26 | P a g e
27 | P a g e
10. Python OOPs Concepts
Class
Objects
Polymorphism
Encapsulation
Inheritance
Data Abstraction
10.1.1 Class
A class is a collection of objects. A class contains the blueprints or the prototype from
which the objects are being created. It is a logical entity that contains some attributes and
methods.
28 | P a g e
To understand the need for creating a class let’s consider an example, let’s say you wanted
to track the number of dogs that may have different attributes like breed, age. If a list is
used, the first element could be the dog’s breed while the second element could represent
its age. Let’s suppose there are 100 different dogs, then how would you know which
element is supposed to be which? What if you wanted to add other properties to these dogs?
This lacks organization and it’s the exact need for classes.
10.1.2 Objects
The object is an entity that has a state and behavior associated with it. It may be any real-
world object like a mouse, keyboard, chair, table, pen, etc. Integers, strings, floating-point
numbers, even arrays, and dictionaries, are all objects. More specifically, any single integer
or any single string is an object. The number 12 is an object, the string “Hello, world” is an
object, a list is an object that can hold other objects, and so on. You’ve been using objects
all along and may not even realize it.
To understand the state, behavior, and identity let us take the example of the class dog
(explained above).
The identity can be considered as the name of the dog.
State or Attributes can be considered as the breed, age, or color of the dog.
The behavior can be considered as to whether the dog is eating or sleeping.
10.1.3 Inheritance
Inheritance is the capability of one class to derive or inherit the properties from another
class. The class that derives properties is called the derived class or child class and the class
from which the properties are being derived is called the base class or parent class. The
benefits of inheritance are:
It represents real-world relationships well.
29 | P a g e
It provides the reusability of a code. We don’t have to write the same code again and
again. Also, it allows us to add more features to a class without modifying it.
It is transitive in nature, which means that if class B inherits from another class A, then
all the subclasses of B would automatically inherit from class A.
10.1.5 Encapsulation
10.1.5 Polymorphism
Polymorphism simply means having many forms. For example, we need to determine if the
given species of birds fly or not, using polymorphism we can do this using a single
function.
30 | P a g e
10.1.6 Data Abstraction
It hides the unnecessary code details from the user. Also, when we do not want to give out
sensitive parts of our code implementation and this is where data abstraction came.
31 | P a g e
11.1 FUNCTION AND PROGRAM
32 | P a g e
g=Entry(f,show="*")
g.place(x=120,y=80)
cb=Checkbutton(f,text="show password")
cb.place(x=100,y=120)
b=Button(f,text="login")
b.place(x=100,y=150)
s=IntVar()
s1=IntVar()
rd=Radiobutton(f,text="male",value=s,var=s)
rd.place(x=150,y=150)
gh=Radiobutton(f,text="female",value=s1,var=s)
gh.place(x=150,y=190)
from tkinter .ttk import Combobox
t=("jalandhar","patiala","chandigarh","delhi")
c=Combobox(f,text="combobox",value=t)
c.place(x=250,y=150)
f.mainloop()
11.1.1 OUTPUT:-
33 | P a g e
34 | P a g e