python lab
python lab
# Python program to
a=5
b = 5.0
c = 2 + 4j
Output:
Program2:- a program to compute distance between two points taking input from the user Write a
program add.py that takes 2 numbers as command line arguments and prints its sum.
Solution:-
Distance can be calculated using the two points (x1, y1) and (x2, y2), the
for e.g : let x1 , y1=10,9 and x2 , y2=4,1 then (x2-x1)2=(10-4)2 = 62 = 36 and (y2-y1)2= (9-1)2 = 82 = 64 now
64 + 36 =100 and 100 is square root of 10 sp distance between (10,9) and (4,1) is 10
Code:-
x1=int(input("enter x1 : "))
x2=int(input("enter x2 : "))
y1=int(input("enter y1 : "))
y2=int(input("enter y2 : "))
output
enter x1 : 10
enter x2 : 4
enter y1 : 9
enter y2 : 1
Program-3 Write a Program for checking whether the given number is an even number or not.
Solution:
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
output:
Enter a number: 3
3 is Odd
Program 4:-Write a Program to demonstrate list and tuple in python. Write a program using a for loop
that loops over a sequence. Write a program using a while loop that asks the user for a number, and
Solution:-
# creating a list
list1 = [1, 2, 5, 6]
print(res)
Output:
Write a program using a while loop that asks the user for a number, and prints a countdown from that
number to zero.
n=int(input("Enter n value:"))
for i in range(n,-1,-1):
print(i)
output
Enter n value:3
1
0
Program 5:-Find the sum of all the primes below two million. By considering the terms in the Fibonacci
sequence whose values do not exceed four million, WAP to find the sum of the even-valued terms.
number = numbers[pos]
pos += 1
num = number
num += number
if num in numbers[:]:
print sum_of_primes + 2
Program 6:-Write a program to count the numbers of characters in the string and store them in a
dictionary data structure Write a program to use split and join methods in the string and trace a
Code:-
f = {}
for i in str:
if i in f:
f[i] += 1
else:
f[i] = 1
print(f)
output
{'i': 1, ' ': 2, 'a': 1, 'm': 1, 'c': 1, 'o': 1, 'd': 1, 'e': 1, 'r': 1}
Write a program to use split and join methods in the string and trace a birthday of a person with a
split
Str.split()
join
Str1.join(str2)
Algorithm
Step 2: here we use split method for splitting and for joining use
join function.
Example code
#split of string
position 2
Output
['python', 'program']
['python', 'program']
['python', 'program']
['python', 'program']
Example Code
#string joining
Program7:-Write a program to count frequency of characters in a given file. Can you use character
frequency to tell whether the given file is a Python program file, C program file or a text file? Write a
program to count frequency of characters in a given file. Can you use character frequency to tell
whether the given file is a Python program file, C program file or a text file?
f = {}
for i in str:
if i in f:
f[i] += 1
else:
f[i] = 1
print(f)
Here is source code of the Python Program to count the occurrences of a letter in a text file.
k=0
for line in f:
words = line.split()
for i in words:
for letter in i:
if(letter==l):
k=k+1
print(k)
Program Explanation
2. The file is opened using the open() function in the read mode.
5. A for loop is used to traverse through the words list and another for loop is used to
6. If the letter provided by the user and the letter encountered over iteration are equal, the
Program 8 :-Write a program to print each line of a file in reverse order. Write a program to compute
Given a text file. The task is to reverse as well as stores the content from an input file to
an output file.
• Full reversing: In this type of reversing all the content get reversed.
• Word to word reversing: In this kind of reversing the last word comes first and
• ext file:
• filter_none
• brightness_4
f1 = open("output1.txt", "w")
data = myfile.read()
# the string
data_1 = data[::-1]
# following command
f1.write(data_1)
f1.close()
Output:
Write a program to compute the number of characters, words and lines in a file.
import sys
fname = sys.argv[1]
lines = 0
words = 0
letters = 0
lines += 1
letters += len(line)
pos = 'out'
words += 1
pos = 'in'97i’
pos = 'out'
print("Lines:", lines)
print("Words:", words)
print("Letters:", letters)
# using GCD
import sys
# Function to return
lar = max(a, b)
small = min(a, b)
i = lar
while(1) :
if (i % small == 0):
return i
i += lar
# Driver Code
a=5
b=7
b , " is " ,
Output:
LCM of 15 and 20 is 60
def gcd(a,b):
# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a
# base case
if (a == b):
return a
# a is greater
if (a > b):
return gcd(a-b, b)
a = 98
b = 56
if(gcd(a, b)):
else:
print('not found')
Output:
GCD of 98 and 56 is 14
Program 10 :-Write a program to implement Merge sort. Write a program to implement Selection
#ubarrays of arr[].
# First subarray is arr[l..m]
n1 = m - l + 1
n2 = r- m
L = [0] * (n1)
R = [0] * (n2)
L[i] = arr[l + i]
R[j] = arr[m + 1 + j]
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
# Copy the remaining elements of L[], if there
# are any
arr[k] = L[i]
i += 1
k += 1
# are any
arr[k] = R[j]
j += 1
k += 1
def mergeSort(arr,l,r):
if l < r:
# large l and h
m = (l+(r-1))//2
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
for i in range(n):
mergeSort(arr,0,n-1)
for i in range(n):
Output:
Given array is
12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13
# Sort
import sys
for i in range(len(A)):
# unsorted array
min_idx = i
min_idx = j
for i in range(len(A)):
print("%d" %A[i]),
def insertionSort(arr):
key = arr[i]
j = i-1
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
insertionSort(arr)
for i in range(len(arr)):
Output:
5 6 11 12 13