Iit Python Virtual Lab

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

-:INDEX:-

SR.NO AIM Signature


1. In this experiment the Student will be able to understand
the basics of Arithmetic Operations used in Python
programming Language with the help of a iterative
simulator.
2. In this experiment the Student will be able to understand
the basics of functions used in Python programming
language with the help of a iterative simulator
3. In this experiment the Student will be able to understand
the flow of controls and types of loops used in Python
programming language with the help of a iterative
simulator.
4. In this experiment, user will learn about the various
array types in Python programming language. To
implement those data types in programs.
5. In this experiment, user will learn -
The concepts of string in Python programming
language.
To implement the operations that can be performed in a
string.
6. In this experiment Student will learn the concept of class
and object
7. In this experiment you will perform specific functions.
8. In this experiment, user will learn the concepts of
Constructor and Inheritance in Python programming
language. To implement those concepts in solving a
simple problem in the simulator
9. In this experiment user will understand file operations.

Hiren pandav [215690694006]


Virtual Lab

Practical:-1-Arithmetic
Operations

Theory:-
An operator is a symbol that tells the compiler that either a mathematical or
a logical manipulation has to be done. In this lab you will be studying about
the Arithmetic Operations.
They are of the following types :

Addition Operator ( + )
The addition operator is used to add two numbers. It is placed between two
numbers that are to be added.
Syntax : number1 + number2

Program:

Output:
x+y=5

Subtraction Operator ( - )
The subtraction operator is used to subtract two numbers. It is placed
between two numbers that are to be subtracted. The right placed number is
subtracted from the one that is placed at left.
Syntax : number1 - number2
Program:

Output:
x–y=1
Multiplication Operator ( * )
The multiplication operator is used to multiply two numbers. It is also
placed between the two numbers that are to be operated.

Hiren pandav [215690694006]


Virtual Lab

Syntax : number1 * number2

Program:

Output:
x * y = 300

Division Operator ( / )
The division operator is used to divide two numbers. It is used between the
numbers that are to be operated.
Syntax : number1 / number2
It has some different rules that have to be kept in mind before operating the
numbers. Python2 operates the division operator by taking the integral
value.
Example : 6 / 4
Answer : This operation will be solved in Python2 by taking the integral
value i.e 1. Therefore, the answer of 6 / 4 = 1
This problem can be taken care by Type Casting. Type Casting is used to
convert the output in a desired form.
To get the correct answer of the above example, we will type cast it using
float data type.

Program:

Output:
x/y = 3.0

Example : float(6 / 4)
Answer : Now, the output will be changed into float type and the answer will
be 1.5.
float(6 / 4) = 1.5
There's another way of solving such problem. By using one float type input,
we can get the desired answer.
Example : 6.0 / 4
Answer : 1.5

Hiren pandav [215690694006]


Virtual Lab

Modulus Operation ( % )
It is used to give out the remainder of a division operation. It is also placed
between numbers. The right placed number divides the one on the left and
the remainder is given as output.
Syntax : number1 % number2

Program:

Output:
x%y=2

Exponent Operation ( ** )
It is used to perform exponential calculations. The right placed number acts
as the power.
Syntax : number1* *number2

Program:

Output:
X**y=8

Floor Division Operator ( // )


It is used to perform floor division. This gives the result in int format.
Syntax : number1 // number2

Program:

Output:

X // y = 0

45 / 9 will give 5.0 where as 45 // 9 will give 5

Hiren pandav [215690694006]


Virtual Lab

Pretest:-

Hiren pandav [215690694006]


Virtual Lab

Simulation:-

Hiren pandav [215690694006]


Virtual Lab

Hiren pandav [215690694006]


Virtual Lab

Posttest:-

Refrences:-

www.learnpython.org/en/Basic_Operators

Hiren pandav [215690694006]


Virtual Lab

Practical:-2. Built-in Function

Theory:-
An executable program in a programming language contains multiple
lines. To simplify this code, various functions are used. They can be built-
in functions or user defined functions.A function is basically a chunk or
module of code that takes in some input from the user and may or may
not give any output. The function may provide some alterations to the
input values.

'id' function :
This function returns the identity of an object. A identity has to be unique
and constant for a particular object during the lifetime.
Syntax : id(object)

Program:

Output

1750812191152

Program:

Output:

False

Hiren pandav [215690694006]


Virtual Lab

'Type' function :
This function returns the data type of an object. It returns the following
data types :
i. Integer
ii. String
iii. Float
type() method returns class type of the argument(object) passed as
parameter. type() function is mostly used for debugging purposes.

Syntax : type(object)

Program:

Output:

<class 'dict'>

Program:

Hiren pandav [215690694006]


Virtual Lab

Output:

<class 'float'>
<class 'str'>
<class 'int'>

Pretest:-

Hiren pandav [215690694006]


Virtual Lab

Simulation:-

Hiren pandav [215690694006]


Virtual Lab

Posttest:-

Refrences:-
www.programiz.com/python-programming/methods/built-in

Hiren pandav [215690694006]


Virtual Lab

Practical:-3-LOOPS

Theory:-
The execution of programming language codes is done by a compiler. A
compiler is given a set of codes or rather a sequence of codes that perform
a desired task. The task may or may not be repetitive but the compiler is
smart enough to process it. Such repetitive code is known as a 'loop'.

Loop is a sequential set of instructions which gets executed multiple times


to reduce minimize the repetition of code.In Python, we have two types of
loops:
i. for loop
ii. while loop
To understand the functioning and flow of a loop, you must get familiar with
the term 'block'. A block is the smallest unit in a loop which performs one
particular task.

'For' loop :

Syntax :

for object in range(initialization, limit, update ):


statements

The above given syntax is of for loop where we put the object name after
'for' and the limit inside 'range( )'.

Program:

Hiren pandav [215690694006]


Virtual Lab

Output :
0
1
2
3
4

'While' loop :

Syntax :

while expression:
statements

The above statement is for while loop, where the testing condition is placed
after while and it is followed by the statements placed in the loop body.
Program:

Output:

Hello people
Hello people
Hello people

Program:

Output:

Hiren pandav [215690694006]


Virtual Lab

hello geek
hello geek
..........

Nested loops :

A nested loop is an inner loop in the loop body of the outer loop. The inner
or outer loop can be any type, such as a while loop or for loop. For
example, the outer for loop can contain a while loop and vice versa

Program:

Output:

1
22
333
4444

Program:

Output:

Hiren pandav [215690694006]


Virtual Lab

1 Outer loop is executed only once


1 Inner loop is executed until to completion
2 Inner loop is executed until to completion
2 Outer loop is executed only once
1 Inner loop is executed until to completion
2 Inner loop is executed until to completion

Pretest:-

Hiren pandav [215690694006]


Virtual Lab

Simulation:-

Hiren pandav [215690694006]


Virtual Lab

Hiren pandav [215690694006]


Virtual Lab

Posttest:-

Refrences:-
www.learnpython.org/en/Loops

Hiren pandav [215690694006]


Virtual Lab

Practical:- 4 - DATA TYPES

Data Types
Before starting with arrays you must know the concept of
mutability. An object may be classified into two categories :
i. Mutable
ii. Immutable

Mutable are those objects whose value can be altered after


assigning a particular value. Immutable are those objects whose
value can not be altered after assigning of a value.
List And Dictionary are Mutable.
Tuples are Immutable.

Data Type Character


i.List Mutable
ii.Tuple Immutable
iii.Dictionary Mutable

LIST
It is a collection of data in which date is stored in ordered form.
Syntax : object_name = [ elements ]
The above mentioned syntax is of list where the elements are
mentioned in square brackets.

Hiren pandav [215690694006]


Virtual Lab

Program:

Output:

['apple', 'banana', 'cherry']

Tuple
A tuple is a collection of data which is mostly similar to a list
except that it is immutable i.e. the value once stored in a tuple
cannot be changed. Though tuple allows concatenation. Syntax :
object_name = ( elements )
This is the syntax of a tuple, where elements are placed under
parenthesis.

Program:

Output:

('apple', 'banana', 'cherry')

Tuples allow duplicate values:

Hiren pandav [215690694006]


Virtual Lab

Program:

Output:

('apple', 'banana', 'cherry','apple')

Dictionary
Dictionary is a collection of data which is not sequential like a list,
thus indexing is not possible. It consists of a key and its value.
Example : dict1 = {1 : "Red", 2 : "Blue", 3 : "Green"}
The above syntax is for a dictionary where the elements are
placed under curly brackets.

Program:

Output:

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}


Duplicates are not allowed in dictionary.

Hiren pandav [215690694006]


Virtual Lab

