Python Lab Practicals:
1. Write a Program to read and print values of variables of different data types.
a=10
b="Hi Python"
c = 10.5
print(type(a))
print(type(b))
print(type(c))
a = 5
print("The type of a", type(a))
b = 40.5
print("The type of b", type(b))
c = 1+3j
print("The type of c", type(c))
print(" c is a complex number", isinstance(1+3j,complex))
str = "string using double quotes"
print(str)
s = '''''A multiline
string'''
print(s)
str1 = 'hello javatpoint' #string str1
str2 = ' how are you' #string str2
print (str1[0:2]) #printing first two character using slice operator
print (str1[4]) #printing 4th character of the string
print (str1*2) #printing the string twice
print (str1 + str2) #printing the concatenation of str1 and str2
list1 = [1, "hi", "Python", 2]
#Checking type of given list
print(type(list1))
#Printing the list1
print (list1)
# List slicing
print (list1[3:])
# List slicing
print (list1[0:2])
# List Concatenation using + operator
print (list1 + list1)
# List repetation using * operator
print (list1 * 3)
tup = ("hi", "Python", 2)
# Checking type of tup
print (type(tup))
#Printing the tuple
print (tup)
# Tuple slicing
print (tup[1:])
print (tup[0:1])
# Tuple concatenation using + operator
print (tup + tup)
# Tuple repatation using * operator
print (tup * 3)
# Adding value to tup. It will throw an error.
t[2] = "hi"
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
# Printing dictionary
print (d)
# Accesing value using keys
print("1st name is "+d[1])
print("2nd name is "+ d[4])
print (d.keys())
print (d.values())
# Python program to check the boolean type
print(type(True))
print(type(False))
print(false)
# Creating Empty set
set1 = set()
set2 = {'James', 2, 3,'Python'}
#Printing Set value
print(set2)
# Adding element to the set
set2.add(10)
print(set2)
#Removing element from the set
set2.remove(2)
print(set2)
2. Write a program to perform addition, subtraction, multiplication, division and modulo division on two integers.
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
print("Enter which operation would you like to perform?")
ch = input("Enter any of these char for specific operation +,-,*,/: ")
result = 0
if ch == '+':
result = num1 + num2
elif ch == '-':
result = num1 - num2
elif ch == '*':
result = num1 * num2
elif ch == '/':
result = num1 / num2
else:
print("Input character is not recognized!")
print(num1, ch , num2, ":", result)
Output 1: Addition
Enter First Number: 100
Enter Second Number: 5
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: +
100 + 5 : 105
Output 2: Division
Enter First Number: 20
Enter Second Number: 5
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: /
20 / 5 : 4.0
Output 3: Subtraction
Enter First Number: 8
Enter Second Number: 7
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: -
8 - 7 : 1
Output 4: Multiplication
Enter First Number: 6
Enter Second Number: 8
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: *
6 * 8 : 48
3. Write a program to input two numbers and check whether they are equal or not.
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if(a >= b):
print(a, "is greater")
else:
print(b, "is greater")
4. Write a program that prompts user to enter a character (O, A, B, C, F). Then using if-elseif-else construct displays Outstanding, Very Good, Good, Average and Fail respectively.
# Python3 program to assign grades
# to a student using nested if-else
if __name__ == "__main__":
# Store marks of all the subjects
# in an array
marks = [25, 65, 46, 98, 78, 65 ]
# Max marks will be 100 * number
# of subjects
max_marks = len(marks)* 100
# Initialize student's total
# marks to 0
total = 0
# Initialize student's grade
# marks to F
grade = 'F'
# Traverse though the marks array
# to find the sum.
for i in range(len(marks)):
total += marks[i]
# Calculate the percentage.
# Since all the marks are integer,
percentage = ((total) /max_marks) * 100
# Nested if else
if (percentage >= 90):
grade = 'A'
else :
if (percentage >= 80 and
percentage <= 89) :
grade = 'B'
else :
if (percentage >= 60 and
percentage <= 79) :
grade = 'C'
else :
if (percentage >= 33 and
percentage <= 59) :
grade = 'D'
else:
grade = 'F'
print(grade)
5. Write a program to print Fibonacci series using recursion.
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
# take input from the user
nterms = int(input("How many terms? "))
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
6. Write a program that prints absolute value, square root and cube root of a number. (import math package).
# integer number
integer = -40
print('Absolute value of -40 is:', abs(integer))
# floating number
floating = -40.83
print('Absolute value of -40.83 is:', abs(floating))
import math # import math module
N = 25 # define the value to the variable N
result = math.sqrt(N) # use math.sqrt() function and pass the variable.
print(" Square root of 25 is :", result) # prints the square root of a given number
M = 625 # define the value
result = math.sqrt(M) # use math.sqrt() function and pass the variable
print(" Square root of 625 is :", result) # prints the square root of a given number
P = 144 # define the value
result = math.sqrt(P) # use math.sqrt() function and pass the variable
print(" Square root of 144 is :", result) # prints the square root of a given number
S = 64 # define the value
result = math.sqrt(S) # use math.sqrt() function and pass the variable
print(" Square root of 64 is :", result) # prints the square root of a given number
import math
number = int(input("Enter the number: "))
cuberoot = math.ceil(number ** (1/3))
print("The cube root is:",cuberoot
7. Write a program that finds the greatest of three given numbers using functions. Pass three arguments.
def maximum(a, b, c):
list = [a, b, c]
return max(list)
# Driven code
x = int(input("Enter First number"))
y = int(input("Enter Second number"))
z = int(input("Enter Third number"))
print("Maximum Number is ::>",maximum(x, y, z))
8. Write a program to get a string made of the first 2 and last 2 characters from a given string. If the string length is less than 2, return an empty string.
def string_both_ends(str):
if len(str) < 2:
return ''
return str[0:2] + str[-2:]
print(string_both_ends('w3resource'))
print(string_both_ends('w3'))
print(string_both_ends('w'))
9. Write a program that fetches data from a specified URL and writes it in a file.
import urllib.request
# open a connection to a URL using urllib
webUrl = urllib.request.urlopen('https://www.youtube.com/user/guru99com')
#get the result code and print it
print ("result code: " + str(webUrl.getcode()))
# read the data from the URL and print it
data = webUrl.read()
print (data)
10. Write a program to find the resolution of an image.
a=img_file.read(2)
height=(a[0]<<8)+a[i]
a=img_file.read(2)
width=(a[0]<<8)+a[1]
print("Image Resolution is :",width, "x", height)
find_res("C:\Python3.7.4\Icon.jpg")
No comments:
Post a Comment