Python Programs All Manual
Python Programs All Manual
OUTPUT:
Enter first number: 45
Enter second number: 563
The sum of 45 and 563 is 608.0
The subtraction of 45 and 563 is -518.0
The multiplication of 45 and 563 is 25335.0
The division of 45 and 563 is 0.07992895204262877
B.) Given 2 variables and perform a= 0011 1100, b=0000 1101 bitwise operation
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c=0
c = a & b; # 12 = 0000 1100
print("Value of c is ", c)
c = a | b; # 61 = 0011 1101
print("Value of c is ", c)
c = a ^ b; # 49 = 0011 0001
print("Value of c is ", c)
c = ~a; # -61 = 1100 0011
print("Value of c is ", c)
c = a << 2; # 240 = 1111 0000
print("Value of c is ", c)
c = a >> 2; # 15 = 0000 1111
print("Value of c is ", c)
OUTPUT:
Value of c is 12
Value of c is 61
Value of c is 49
Value of c is -61
Value of c is 240
Value of c is 15
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!")
OUTPUT:
Enter First Number: 25
Enter Second Number: 25
Enter which operation would you like to perform?
Enter any specific operation +,-,*,/: +
25 + 10 : 35
OUTPUT:
#Accessing items
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
print(x)
Output:-
Mustang
#use get()
x = thisdict.get("model")
print(x)
Output:-
Mustang
#Changing Values
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
print(thisdict)
output:-
#use len()
Len(thisdict)
Output:-
print(x)
Output:-
(“apple”,”kiwi”,”cherry”)
#len()
Len(x)
Output:-3
Output:-
Output:-
apple
4a)# write a python program for sum of two numbers
a = int(input("Enter a value : "))
b = int(input ("Enter b value: "))
c = a+b
print("sum of a and b: ",c)
output:
Enter a value : 1
Enter b value: 2
sum of a and b: 3
output:
Enter a number : -1
Entered number is negative
4c)# write a program to find the greatest value among three numbers
a = int(input("Enter a number: "))
b = int(input("Enter b value: "))
c = int(input("Enter c value: "))
if a>b and a>c:
print("the greatest is: ",a)
elif b>a and b>c:
print("the greatest is: ",b)
else :
print("the greatest is: ",c)
output:
Enter a number: 55
Enter b value: 445
Enter c value: 1
the greatest is: 445
4d)# write a program to find the corresponding day using the user input number
print("CHOOSE NUMBERS FROM 1-6 FOR ANY DAY")
n = int(input("Enter a number to access corresponding day: "))
if n==1:
print("MONDAY")
elif n==2:
print("TUESDAY")
elif n==3:
print("WEDNESDAY")
elif n==4:
print("THURSDAY")
elif n==5:
print("FRIDAY")
elif n==6:
print("SATURDAY")
else:
print("SUNDAY")
output:
CHOOSE NUMBERS FROM 1-6 FOR ANY DAY
Enter a number to access corresponding day: 2
TUESDAY
5a)# Write a python program to find the sum of all numbers stored in a list.
PROGRAM:
lst=[]
num=int(input('How many numbers:'))
for n in range(num):
numbers=int(input('Enter number'))
lst.append(numbers)
print("Sum of elements in given list is:",sum(lst))
output:
How many numbers:2
Enter number1
Enter number3
Sum of elements in given list is: 4
5b)# Write a python program to add natural numbers up to sum=1+2+3+...+n take the input
from the user by using while loop.
PROGRAM:
num =int(input('Enter end number:'))
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
output:
Enter end number:5
The sum is 15
5c)# write a python program to print numbers from 20 to 100 using range().
PROGRAM:
for i in range(20, 100):
print(i, end=" ")
print()
output:
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
6b
output:
Enter the value of n:10
Enter the value of d:5
Enter the value of c:5
Division by Zero!
6c
output:
Yeah! Your answer is : 1
This is always executed
sorry!You are dividind by zero
This is always executed
In [ ]:
7A
import numpy as np
arr=np.array([[1,2,3],[2,4,5]])
print("Array is of type;",type(arr))
print("NO. of demensions;",arr.ndim)
print("Shape pf array;",arr.shape)
print("Size of array;",arr.size)
print("Array stores elements of type;",arr.dtype)
output:
Array is of type; <class 'numpy.ndarray'>
NO. of demensions; 2
Shape pf array; (2, 3)
Size of array; 6
Array stores elements of type; int32
7B-1.
import numpy as np
initialArray = np.array(["1.1", "2.2", "3.3", "4.4"])
print("Our Initial Array: ", str(initialArray))
print("Original type: " + str(type(initialArray[0])))
finalArray = np.asarray(initialArray, dtype = np.float64,order ='c')
print("Our Final Array: ", str (finalArray))
print("Final type: " + str(type(finalArray[0])))
output:
Our Initial Array: ['1.1' '2.2' '3.3' '4.4']
Original type: <class 'numpy.str_'>
Our Final Array: [1.1 2.2 3.3 4.4]
Final type: <class 'numpy.float64'>
1
7B-2.
import numpy as array
A=array.zeros([3,4])
print (A)
output:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
7B-3.
import numpy as np
a= np.array([['ram','jam'],['sam','rio'],['pot','hot']])
b= tuple(map(tuple,a))
print ("array :"+str(b))
output:
array :(('ram', 'jam'), ('sam', 'rio'), ('pot', 'hot'))
Program-8
Program :-
try:
c=a/b
print('Result is :',c)
except ZeroDivisionError:
finally:
output :-
Enter two numbers2 3
Result is : 0.6666666666666666
This is finally block
#10
import os,sys
path='C:\\Users\\student\\Desktop\\PP\\vivek.txt'
if os.path.isfile(path):
f=open(path,'r')
else:
print(path+'does not exist')
sys.exit()
print('the file contents are:')
str=f.read()
print(str)
f.close()
#11
#importing os.path module
import os.path
#path
path='C:\\Users\\student\\Desktop\\PP\\python internals.txt'
isdir=os.path.isdir(path)
print(isdir)
#12
#counting number of lines,words and characters in a file
import os,sys
path=input('enter the file name with path:')
if os.path.isfile(path):
f=open(path,'r')
else:
print('does not exists')
sys.exit()
cl=cw=cc=0
for line in f:
words = line.split()
cl +=1
cw +=len(words)
cc +=len(line)
print('no. of lines:',cl)
print('no.of words:',cw)
print('no.of characters:',cc)
f.close()