Vpython 1-5
Vpython 1-5
List of Experiments
L PS PI
Sr. No. Title
O O
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 1
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 2
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 3
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
6.1.1,
4.3.1
COURSE IN CHARGE:
Vidya Gogate
Manisha Mane
Manjusha Kulkarni
Amit Tiwari
Pooja P.
Bhumika Tarate
Name: Signature:
Date:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 4
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Experiment No. 1
Problem Statement:
a. Write a python code to find the Maximum of two numbers entered by user.
● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
● Identity operators
● Bitwise operators
1 **
Exponentiation (raise to the power)
2 ~+-
Complement, unary plus and minus (method names for the last two are +@ and -@)
3 * / % //
Multiply, divide, modulo and floor division
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 5
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
4 +-
Addition and subtraction
5 >> <<
Right and left bitwise shift
6 &
Bitwise 'AND'
7 ^|
Bitwise exclusive `OR' and regular `OR'
9 <> == !=
Equality operators
10 = %= /= //= -= += *= **=
Assignment operators
11 is is not
Identity operators
12 in not in
Membership operators
13 not or and
Logical operators
Programs:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 6
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Write a python code to find the maximum of two numbers entered by user:
PROGRAM
Output:
PROGRAM
Output:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 7
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
PROGRAM
Output:
Experiment No. 2
Aim: Study of Strings, Lists, Tuples, Dictionaries, Sets, Accessing Elements, Properties,
Operations and methods on these data structures.
Problem Statement:
b. Write a program to carry out the following operations on the given set
s = {10,2,-3,4,5,88}
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 8
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
a) Number of items in set s
Theory:
String: String is sequence of Unicode characters. We can use single quotes or double
quotes to represent strings. Multi-line strings can be denoted using triple
quotes, ''' or ""“.
s = "This is a string”
List: List is an ordered sequence of items. It is one of the most used data type in
Python and is very flexible. All the items in a list do not need to be of the same type.
Declaring a list is pretty straight forward. Items separated by commas are enclosed
within brackets [ ].
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 9
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
We can use the slicing operator [ ] to extract an item or a range of items from a list.
Index starts form 0 in Python
Tuple: Tuple is an ordered sequence of items same as list. The only difference is that
tuples are immutable. Tuples once created cannot be modified.
Tuples are used to write-protect data and are usually faster than list as it cannot
change dynamically.
We can use the slicing operator [] to extract items but we cannot change its value.
Set: Set is an unordered collection of unique items. Set is defined by values separated
by comma inside braces {}. Items in a set are not ordered.
a = {5,2,3,1,4}
print(type(a))
We can perform set operations like union, intersection on two sets. Set have unique
values. They eliminate duplicates.
Since, set are unordered collection, indexing has no meaning. Hence the slicing
operator [] does not work.
It is generally used when we have a huge amount of data. Dictionaries are optimized
for retrieving data. We must know the key to retrieve the value.
In Python, dictionaries are defined within braces {} with each item being a pair in the
form key: value.
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 10
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Key and value can be of any type.
d = {1:'value','key':2}
We use key to retrieve the respective value. But not the other way around
PROGRAM:
OUTPUT:
2. Write a program to carry out the following operations on the given set
s = {10,2,-3,4,5,88}
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 11
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
f) Report whether is 100 an element of set s
PROGRAM:
OUTPUT:
PROGRAM:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 12
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
OUTPUT:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 13
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Experiment No: 03
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 14
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Theory:
Tuple is the collection of the object much like the lists. The sequence is s of value stored
in the tuple can be any type, and they are indexed by integers. Value of tuple are syntactically
separated by ‘commas. Although it is not necessary, it is more effective way to define tuple.
Sequence of the parenthesis, This help tuple to understand more easily. It is defined within
parentheses () where items are separated by commas. >>> t = (5,'program', 1+3j) we can use
the slicing operator [] to extract items but we cannot change its Value.
Dictionaries: Dictionaries are used to store data values in key: value pairs. A dictionary is a
collection which is ordered*, changeable and does not allow duplicates.
Example:
thisdict ={
"Food_category": "fruit",
"seasonal": "Mango",
"month": "April"
print(thisdict["Food_category"])
Program:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 15
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Output:
EXPERIMENT -4
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 16
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Aim: To study file handling operations, merging two files into the third file and to get
numbers, character, spaces and lines.
Problem Statement:
Typically there are four types of operations that can be handled by python on files such as-
● Open
● Read
● Write
● Close
The functions for each type are as follows-
1) Opening File - It can be opened for either read, write and append and it is the built-in
function.
▪ open()
Syntax- file_object=open(filename,mode)
The mode in the open function syntax will tell Python as what operation you want to
do on a file.
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 17
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
● ‘a’ – Append Mode: Append mode is used to append data to the file. Remember data
will be appended at the end of the file pointer.
● ‘r+’ – Read or Write Mode: This mode is used when we want to write or read the data
from the same file.
● ‘a+’ – Append or Read Mode: This mode is used when we want to read data from the
file or append the data into the same file.
● wb’ – Open a file for write only mode in the binary format.
● ‘rb’ – Open a file for the read-only mode in the binary format.
● ‘ab’ – Open a file for appending only mode in the binary format.
● ‘rb+’ – Open a file for read and write only mode in the binary format.
● ‘ab+’ – Open a file for appending and read-only mode in the binary format.
2) Reading File – To read from file, python provides many functions like
▪ read([n])
Syntax - Filehandle.read([n]) – It reads and return n bytes if n is not specified
it reads entire file.
▪ readline([n])
Syntax- Filehandle.readline([n]) – It reads a line of input if n is specified
reads at most n bytes.
▪ Readlines()
Syntax- Filehandle.readlines() - It reads all lines and returns them in a list.
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 18
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
▪ writelines()
Syntax – Filehandle.writelines(L) : Writes all string value in List L as lines to
file referenced by filehandle.
1) To read data
Program:
Output:
2) To write data
PROGRAM:
OUTPUT:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 19
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 20
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
PROGRAM:
OUTPUT:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 21
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
OUTPUT:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 22
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Experiment No: 05
PROBLEM STATEMENT:
a) Write a python program to implement classes, object, static method and inner class.
b) If any integer is given as in input through the keyboard, write a program to find whether it
is odd or even number.
c) If ages of Ram, Shyam and Ajay are given as an input through the keyboard, write a
program to determine the youngest of three.
d) Write a program that prints square root and cube root of numbers from 1 to 10, up to 4.
THEORY:
A class is a code template for creating objects. A class is user defined blueprint or prototype
from objects are created. Classes provide a means of bundling data and functionality together.
A class is defined using the class keyword followed by the class name and : operator after
the class name, which allows you to continue in the next indented line to define class
members.
x=5
An object is simply a collection of data (variables) and methods (functions) that act on
those data. An object is an instance of a class.
print(p1.x)
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 23
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Static methods, much like class methods that are bound to a class rather than its object.
They do not require a class instance creation. So, they are not dependent on the state of the
object.
Syntax :
Class C (object)
staticmethod
A class defined in another class is known as inner class or nested class. If an object is
created using child class means inner class then the object can also be used by parent class or
root class. A parent class can have one or more inner class.
a) Write Python program to implement classes, object, Static method and inner class
PROGRAM:
output:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 24
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
b) If any integer is given as in input through the keyboard , write a program to find
whether it is even or odd.
Program:
Output:
c) If ages of Ram, Shyam and Ajay are given as an input through the keyboard, write a
program to determine the youngest out of three.
Program:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 25
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Output:
d) Write a program that prints square roots and cube roots of numbers from 1 to 10 , up to 4.
PROGRAM:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 26
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING
COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
output:
SAKEC, ECS, SE, SEM-IV, 2023-24 SKILL BASED LAB, PYTHON PROG 27