PYTHON
PYTHON
The User
Program:
import math
a=int(input("enter a value:"))
b=int(input("enter b value:"))
c=int(input("enter c value:"))
d=int(input("enter d value:"))
distance = math.sqrt(((a-c)**2)+((b-d)**2))
print(" distance =",distance)
Output:
Result:
Program:
num1=float(input("please enter the first number 1:"))
num2=float(input("please enter the second number 2:"))
add=num1+num2
sub=num1-num2
multi=num1*num2
div=num1/num2
mod=num1%num2
expo=num1**num2
print(" sum=",add)
print("difference=",sub)
print("product=",multi)
print("quotient=",div)
print(" remainder=",mod)
print("exponential value=",expo)
Output:
Result:
Program:
#numbers
x=20
print(x)
print(type(x))
x=20.5
print(x)
print(type(x))
x=1j
print(x)
print(type(x))
#string
x="GoEduHub.com"
print(x)
print(type(x))
#list
x=["Go","Edu","Hub"]
print(x)
print(type(x))
#tuple
x=("Python","from","GoEduHub.com")
print(x)
print(type(x))
#boolean
x= True
print(x)
print(type(x))
x=b"Hello"
print(x)
print(type(x))
Output:
Result:
Output:
Result:
Output:
Result:
Program:
for i in range(10):
print(i,end="")
print()
l=[10,20,30,40]
for i in range(len(l)):
print(l[i],end="")
print()
sum=0
for i in range(1,11):
sum=sum+i
print("sum of first 10 natural numbers:",sum)
Output:
Result:
Output:
Result:
Program:
list=[10,30,50,20,40]
tuple=(10,50,30,40,20)
print("length of list:",len(list))
print("length of tuple:",len(tuple))
print(" max of list:",max(list))
print( "max of tuple:",max(tuple))
print("min of list",min(list))
print("min of tuple",min(tuple))
print("sum of list",sum(list))
print("sum of tuple",sum(tuple))
print("list in sorted order",sorted(list))
print("tuple in sorted order",sorted(tuple))
Output:
Result:
Program:
dict1={1:"India",2:"USA",3:"UK",4:"Canada"}
print(dict1)
print(dict1.keys())
print(dict1.values())
print(dict1.items())
Output:
Result:
Program:
A={31,22,23,15,17,19,7}
B={2,4,6,7,9,0}
print('union:',A|B)
print('difference:',A-B)
print('intersection:',A&B)
print('symmetric difference:',A^B)
Output:
Result:
Result:
Program:
matrix1=[[1,2],[3,4]]
print("printing elements of first matrix:")
for row in matrix1:
for element in row:
print(element,end="")
print()
result=[[0,0],[0,0]]
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
result[i][j]= matrix1[j][i]
print("transpose of matrix")
for row in result:
for element in row:
print(element,end="")
print()
Output:
Result:
Program:
def composite_function(f,g):
return lambda x:f(g(x))
def add(x):
return x+2
def multiply(x):
return x*2
add_multiply=composite_function(multiply,add)
print("result:",add_multiply(5))
Output:
Result:
Program:
try:
num1,num2= eval(input("enter 2 numbers,seperated by a comma:"))
result = num1/num2
print("result is",result)
except ZeroDivisionError:
print("division by zero is error!!")
except SyntaxError:
print(" comma is missing. Enter numbers seperated by comma like this 1,2")
except:
print("wrong input")
else:
print("no exceptions")
finally:
print("this will execute no matter what")
Output:
Result:
Program:
my_file=open("D:\\file1.txt","x")
print("file created successfully")
with open("file1.text",'w',encoding='utf-8')as f:
f.write(" my first file\n")
f.write(" this file\n")
f.write(" contains 3 lines\n")
print("Successfully inserted")
my_file=open("D:\\file1.txt","r")
print(my_file.read())
my_file=open("D:\\file1.txt","r")
print("Readline:")
print(my_file.readline(10))
with open("file1.txt",'a',encoding='utf-8') as f:
f.write("jaisakthi\n")
print("successfully appended")
Output:
Result:
Output:
Program:
def open_file(filename):
try:
file=open(filename,'r')
contents=file.read()
print("file contents:")
print(contents)
file.close()
except FileNotFoundError:
print("error:file not found")
file_name=input("input a file name:")
open_file(file_name)
Output:
Program:
def get_numeric_input(prompt):
while True:
try:
value=float(input())
return value
except ValueError:
print("error:invalidinput,please input a valid number")
n1=get_numeric_input("input first num:")
n2=get_numeric_input("input second num:")
result=n1+n2
print("product of the said two numbers:",result)
Output:
Program:
def testindex(data,index):
try:
result=data[index]
print("result:",result)
except IndexError:
print("error:index out of range")
nums=[1,2,3,4,5,6,7]
index=int(input("input the index:"))
testindex(nums,index)
Output:
Result:
Program:
import numpy as np
import pandas as pd
numpyArray=np.array([[5,22,3],[13,24,6]])
columns=[['x','y','z']
panda_df=pd.DataFrame(data=numpyArray,index=["A","B"],columns=columns)
print(panda_df)
Output:
Result:
Program:
import tkinter as tk
master=tk.Tk()
master.title("MARKSHEET")
master.geometry("700x250")
e1=tk.Entry(master)
e2=tk.Entry(master)
e3=tk.Entry(master)
e4=tk.Entry(master)
e5=tk.Entry(master)
e6=tk.Entry(master)
e7=tk.Entry(master)
def display():
tot=0
if e4.get()=="A":
tk.Label(master,text="40").grid(row=3,column=4)
tot+=40
if e4.get()=="B":
tk.Label(master,text="36").grid(row=3,column=4)
tot+=36
if e4.get()=="C":
tk.Label(master,text="32").grid(row=3,column=4)
tot+=32
if e4.get()=="D":
tk.Label(master,text="28").grid(row=3,column=4)
tot+=28
if e4.get()=="P":
tk.Label(master,text="24").grid(row=3,column=4)
tot+=24
if e4.get()=="F":
tk.Label(master,text="0").grid(row=3,column=4)
tot+=0
if e5.get()=="A":
tk.Label(master,text="40").grid(row=4,column=4)
tot+=40
if e5.get()=="B":
tk.Label(master,text="36").grid(row=4,column=4)
tot+=36
if e5.get()=="C":
tk.Label(master,text="32").grid(row=4,column=4)
tot+=32
if e5.get()=="D":
tk.Label(master,text="28").grid(row=4,column=4)
Program:
if (num % 2) == 0:
else:
Output:
Result:
:
Program:
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Output:
Result:
Program:
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 7
result = factorial(num)
print("The factorial of", num, "is", result)
Output:
Result:
Output:
Result:
Program:
import random
num = random.randint(1, 10)
guess = None
while guess != num:
guess = input("guess a number between 1 and 10: ")
guess = int(guess)
if guess == num:
print("congratulations! you won!")
break
else:
print("nope, sorry. try again!")
Output:
Result:
Program:
import calendar
# Enter the month and year
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
# display the calendar
print(calendar.month(yy,mm))
Output:
Result:
Output:
Result:
Result:
Program:
#Initialize array
arr = [5, 2, 8, 7, 1];
temp = 0;
print();
Output:
Program:
def binary_search(list1, n):
low = 0
high = len(list1) - 1
mid = 0
while low <= high:
# for get integer result
mid = (high + low) // 2
# Check if n is present at mid
if list1[mid] < n:
low = mid + 1
# If n is greater, compare to the right of mid
elif list1[mid] > n:
high = mid - 1
# Function call
result = binary_search(list1, n)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in list1")
Output:
Program:
def Merge_Sort(array):
if len(array) > 1:
# mid is the point where the array is divided into two subarrays
mid = len(array)//2
#print(mid)
Left = array[:mid]
Right = array[mid:]
Merge_Sort(Left)
Merge_Sort(Right)
i=j=k=0
print(Left)
print(Right)
while i < len(Left) and j < len(Right):
if Left[i] < Right[j]:
array[k] = Left[i]
i =i+ 1
else:
array[k] = Right[j]
j =j+ 1
k += 1
while i < len(Left):
array[k] = Left[i]
i =i+ 1
k =k+ 1
Result: