Python File (Sem 1) (Complete)
Python File (Sem 1) (Complete)
Step 2) Once the download is completed, run the .exe file to install Python. Now click on Install
Now
~1~
Step 4) When it finishes, you can see a screen that says the Setup was successful. Now click
on “Close”.
Step 2) Once the download is complete, run the exe for install PyCharm. The setup wizard
should have started. Click “Next”.
~2~
Step 3) On the next screen, Change the installation path if required. Click “Next”.
Step 4) On the next screen, you can create a desktop shortcut if you want and click on “Next”.
Step 5) Choose the start menu folder. Keep selected JetBrains and click on “Install”.
~3~
Step 6) Wait for the installation to finish.
Step 7) Once installation finished, you should receive a message screen that PyCharm is
installed. If you want to go ahead and run it, click the “Run PyCharm Community Edition” box
first and click “Finish”.
Step 8) After you click on “Finish,” the Following screen will appear.
~4~
3. Running instructions in Interactive interpreter and a
Python Script
Open command prompt and type python or python3 and click Enter.
Open Python 3.10 IDE and print “Hello World” and press Enter.
~5~
4. Write a program to purposefully raise Indentation
Error and Correct it
Program with indentation error
Input:
a= "YE NOT CRAZY!!"
print(a)
Output:
Output:
~6~
5. Operations
x1=int(input("enter x1 : "))
x2=int(input("enter x2 : "))
y1=int(input("enter y1 : "))
y2=int(input("enter y2 : "))
~7~
~8~
2. Write a program to find sum, difference, product, quotient, remainder
of two numbers taking input from user.
~9~
~ 10 ~
6. Conditionals and Loops
1. Write a Program for checking whether the given number is an even
number or not.
~ 11 ~
~ 12 ~
2. Using a for loop, write a program that prints out the decimal
equivalents of 1/2, 1/3, 1/4, . . .,
1/10.
for i in range(2,11):
print(1/i)
~ 13 ~
~ 14 ~
3. Write a program to find prime factors of a number by taking
inputs from user
4. def primefactor(n):
while (n%2==0):
print(2)
n=n/2
if(n>2):
print(n)
~ 15 ~
~ 16 ~
4. Write a program using a while loop that asks the user for a number,
and prints a countdown
from that number to zero.
~ 17 ~
~ 18 ~
5. Write a program to find whether a number is prime or not such that
number is input by user.
def primenumber(n):
h=False
for i in range(2,n):
if n%i==0:
h=True
break
if h:
print("Not a prime number")
else:
print("Prime Number")
~ 19 ~
~ 20 ~
6. Write a program to print Fibonacci series using loops.
~ 21 ~
~ 22 ~
7. Write a program to check whether a number is Armstrong number or
not.
if count==n:
print("armstrong number")
else:
print("not")
~ 23 ~
~ 24 ~
8. Write to find sum of squares till the number input by the user.
~ 25 ~
~ 26 ~
9. Write the code for following patterns:
a.
for i in range(1,row+1):
for c in range(1,i+1):
print(c, end=" ")
print(" ")
b.
c.
~ 27 ~
a.
b.
c.
~ 28 ~
10. Write a program to print pascal’s triangle.
def fact(n):
ans=1
for i in range (2,n+1):
ans*=i
return int(ans)
n = int(input("Enter the number : "))
for i in range(0,n):
for k in range(n,i,-1):
print (end=" ")
for j in range (0,i+1):
print(fact(i)//(fact(j) * fact(i-j)),end=" ")
print()
~ 29 ~
~ 30 ~
11. Write a program to write factorial of a number using loops.
~ 31 ~
~ 32 ~
12. Write a program to find reverse of the number ‘895458’.
s = "895458"
print ("The reversed string is: ",end="")
print (s[::-1])
output
~ 33 ~
7. Functions and Recursion
1. Write a function that takes two balls as parameters and computes if
they are colliding. Your function should return a Boolean representing
whether or not the balls are colliding.
Hint:Represent a ball on a plane as a tuple of (x, y, r), r being the
radius. If (distance between two balls centers) <= (sum of their radii)
then (they are colliding).
~ 34 ~
~ 35 ~
2. Write function to compute gcd, lcm of two numbers using recursion.
def GCD(x,y):
r=x%y
if(r==0):
return y
else:
return GCD(y,r)
def lcm(a,b):
lcm.multiple=lcm.multiple+b
if((lcm.multiple % a == 0) and (lcm.multiple % b == 0)):
return lcm.multiple;
else:
lcm(a, b)
return lcm.multiple
lcm.multiple=0
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
if(a>b):
LCM=lcm(b,a)
else:
LCM=lcm(a,b)
print(f"The LCM of the two numbers is: {LCM}")
~ 36 ~
~ 37 ~
3. Write a function to find length of a string.
Output
~ 38 ~
4. Write a program to print Fibonacci series and Factorial of a number
with and without using recursion.
a.with recursion
#fibonacci program
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
if nterms <= 0:
print("Please enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
#factorial program
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))
~ 39 ~
~ 40 ~
b. without recursion
#fibonacci
def fibonacci():
f = 0
s = 1
if a <= 0:
print("The requested series is",f)
else:
print(f)
print(s)
for x in range(2, a):
next = f + s
print(next)
f = s
s = next
#factorial
def factorial(num):
factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative
numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
~ 41 ~
~ 42 ~
5. Solve the following Tower of Hanoi.
disks = 4
#referring source as A, auxiliary as B, target as C
tower_of_hanoi(disks, 'A', 'B', 'C') # Calling the function
~ 43 ~
~ 44 ~
7. Strings
1. Write a function nearly equal to test whether two strings are nearly
equal. Two strings a and b are nearly equal when a can be generated
by a single mutation on b.
~ 45 ~
~ 46 ~
2. Write a function to find whether a string is palindrome or not.
def isPalindrome(s):
return s == s[::-1]
s = input("Enter something: ")
ans = isPalindrome(s)
if ans:
print("a palindrome")
else:
print("Not a palindrome")
~ 47 ~
~ 48 ~
3. Write a program to find number of P’s in “Problem Solving using
Python”
~ 49 ~
~ 50 ~
8. Lists, Tuples, Dictionaries and Sets
1. Write a program that finds sum of all even numbers in a list.
num=[a,b,c]
sum=0
for i in range(0 , len(num)):
if num[i]%2==0:
sum= sum +num[i]
~ 51 ~
~ 52 ~
2. Write a program that finds the sum of all the numbers in a Tuples
using while loop.
~ 53 ~
~ 54 ~
3. Write a program to reverse dictionary elements.
# initializing dictionary
test_dict = {'a': a, 'b': b, 'c': c}
print("The original dictionary : " + str(test_dict))
res = dict(reversed(list(test_dict.items())))
~ 55 ~
~ 56 ~
4. Write a program to find all duplicate elements in the list.
l=[11,22,11,33,45,54,99,99,88,767,767,98,69,54,65,76,32,69,90,21
,21,34,32]
l1=[]
print("The duplicate elements are: ")
for i in l:
if i not in l1:
l1.append(i)
else:
print(i,end=' ')
~ 57 ~
~ 58 ~
5. Write a program unique to find all the unique elements of a list.
my_list = [11,2,45,32,66,987,54,69,69,33,11,2]
print("Original List : ",my_list)
my_set = set(my_list)
my_new_list = list(my_set)
print("List of unique numbers : ",my_new_list)
~ 59 ~
~ 60 ~
6. Write a program to compute cumulative product of a list of numbers.
def product(list):
p =1
for i in list:
p *= i
print(p)
return p
list= [1,2,3,4,5,6,7,8,9,10]
c=product(list)
~ 61 ~
~ 62 ~
7. Find mean, median, mode for the given set of numbers in a list.
def find_mean(numbers):
return sum(numbers) / len(numbers)
def find_median(numbers):
if len(numbers) % 2 == 0:
return (numbers[(len(numbers) - 1) // 2] +
numbers[(len(numbers) + 1) // 2]) / 2
return numbers[len(numbers) // 2]
def find_max_frequency(frequencies):
return max(frequencies)
def find_modes(numbers):
frequencies = []
for number in numbers:
frequency = count_frequency(numbers, number)
frequencies.append(frequency)
max_frequency = find_max_frequency(frequencies)
modes = []
numbers = [12, 435, 12, 46, 565, 87, 867, 998, 90, 32, 432, 87]
numbers.sort()
mean = find_mean(numbers)
median = find_median(numbers)
modes = find_modes(numbers)
print("Mean: ", round(mean,2))
print("Median: ", median)
print("Mode: ", end='')
for mode in modes:
print(mode, end=" ")
~ 63 ~
~ 64 ~
8. Given the sets A={“Arush”, “Sunita”, 89, 98.5} & B={89, “DSEU”}
~ 65 ~
~ 66 ~