0% found this document useful (0 votes)
82 views16 pages

Python Practical

Practical 1: The document discusses installing Python on your system in 3 steps: 1) Download the Python Installer, 2) Run the Installer, 3) Add Python to environment variables and verify the installation. Practical 2: The document discusses various operators in Python like arithmetic, comparison, assignment, logical and bitwise operators giving examples of each. Practical 3: The aim is to write a program to study conditional statements in Python. The program compares 3 numbers and prints which number is greatest using if-else and nested if statements.

Uploaded by

himanshi Garg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
82 views16 pages

Python Practical

Practical 1: The document discusses installing Python on your system in 3 steps: 1) Download the Python Installer, 2) Run the Installer, 3) Add Python to environment variables and verify the installation. Practical 2: The document discusses various operators in Python like arithmetic, comparison, assignment, logical and bitwise operators giving examples of each. Practical 3: The aim is to write a program to study conditional statements in Python. The program compares 3 numbers and prints which number is greatest using if-else and nested if statements.

Uploaded by

himanshi Garg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

Practical 1

Aim: - Steps installation of Python on your system.


What is a Python:-Python is an interpreted, object-oriented, high-level programming
language with dynamic semantics. Its high-level built in data structures, combined
with dynamic typing and dynamic binding, make it very attractive for Rapid
Application Development, as well as for use as a scripting or glue language to
connect existing components together.
Steps: - There are several built steps in Python.
• Step 1: Download the Python Installer binaries. Open the official Python website
in your web browser. ...
• Step 2: Run the Executable Installer. Once the installer is downloaded, run the
Python installer. ...
• Step 3: Add Python to environmental variables. ...
• Step 4: Verify the Python Installation.

Practical 2

Aim: - Concept usage of various operators available in python.


These are the special symbols in python and are used to execute an Arithmetic or
Logical computation. An operator alone cannot perform an activity, it needs an
Operand. What is an operand? An Operand is a value that the operator needs to
complete a task.

Types of operators in Python:


We have multiple operators in Python, and each operator is subdivided into other
operators. Let’s list them down and know about each operator in detail.
• Arithmetic operators
• Comparison operators
• Assignment operators
• Logical operators
• Bitwise operators
Arithmetic operators
• 1) Python (+) Addition operator
• Addition operator in python is being used to add two operands (values) in
python programming. Look at the below program to know how it works.
• Program
• X = 20
• Y = 30

• # output: x + y = 50

• print ( ‘x + y =’, x + y )

• When you execute the above program, you will get the below output.
• Run
• x + y = 50

2) Python (-) Subtraction operator


In python programing, the operator, Subtraction is used to execute a mathematical
function which subtracts the right hand operand from the left hand one.
Program:
x = 20

y = 30

# output: x - y = - 10

print (‘x - y =’, x - y )

Run
x - y = - 10

3) Python (*) Multiplication operator


It is used in python programming to multiply the values and thereby to get output. Let's
consider the below program to understand the multiplication operator.
Program
x = 15

y = 10

# output: x * y = 150

print (‘x * y =’, x * y )

Run
x * y = 150

4) Python (/) Division operator


Division operator is used to divide the left hand operand by right hand one.
x = 40
y = 50
i.e x/y (10/20 = 0.8 )
Let’s consider it in python way
Program
x = 40

y = 50

# output: x / y = 0.8

print (‘x / y =’, x / y )

Run
x / y = 0.8

5) Python (%) Percentail operator


The Percentile (%) is the string formatting operator in python, and it is also called as a
interpretation operator. It divides the left hand operand by right hand one and also
returns reminder. Ex: x/y
Program
x = 30

y = 10

# output: x % y = 0.

print (‘x % y =’, x % y )

Run
x % y = 0

6)Python (//) Floor division operator


The Floor Operator is used to make a whole number adjusted to the left hand side in
the number line.
Let's check the below program to know how a floor operator works in Python
Program
x = 100

y = 40

# Output: x // y = 2
print('x // y =',x//y)

Run
x // y = 2

Python Comparison Operators


1) Python (==) Equal Comparison Operator
Equal operator is used to compare the two values and to give the result whether they
are equal or not. If both the values are equal, then it gives the result as True, if not
False.
Let's consider below program.
Program
x = 30

y = 95

# Output: x == y is False

print('x == y is',x==y)

Run:
x == y is False

2) Python (!=) Not Equal Comparison Operator


This Not equal operator is used to compare two operands and returns True statement
if they are Not equal, and False if they are Equal.
Program
x = 60

y = 85

# Output: x != y is True

print('x != y is',x!=y)

Run:
x != y is true

equal operator is exclusively used to return the True statement if the values are not
equal.
3) Python (>) Greater Than Operator
This operator is mainly used in Python to return true if the Left hand value is greater
than right one. If left hand operand is greater than right hand operand, it returns True
as the output.
Ex: 12 is > 8, then the answer becomes True
07 is > 12, then the answer becomes False
Program:
ex :
x = 50

y = 25

# Output: x > y is True

print('x > y is',x>y)

Run:
x > y is True

4) Python (<) Less Than Operator


This Operator compares the right hand operator with the left hand one and if the right
hand one is greater than the left hand one, it returns True statement.
Ex: 10 is < 12, then the answer becomes True
10 is < 8, then the answer becomes False
Program:
x = 20

y = 75

# Output: x < y is True

print('x < y is',x<y)

Run:
x < y is True

5) Python >= Greater Than or Equal to operator


Greater than operator is used in python to evaluate to True if the left hand operand is
>= (greater than or equal) to right hand one
Program:
x = 16
y = 30

# Output: x >= y is False

print('x >= y is',x>=y)

Run:
x >= y. Hence it is False

6) Python <= Less than or equal to operator


Less than or equal to operator returns true if the right hand operator is greater than or
equal to left hand one.
Program:
x = 50

y = 100

# Output: x <= y is True

print('x <= y is',x<=y)

Run:
x <=y is True

Python Assignment Operators


1) Python (=) Assign value Operator
This operator assigns values to the left hand operand from the right side operand.
Example: 4 = 1+3
In the above example, the right hand operands are 1 & 3, and have contributed to
construct value 4.
2) Python (+=) Add AND Operator
It adds the value of right operand to the value of left hand operand.
Let's consider an example; x = 12, x + = 5, which will return the value as 17 by adding
the right side value to the left hand one. Therefore, the answer is x= 17.
3) Python(-=) Subtract AND Operator
Subtract And operator (-=) subtracts the value of left operand from the right side
operand and returns the output.

x = 100, y -= 70, the answer is x = 30

 
4) Python (*=) Multiply AND Operator
It multiplies the left hand operand with the right hand one and returns the value as
output.
Example; 
x = 20, x*=5, the answer is x = 100.

5) Python (/=) Divide AND Operator


Divide And operator divides the left hand operand with the right hand operand value
and gives the result
Example; 
x = 15, x/= 5 the result is x = 3

6) Python (%=) Modulus AND Operator


It divides the value of left hand operand with the right hand one and the reminder that
we get through this task will be placed as a value of x.
Example; 
x = 100, x%= 2 the result is x = 0

7) Python **= Exponent AND operator


It gives us the exponential value of the left hand operand when raised to the power of
the value found in the right hand side.
Example; 
x = 10, x**= 7 the result is x = 1,00,00,000

8) Python (//=) Floor Division


Floor division divides the value found in the left hand side with the value of right hand
operand. The integer value becomes the left hand Operand value.
Example; 
x = 8, x//= 4 the result is x = 2

Python logical Operators


1) Python and Logical AND operator
AND operator returns the logical value ‘True’ when two Operands are true; otherwise, it
will return False.
Ex:  variable A = 10, B = 20,
x = 50

y = 20

# Output: x and y is True

print('x and y is',x and y)

Output:
Ans: A and B is True

2) Python or Logical OR operator


In this Logical OR operator, if any one of the two operands are non zero, then it returns
the True condition. .
Ex: variable A = 10, B = 20,
Program
x = 90

y = 20

# Output: x or y is True

print('x or y is',x or y)

Output:
Ans: a or b is True

3) Python not Logical NOT


Logical NOT is used to reverse the logical state of its operands.
Program
x = 10

y = 20

# Output: not x is False

print('not x is',not x)

Output:
Ans: Not (a and b) is false.
Python Bitwise operators
1) Python & Binary AND Bitwise Operator
Description: This operator performs bit by bit AND operations on the values on either
side of the operator.
Example Program
a = 15

b = 3

# Print bitwise AND operation

print(a & b)

Output: 3
2) Python | Binary OR Bitwise Operator
Description: This operator performs bit by bit OR operations on the values on either
side of the operator.
Example Program
a = 08

b = 5

# Print bitwise OR operation

print(a | b)

Output: 13
3) Python ^ Binary XOR Bitwise Operator
Description: This operator performs bit by bit exclusive OR operations on the values
on either side of the operator.
Example Program
a = 15

b = 6

# print bitwise XOR operation

print(a ^ b)

Output: 09
4) Python ~ Binary Ones Complement Bitwise Operator
Description: The one’s complement of a number’s binary is returned.
Example Program:
a = 10

b = 4

# Print bitwise NOT operation

print(~a)

Output: -11

5) Python << Binary Left shift Bitwise Operator


Description: The value to the left operator is shifted to the left as many times as the
value on the right side of the operator.
Example Program
a = 10

b = 4

# print bitwise left shift operation

print(a << 2)

Output: 40
6) Python >> Binary Right shift Bitwise Operator
Description: The value to the left of the operator is shifted to the right as many times as
the value on the right of the operator.

a = 10

b = 4

# print bitwise right shift operation

print(a >> 2)

Output: 2

Practical : 3
Aim : Write a program to study conditional statements in python.
Program 3 :
num1=10
num2=20
num3=30
if(num1>num2):
if(num1>num3):
print("num1 is greater")
else:
print("num3 is greater")
elif(num2>num3):
print("num2 is greater")
else:
print("num3 is greater")

Output : num3 is greater


Practical : 4
Aim : Write a program to understand different types of loops in Python.
Program 4.1 :
num=5
count=1
while count <= num:
print("the current number is:",count)

Output : the current number is: 1

Program 4.2 :

for number in 1,2,3,4,5:


print("The current number is ",number)
count+=1

Output :
The current number is 1
The current number is 2
The current number is 3
The current number is 4
The current number is 5

Program 4.3 :
for number in range(1,5):
print ("The current number is ",number)
print("---------------------------")
for number in range(1,7,2):
print ("The current number is ",number)
print("---------------------------")
for number in range(5,0,-1):
print ("The current number is ",number)

Output :
The current number is 1
The current number is 2
The current number is 3
The current number is 4
---------------------------
The current number is 1
The current number is 3
The current number is 5
---------------------------
The current number is 5
The current number is 4
The current number is 3
The current number is 2
The current number is 1

Practical :5
Aim : Write a program to understand the concept of functions in python.

Program 5.1 :
def my_function():
print("Its a Python function")

my_function() // calling a function

Output : Its a Python function

Program 5.2 :
def my_function(fname):
print(fname + " MBA")

my_function("Trisha") // calling by argument


my_function("Himanshi")
my_function("Mukul")

Output :
Trisha MBA
Himanshi MBA
Mukul MBA
Practical :6
Aim : Write a program to perform different operations on strings in python.

Program 6.1 :
txt = "The best things in life are free!"
if "expensive" not in txt: // if operator
print("No, 'expensive' is NOT present.")

Output : No, 'expensive' is NOT present.

Program 6.2 :
b = "Himanshi"
print(b[2:5]) //slicing

Output :
man

Program 6.3 :
a = "Pyhton"
b = "2.0"
c=a+b
print(c)

Output : Python2.0

Practical : 7

Aim : Write a program to create a list and apply different operations to that list.
Program 7 :
list_of_airlines=["AL1","AL2","AL3"]
print("Iterating the list using range()")
for index in range(0,len(list_of_airlines)):
print(list_of_airlines[index])
print("Iterating the list using keyword in")
for airline in list_of_airlines:
print(airline)

Output : Iterating the list using range()


AL1
AL2
AL3
Iterating the list using keyword in
AL1
AL2
AL3

Practical : 8

Aim : Write a program to create a tuple and apply different operations to that tuple. Also, show
that tuples are immutable.

Program 8.1 :
thistuple = ("apple", "banana", "cherry") // creating a tuple
print(thistuple)

Output : ('apple', 'banana', 'cherry')


Program 8.2 :
thistuple = ("apple", "banana", "cherry")
print(len(thistuple)) // operator

Output : 3

Program 8.3 :
tuple1 = (1, 2, 33, 44, 6)

tuple1[4] = 10

Output :
TypeError: 'tuple' object does not support item assignment

You might also like