0% found this document useful (0 votes)
47 views

Exp1 and 2 Python

The document outlines experiments conducted for a Python programming lab course, including taking input, performing arithmetic operations, solving quadratic equations, and implementing various operations on lists, tuples, sets, and dictionaries; it provides the code implementations and expected output for each experiment.

Uploaded by

oko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Exp1 and 2 Python

The document outlines experiments conducted for a Python programming lab course, including taking input, performing arithmetic operations, solving quadratic equations, and implementing various operations on lists, tuples, sets, and dictionaries; it provides the code implementations and expected output for each experiment.

Uploaded by

oko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DEPARTMENT OF COMPUTER ENGINEERING

ACADEMIC YEAR 2022-2023 (Even)

Course Name : Skill Base Lab Course: Python


Programming

Course Code : CSL405

Experiment No : ____
AIM :
_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

LO Covered :

_______________________________________________________________________

NAME OF THE STUDENT

CLASS / SEM. / DIV. /

BATCH

DATE OF SUBMISSION

GRADE :

FACULTY SIGNATURE :
Page |1

EXPERIMENT NO: 1
a) Take two numbers as input(float) and print addition.

# Store input numbers


num1 = float(input("Enter the first number :"))
num2 = float(input("Enter the second number :"))
# Add two numbers
sum = num1 + num2
# Display the sum
print("sum of {0} and {1} is {2}" .format(num1, num2, sum))

OUTPUT:

b) To swap two numbers

# To take inputs from the user

print("Enter first number : ")

num1=int(input())

print("Enter second number : ")

num2=int(input())

print ("Before swapping: ")

print("Value of num1 : ", num1, " and num2 : ", num2)

# create a temporary variable and swap the values

temp=num1

num1=num2

num2=temp

print ("After swapping: ")

print("Value of num1 : ", num1, " and num2 : ", num2)

OUTPUT:
Page |2

c) Solve the quadratic equation ax**2 + bx + c = 0

import cmath

a=int(input("Enter the value of a : "))

b=int(input("Enter the value of b : "))

c=int(input("Enter the value of c : "))

# calculating the discriminant

"Discriminant, D = b2 – 4ac (FORMULA)"

dis=(b*b)-(4*a*c)

print(" the value of discriminant is : ",dis)

# find two results

root1 = (-b + cmath.sqrt(dis))/(2 * a)

root2 = (-b - cmath.sqrt(dis))/(2 * a)

print("The roots of the equation are : ",root1, root2)

OUTPUT:

d) Perform (at least 5) operations on each.

i) list

lst = [ ]

n = int(input("Enter number of elements : "))

for i in range(0, n):

ele = [input()]

lst.append(ele)

#Accessing an entire list

print(lst)

#getting the length of the list

print("The length of the list is ")

print(len(lst))

#repetition operator

print("Multiplying the list")

print (lst * 4);


Page |3

#Slicing a Python

print("Slicing of list ")

print(lst[2:n])

#concatenation of list

print("Concatenation of list ")

print (lst + lst)

#Appending element in the list

print("Appending element 12")

lst.append(12)

print(lst)

#Reversing the list

print ('Reverse printing list')

print(lst[::-1])

OUTPUT:

ii)tuple

t = ("hello", "python", 3, "Engineering")

t1=(1, "Bhupali", 12, "GIRL" )

# Values in Tuples

print("Value in t[0] = ", t[0])

print("Value in t[1] = ", t[1])

print("Value in t[2] = ", t[2])


Page |4

#Concatenation of Python Tuples

print("Concatenation of two tuple")

print(t + t1)

# Code to create a tuple with repetition

print("Multiplying the tuple")

print (t1 * 3);

#Slicing Python Tuples

print("Slicing tuple : ")

print(t[1:])

print(t[::-1])

print(t[2:4])

# code to test that tuples are immutable

t[0] = 4

print(t)

OUTPUT:

iii) set

A = {"java", "python", "html",12 , "Apple", 11}

B = {"Bhupali", 12, 33, "Buses"}

#adding element

A.add("css")

print(A)
Page |5

#removing element

A.remove("html")

print(A)

#the data type of a set

print(type(A))

# perform union operation using |

print('Union using |:', A | B)

# perform union operation using union()

print('Union using union():', A.union(B))

OUTPUT:

iv) dictionaries

capital_city = {"Nepal": "Kathmandu", "Italy": "Rome", "England": "London"}

print("Initial Dictionary ",capital_city)

#Add Elements to a Python Dictionary

capital_city["Japan"] = "Tokyo"

print("Updated Dictionary: ",capital_city)

#Removing elements from Dictionary

del capital_city["Nepal"]

print("Updated Dictionary : ", capital_city)

#Accessing Elements from Dictionary

print(capital_city["England"])

#Printing the keys

print (capital_city.keys());

#printing the values

print (capital_city.values());

OUTPUT:
Page |6

V) STRINGS:

str1 = "Hello, Myself Bhupali. "

str2 = "I love Python."

str3 = "I love Python."

# compare str1 and str2

print(str1 == str2)

# compare str1 and str3

print(str2 == str3)

#Join Two or More Strings

# using + operator

result = str1 + str2

print(result)

#printing 4th character of the string

print (str1[4])

#printing first two character using slice operator

print (str1[0:2])

OUTPUT:
Page |1

EXPERIMENT NO: 02

a) Smallest of 3 numbers (using nested if else).


num1 = int(input('Enter the first number : '))

num2 = int(input('Enter the second number : '))

num3 = int(input('Enter the third number : '))

smallest = 0

if num1 < num2 and num1 < num3 :

smallest = num1

elif num2 < num3 :

smallest = num2

else :

smallest = num3

print(smallest, "is the smallest of three numbers.")

OUTPUT:

b) To check if the input number is prime or not (using for loop)

# To take input from the user

num = int(input("Enter a number: "))

if num > 1:

for i in range(2,num):

if (num % i) == 0:

print(num,"is not a prime number")

print(i,"times",num//i,"is",num)

break

else:

print(num,"is a prime number")

# if input number is less than or equal to 1, it is not prime

else:

print(num,"is not a prime number")


Page |2

OUTPUT:

c) To check if number provided by the user is Armstrong number or not(using while loop)

num = int(input("Enter a number: "))

sum = 0

n1 = len(str(num))

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** n1

temp //= 10

if num == sum:

print(num,"is an Armstrong number")

else:

print(num,"is not an Armstrong number")

OUTPUT:
Page |3

d) Given a list of numbers (integers),


you have to print those Numbers which are not multiples of 5.
n=int(input("How many number you want to insert in a list"))
list=[ ]
for i in range(0,n):
a=int(input("enter element"))
list.append(a)
for i in(list):
if(i%5!=0):
print(i,"is not multiple of 5")
else:
print(i," is multiple of 5")
OUTPUT:

You might also like