Iit Python Virtual Lab
Iit Python Virtual Lab
Iit Python 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.
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
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
Program:
Output:
X // y = 0
Pretest:-
Simulation:-
Posttest:-
Refrences:-
www.learnpython.org/en/Basic_Operators
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
'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:
Output:
<class 'float'>
<class 'str'>
<class 'int'>
Pretest:-
Simulation:-
Posttest:-
Refrences:-
www.programiz.com/python-programming/methods/built-in
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'.
'For' loop :
Syntax :
The above given syntax is of for loop where we put the object name after
'for' and the limit inside 'range( )'.
Program:
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:
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:
Pretest:-
Simulation:-
Posttest:-
Refrences:-
www.learnpython.org/en/Loops
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
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.
Program:
Output:
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:
Program:
Output:
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:
Program:
Output:
Pretest: -
Simulation:-
Posttest:-
Refrences:-
www.learnpython.org/en/Lists
www.learnpython.org/en/Dictionaries
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
Output
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
Output
Hello WorldExample
Program
Output
HelloWorld
Program
Output
HelloWorld
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.
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 ':'
Program
Solution
llo World
Program
Solution
ello Wo
Syntax : len("string")
len("Hello World"
It will return 11.
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
Pretest: -
Simulation:-
Refrences:-
www.learnpython.org/en/Basic_String_Operations
Posttest:-
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
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.
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
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
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
OUTPUT:
myobj1 details:
myobj1 is a student
name: Harry
subject: Physics
Accessing class variable using class name
student
Pretest:-
Simulation:-
Posttest:-
Refrences:-
www.learnpython.org/Classes_and_Objects
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
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
Program:
Output :
5.0
3.141592653589793
114.59155902616465
1.0471975511965976
0.9092974268256817
24
Pretest:-
Simulation:-
Posttest:-
Refrences:-
www.learnpython.org/en/Modules_and_Package
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.
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.
Program
Output
Sharukh
Program
Output
1000
2000
Syntax:
Object_name=class_name('variable values',variable values)
Program
Output
name = Harry
subject = chemistry
Syntax:
Object_name = inherited_class_name('variable1,variable2')
print(Object_name.variable1)
Output: variable value.
Program
Output
Geek1 False
Geek2 True
Pretest:-
Simulation:-
Posttest:-
Refrences:-
www.python-course.eu/python3_inheritance.php
interactivepython.org/runestone/static/CS152f17/ClassesBasics/UserDe
finedClasses.html
Practical:-9-File Operation
Program:
Output:
Pretest:-
Simalution:-
Posttest:-
Refrences:-
en.m.wikibooks.org/wiki/Python_Programming/Files
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: