You are on page 1of 17

1a.List out Different IDE’s? Write and execute how to install python and setting path?

Different types of IDE’s for python:


1)IDLE(Integrated Development and Learning Environment)
2)Pycharm
3)Jupyter
4)pydev
5)vscode
To install python go to website www.python.org
Step 1 − Select Version of Python to Install.
Step 2 − Download Python Executable Installer.
Step 3 − Run Executable Installer.
Step 4 − Verify Python is installed on Windows.
Step 5 − Verify Pip was installed.
Setting path for python:
1.Right click on My Computer and click on properties.
2.Click on Advanced System settings.
3.Click on Environment Variable tab.
4.Click on new tab of user variables.
5.Write path in variable name.
6.Copy the path of Python folder.
7.Paste path of Python in variable value.
1b.Write a Python Program to declare and assign a Value to a variable?
In Python, there is no need of explicit declaration in variables as comparing some other
programming languages. You can start using the variable right away.
#assigning value to variable
a=5
print(“The value of a is :”,a)
Ex:
x=5
y = "John"
print(x)
print(y)
output:- 5 John
1c.write a python program to change the value of variable?
It is easy to change the values for variables in python. We can directly change the value by
giving another value to variable
#assigning value to variable
a=5
print(a) output:- 5
#changing the value of variable a
a=9
print(a)
output:- 9
1d.Write a Python Program to assign multiple values to multiple variables?
Example program:-
#Multiple values to multiple variables:-
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
output:-
Orange
Banana
Cherry
#One Value to Multiple Variables:-
x = y = z = "Orange"
print(x)
print(y)
print(z)
output:-
Orange
Orange
Orange

2. A) Write a python program to perform arithmetic operators


1. num1 = input('Enter first number: ')
2. num2 = input('Enter second number: ')
3. # Add two numbers
4. sum = float(num1) + float(num2)
5. # Subtract two numbers
6. min = float(num1) - float(num2)
7. # Multiply two numbers
8. mul = float(num1) * float(num2)
9. #Divide two numbers
10. div = float(num1) / float(num2)
11. # Display the sum
12. print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
13. # Display the subtraction
14. print('The subtraction of {0} and {1} is {2}'.format(num1, num2, min))
15. # Display the multiplication
16. print('The multiplication of {0} and {1} is {2}'.format(num1, num2, mul))
17. # Display the division
18. print('The division of {0} and {1} is {2}'.format(num1, num2, div))

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

C) Write a program to find sum , difference , product ,multiplication ,division of two


numbers by taking the input from the user?

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 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:
Enter First Number: 25
Enter Second Number: 25
Enter which operation would you like to perform?
Enter any specific operation +,-,*,/: +
25 + 10 : 35

D) Write a program to find that given year is leap year or not?


year = 2000
# year = int(input("Enter a year: "))
if (year % 400 == 0) and (year % 100 == 0):
print("{0} is a leap year".format(year))
elif (year % 4 ==0) and (year % 100 != 0):
print("{0} is a leap year".format(year))
print("{0} is not a leap year".format(year))

OUTPUT:

2000 is a leap year

3.A.Create a list and perform the following methods


1)insert()
Program:-
List=[1,3,5,7,8,9[6,7,8],”hello”]
print(a)
a.insert(3,20)
print(a)
a.remove(7)
print(a)
a.append(“hi”)
print(a)
len(a)
print(a)
a.pop()
print(a)
a.pop(6)
print(a)
a.clear()
print(a)
Output:-
[1, 3, 5, 7, 8, 9, [6, 7, 8], 'hello']
[1, 3, 5, 20, 7, 8, 9, [6, 7, 8], 'hello']
[1, 3, 5, 20, 8, 9, [6, 7, 8], 'hello']
[1, 3, 5, 20, 8, 9, [6, 7, 8], 'hello', 'hi']
[1, 3, 5, 20, 8, 9, [6, 7, 8], 'hello', 'hi']
[1, 3, 5, 20, 8, 9, [6, 7, 8], 'hello']
[1, 3, 5, 20, 8, 9, 'hello']
[]

B.Create a dictionary and apply the following methods


Program:-
#Creating a dictionary
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Output:-
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

#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:-

{'brand': 'Ford', 'model': 'Mustang', 'year': 2018}

#use len()

Len(thisdict)

Output:-

C.Create a Tuple and perform the following methods


Program:-
#Creating a tuple
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Output:-
('apple', 'banana', 'cherry')

#Adding items to a tuple


///As it is not possible to add items to tuple Convert tuple to list

x = ("apple", "banana", "cherry")


y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)

Output:-
(“apple”,”kiwi”,”cherry”)

#len()

Len(x)

Output:-3

#checking for the item in tuple

thistuple = ("apple", "banana", "cherry")


if "apple" in thistuple:
print("Yes, 'apple' is in the fruits tuple")

Output:-

Yes, 'apple' is in the fruits tuple

#Accessing tuple items

thistuple = ("apple", "banana", "cherry")


print(thistuple[1])

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

4b)# writing a program to find a entered number is negative or positive


a = int(input("Enter a number : "))
if a>0:
print("Entered number is positive")
else:
print("Entered number is negative")

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

5d)#Write a python program to perform different string methods like


lower(),upper(),join(),split(),find(),replace().
PROGRAM:
s = 'Geeks for Geeks'
# print the string after lower method
print(s.lower())
# print the string after upper method
print(s.upper())
# print the string after split method
print(s.split(" "))
# print the string after join method
print("-".join(s.split()))
# print the string after find method
print(s.find('ge', 2))
# print the string after replace method
print(s.replace("Geeks", "geeks"))
output:
geeks for geeks
GEEKS FOR GEEKS
['Geeks', 'for', 'Geeks']
Geeks-for-Geeks
-1
geeks for geeks

6b

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


d=int(input("Enter the value of d:"))
c=int(input("Enter the value of c:"))
try:
q=n/(d-c)
print("Quotient:",q)
except ZeroDivisionError:
print("Division by Zero!")

output:
Enter the value of n:10
Enter the value of d:5
Enter the value of c:5
Division by Zero!
6c

# python code to illustrate


# working of try()
def divide(x,y):
try:
# Floor division: Gives only fractional
# Part as Answer
result=x//y
except ZeroDivisionError:
print("sorry!You are dividind by zero")
else:
print("Yeah! Your answer is :",result)
finally:
# this block is always excuted
# regardless of exception generation
print('This is always executed')

# Look at parameters and note the working of program.


divide(3,2)
divide(3,0)

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

Aim-a python program to handle the ZeroDivisionError exception

Program :-

try:

a,b=[int(x) for x in input("Enter two numbers").split()]

c=a/b

print('Result is :',c)

except ZeroDivisionError:

print('Division By Zero happened')

finally:

print('This is finally block')

output :-
Enter two numbers2 3
Result is : 0.6666666666666666
This is finally block

#9 appending and then reading strings


#open the file for reading data
f=open('C:\\Users\\student\\Desktop\\PP\\vivek.txt','a+')
print('enter text to append(@ at end):')
while str!='@':
str=input()
if(str!='@'):
f.write(str+"\n")
f.seek(0,0)
print('the file contents are:')
str=f.read()
print(str)
f.close()

#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()

You might also like