Program:

Output:

{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}


Duplicate values will overwrite existing values

Pretest: -

Hiren pandav [215690694006]


Virtual Lab

Simulation:-

Hiren pandav [215690694006]


Virtual Lab

Hiren pandav [215690694006]


Virtual Lab

Posttest:-

Refrences:-
www.learnpython.org/en/Lists
www.learnpython.org/en/Dictionaries

Hiren pandav [215690694006]


Virtual Lab

Practical:- 5-STRINGS

String
We can access a string using indexing. In a string each character
is assigned with a unique index value which starts from 0. A string
can be written in both single quotes and double quotes. Example :
'Hello World'
"Hello World"

Program:

Output:

Hello
Hello

Program

Output

Hello

Program

Hiren pandav [215690694006]


Virtual Lab

Output

Lorem ipsum dolor sit amet,


consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.

Python allows negative indexing as well.


Example : -1, -3, -5.
Where -1 refers to the last index, -2 refers to second last index
and so on.
Printing here can be done by placing the string in single or double
quotes after print.

print("Hello World")

Concatenation
A string in python is immutable i.e. it can not be changed once
defined. But concatenation is still possible

Program

Hiren pandav [215690694006]


Virtual Lab

Output

Hello WorldExample

Using Join() method also we can concatenate string

Program

Output

HelloWorld

Program

Output

HelloWorld

Hiren pandav [215690694006]


Virtual Lab

Repetition
This is a unique property of strings in Python programming
language. When a string is multiplied with an integer, the
characters of string are multiplied the same number of times.

Syntax : string * integer

Program

Solution

kkkkk

Slicing
Slicing is done in Python to select or display the desired number
of characters in a string. It is done with the help of symbol ':'

Syntax : String[ index: ]

Program

Hiren pandav [215690694006]


Virtual Lab

Solution

llo World

All the characters from and after second index is selected

Program

Solution

ello Wo

Characters between index number 1 and 8 are selected

Length of a string can be calculated using the len function.

Syntax : len("string")
len("Hello World"
It will return 11.

Deleting / updating from a String:

In Python, Updation or deletion of characters from a String is not


allowed. This will cause an error because item assignment or item
deletion from a String is not supported

Hiren pandav [215690694006]


Virtual Lab

Program

Output

Initial String:
Hello, I'm a coder
Traceback (most recent call last):
File "e:\VLAB\python\programs\a.py", line 51, in
String1[2] = 'p'
TypeError: 'str' object does not support item assignment

Hiren pandav [215690694006]


Virtual Lab

Pretest: -

Hiren pandav [215690694006]


Virtual Lab

Simulation:-

Refrences:-
www.learnpython.org/en/Basic_String_Operations

Hiren pandav [215690694006]


Virtual Lab

Posttest:-

Hiren pandav [215690694006]


Virtual Lab

Practical:-6-CLASSES AND
OBJECT

Theory:-
A class is a user-defined blueprint or prototype from which objects are created.
Classes provide a means of bundling data and functionality together. Creating a
new class creates a new type of object, allowing new instances of that type to be
made

Syntax:
class class_name:
statements
statements

How to define class objects?


An Object is an instance of a Class. A class is like a blueprint while an instance is a
copy of the class with actual values.
An object consists of :

State:
It is represented by the attributes of an object. It also reflects the properties of an
object.
Behavior: It is represented by the methods of an object.

It also reflects the response of an object to other objects.

Hiren pandav [215690694006]


Virtual Lab

Identity:
It gives a unique name to an object and enables one object to interact with other
objects

Syntax:
Object_name.class_name()

Program

Output:
graduate
I'm a graduate
I'm a student

The self method


Class methods must have an extra first parameter in the method definition. We do
not give a value for this parameter when we call the method, Python provides it.

Hiren pandav [215690694006]


Virtual Lab

If we have a method that takes no arguments, then we still have to have one
argument.
When we call a method of this object as myobject.method(arg1, arg2), this is
automatically converted by Python into MyClass.method(myobject, arg1, arg2)

init method

The init method is similar to constructors in C++ and Java. Constructors are used
to initializing the object’s state. It runs as soon as an object of a class is
instantiated. The method is useful to do any initialization you want to do with your
object.

Program

Output

Hello, my name is John

Class and Instance Variables:

Class variables is a variable that is shared by all instances of a class. They are
defined within a class but outside any of the class's methods.

Instance Variable is a variable that is defined inside a method and belongs only to
the current instance of a class.

Program

Hiren pandav [215690694006]


Virtual Lab

OUTPUT:

myobj1 details:
myobj1 is a student
name: Harry
subject: Physics
Accessing class variable using class name
student

Hiren pandav [215690694006]


Virtual Lab

Pretest:-

Hiren pandav [215690694006]


Virtual Lab

Simulation:-

Hiren pandav [215690694006]


Virtual Lab

Posttest:-

Refrences:-

www.learnpython.org/Classes_and_Objects

Hiren pandav [215690694006]


Virtual Lab

Practical:-7-Built-In-Modules

Theory:-
To perform specific like functions add, subtract, power, square root, we need
to write two three lines of logic to make our task or function work properly.
So we have built in functions inside those modules which can straight away
perform those task by passing the values to calculate. Here you will learn
about some of the built-in modules used in Python Programming Language.

We simply have to import those built in modules present inside the program
then we can easily perform those specific task after calling those functions.
Some of the modules that are used in Python are : math, datetime, decimal,
operator, test, user, sets, etc.

Program:

Output :

4.0

The above given example is of math module. Here math module is


imported, with the help of which, complex mathematical calculations can be
performed.

The import statement


We can use any Python source file as a module by executing an import
statement in some other Python source file. When the interpreter
encounters an import statement, it imports the module if the module is

Hiren pandav [215690694006]


Virtual Lab

present in the search path. For example, to import the module calculate.py,
we need to put the following command at the top of the script :

Program:

Output :
12

Hiren pandav [215690694006]


Virtual Lab

Program:

Output :

5.0
3.141592653589793
114.59155902616465
1.0471975511965976
0.9092974268256817
24

Hiren pandav [215690694006]


Virtual Lab

Pretest:-

Hiren pandav [215690694006]


Virtual Lab

Simulation:-

Hiren pandav [215690694006]


Virtual Lab

Hiren pandav [215690694006]


Virtual Lab

Posttest:-

Refrences:-
www.learnpython.org/en/Modules_and_Package

Hiren pandav [215690694006]


Virtual Lab

Practical:-8-Constructor and
Inheritance

Theory:-
Inheritance is a feature that says if you define a new class giving a reference of
some other class then due to inheriting property of python your new class will
inherit all attributes and behavior of the parent class. A Constructor is a special
kind of method that have same name as the class in python self variable do the
same. It can be used to set the values of the members of an object.

How to define constructor:

Syntax:

Types of constructors :
Default constructor: The default constructor is a simple constructor which
doesn’t accept any arguments. Its definition has only one argument which is a
reference to the instance being constructed.
Parameterized constructor: constructor with parameters is known as
parameterized constructor. The parameterized constructor takes its first
argument as a reference to the instance being constructed known as self and the
rest of the arguments are provided by the programmer.

Hiren pandav [215690694006]


Virtual Lab

Program

Output

Sharukh

Program

Hiren pandav [215690694006]


Virtual Lab

Output

1000
2000

How to call objects using constructors:

Syntax:
Object_name=class_name('variable values',variable values)

Program

Output
name = Harry
subject = chemistry

How to inherit a class:


Syntax:

class class_name(parent class name)

Hiren pandav [215690694006]


Virtual Lab

How to check whether it is inherited or not:

Syntax:

Object_name = inherited_class_name('variable1,variable2')
print(Object_name.variable1)
Output: variable value.

Program

Output
Geek1 False
Geek2 True

Hiren pandav [215690694006]


Virtual Lab

Pretest:-

Hiren pandav [215690694006]


Virtual Lab

Simulation:-

Hiren pandav [215690694006]


Virtual Lab

Posttest:-

Refrences:-
www.python-course.eu/python3_inheritance.php
interactivepython.org/runestone/static/CS152f17/ClassesBasics/UserDe
finedClasses.html

Practical:-9-File Operation

Hiren pandav [215690694006]


Virtual Lab

Program:

Output:

Hello!, My name is Jake.

Hiren pandav [215690694006]


Virtual Lab

Pretest:-

Hiren pandav [215690694006]


Virtual Lab

Simalution:-

Hiren pandav [215690694006]


Virtual Lab

Posttest:-

Refrences:-

en.m.wikibooks.org/wiki/Python_Programming/Files

Hiren pandav [215690694006]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy