Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
103 views

Python File

The document provides instructions for installing Python and Jupyter Notebook/Pycharm IDEs on a computer. It also includes code snippets to demonstrate basic Python programming concepts like functions, conditionals, loops, etc. Several programs are written to calculate distance between points, find factors of a number, check if a number is prime, print patterns like Pascal's triangle, and more. Functions are defined recursively to find Fibonacci numbers, factorials and GCD/LCM of two numbers.

Uploaded by

Devesh Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Python File

The document provides instructions for installing Python and Jupyter Notebook/Pycharm IDEs on a computer. It also includes code snippets to demonstrate basic Python programming concepts like functions, conditionals, loops, etc. Several programs are written to calculate distance between points, find factors of a number, check if a number is prime, print patterns like Pascal's triangle, and more. Functions are defined recursively to find Fibonacci numbers, factorials and GCD/LCM of two numbers.

Uploaded by

Devesh Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

202

Computer
Science
Project

Name :- Devesh Singh


Roll No. :- 41521015
Subject & Teacher :- Mrs. Sunita tiwari
Ma’am (CSE)
College :- GB Pant DSEU Okhla 1 Campus
1. Learning about installation of python and various IDE
Step 1) To download and install Python, visit the official website of Python https://www.python.org/downloads/ and
choose your version. We have chosen Python version 3.6.3

Step 2) Once the download is completed, run the .exe file to install Python. Now click on Install Now
Step 3) You can see Python installing at this point.

Step 4) When it finishes, you can see a screen that says the Setup was successful. Now click on “Close”.
2. Learning about installation of Jupyter Notebook or Pycharm

Step 1) To download PyCharm visit the website https://www.jetbrains.com/pycharm/download/ and Click the
“DOWNLOAD” link under the Community Section.

Step 2) Once the download is complete, run the exe for install PyCharm. The setup wizard should have started. Click
“Next”.
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”.

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
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.
4. Write a program to purposefully raise Indentation Error and Correct it

Program with indentation error

Input:

a= "YE NOT CRAZY!!"

print(a)

Output:

Program when error was corrected

Input:

a= "YE NOT CRAZY!!"

print(a)

Output:
5. Operations

1. Write a program to compute distance between two points taking input from the user.
x1=int(input("enter x1 : "))

x2=int(input("enter x2 : "))

y1=int(input("enter y1 : "))

y2=int(input("enter y2 : "))

result= ((((x2 - x1 )**2) + ((y2-y1)**2) )**0.5)

print("distance between",(x1,x2),"and",(y1,y2),"is : ",result)

2. Write a program to find sum, difference, product, quotient, remainder of two numbers taking input
from user.

n1 = float(input("Enter First Number: "))

# taking input from user

n2 = float(input("Enter Second Number: "))

# taking input from user

print("Sum =", n1+n2)

# calculating and printing Sum

print("Difference =", n1-n2)

# calculating and printing Difference

print("Product =", n1*n2)

# calculating and printing Product

print("Quotient =", n1/n2) # calculating and printing Quotient


6. Conditionals and Loops

1. Write a Program for checking whether the given number is an even number or not.
n = int(input("Enter a number: "))

if n%2==0:

print("Even number")

else: print("Odd number")

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)
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
for i in range(3, int(n)):
while n%i==0:
print(i)
n=n/i
if(n>2):
print(n)
n= int(input("Enter a number: "))
primefactor(n)

4. 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 a number: "))

while(n>0):

n -= 1 print(n)
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")

n =int(input("Enter a number: "))

primenumber(n)
6. Write a program to print Fibonacci series using loops.

n=int(input("Enter a number: "))

a=0

b=1

for i in range(0,n):

print(a)

c=a+b

a=b

b=c

i+=1

7. Write a program to check whether a number is Armstrong number or not.

n= int(input("Enter a number: "))

count=0

num=n while num>0:

r=num%10

count=count + (r**3)

num//=10

if count==n:

print("armstrong number")

else: print("not")
8. Write to find sum of squares till the number input by the user.
n= int(input("Enter a number: "))

count=0

for i in range(0, n+1):

count=count + (i**2)

print(count)
9. Write the code for following patterns:
a.
row = int(input("Enter a number: "))
for i in range(1,row+1):
for c in range(1,i+1):
print(c, end=" ")
print(" ")
b.
for i in range (0,6):

for k in range (6,i,-1):

print(" ",end="")

for j in range (0,i+1):

print ("* ",end="")

print("")

for i in range(0,5):

for k in range (0,i+2):

print (" ",end="")

for j in range (5,i,-1):

print("* ",end="")

print()

c.
n = int ( input ("Enter a number : ") )

for i in range (0,n):

for j in range (0,n):

if i==0 or i == n-1 or j==0 or j==n-1:

print ("$",end=" ")

else:

print(end=" ")

print()
A.

B.

C.
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()

11. Write a program to write factorial of a number using loops .

n = int(input("Enter the number : " ))

answer=1

for i in range (1,n+1):

answer*=i
print(answer)

12. Write a program to find reverse of the number ‘895458’.


s = "895458"
print ("The reversed string is: ",end="")
print (s[::-1])

output:-

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).
a=int(input("Enter the x1 coordinate: "))

b=int(input("Enter the y1 coordinate: "))

p=int(input("Enter the x2 coordinate: "))

q=int(input("Enter the y2 coordinate: "))

r1=int(input("Enter the radius of ball 1: "))

r2=int(input("Enter the radius of ball 2: "))

def collision():

ball1 = (a, b, r1) ball2 = (p, q, r2)

dist=((((q-b)**2) + ((p-a)**2))**0.5)

sum=r1+r2

if dist<=sum:

print("The balls are colliding")

else: print("Not colliding")

print("checking the Collision status:-")

collision()

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

n= int(input("Enter the first number :"))

m= int(input("Enter the second number :"))

print("The GCD of two numbers is:", GCD(n,m))

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}")

OUTPUT :-

3. Write a function to find length of a string.


string=input("Enter something: ")
print(len(string))
Output:-
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))

nterms = int(input("Enter a number: "))

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)
num = int(input("Enter a number: "))

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

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

a=int(input("Enter the terms: "))

fibonacci()

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

num=int(input("Enter a number: "))

factorial(num)

OUTPUT:-

5. Solve the following Tower of Hanoi.


#recursion method used

def tower_of_hanoi(disks, source, auxiliary, target):

if (disks == 1):

print('Move disk 1 from rod {} to rod {}.'.format(source, target))


return

#function call itself

tower_of_hanoi(disks - 1, source, target, auxiliary)

print('Move disk {} from rod {} to rod {}.'.format(disks, source,


target))
tower_of_hanoi(disks - 1, auxiliary, source, target)

disks = 4

#referring source as A, auxiliary as B, target as C tower_of_hanoi(disks, 'A',


'B', 'C') # Calling the function

Required: 4 disks used, n=4


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.

string1 = input("Enter first string: ")

string2 = input("Enter second string: ")

if string1 == string2:

print("\nBoth strings are equal to each other.")

print(string1," equal to ",string2)

else:

print("\nStrings are not equal.")

print(string1," not equal to ",string2)

OUTPUT:-
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")

OUTPUT:-
3. Write a program to find number of P’s in “Problem Solving using Python”

s="Problem Solving Using Python"

n=0

for i in range(0,len(s)):

if (s[i]=="P") or (s[i]=="p"):

n+=1

print(f"The number of P's are: {n}")


8. Lists, Tuples, Dictionaries and Sets
1. Write a program that finds sum of all even numbers in a list.

a=int(input("Enter a number: "))

c=int(input("Enter a number: "))

b=int(input("Enter a number: "))

num=[a,b,c]

sum=0

for i in range(0 , len(num)):

if num[i]%2==0:

sum= sum +num[i]

print(f"The number of even numbers are: {sum}")

OUTPUT:-
2. Write a program that finds the sum of all the numbers in a Tuples using while loop.

a=int(input("Enter a number: "))

c=int(input("Enter a number: "))

b=int(input("Enter a number: "))

tuple = (a, b, c)

s = 0

i = 0

while(i < len (tuple)):

s = s + tuple[i]

i+= 1

print(f"Sum of numbers in the tuple: {s}")

OUTPUT:-

3. Write a program to reverse dictionary elements.

a=int(input("Enter a number: "))

c=int(input("Enter a number: "))

b=int(input("Enter a number: "))

# initializing dictionary

test_dict = {'a': a, 'b': b, 'c': c}


print("The original dictionary : " + str(test_dict))

res = dict(reversed(list(test_dict.items())))

# printing reversed order dictionary

print("The reversed order dictionary : " + str(res))

OUTPUT:-

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=' ')

OUTPUT:-
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)

OUTPUT:-

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)
OUTPUT:-

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 count_frequency(numbers, number):

frequency = 0

for i in range(len(numbers)):

if numbers[i] == number:

frequency += 1
return frequency

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 = []

for number in numbers:

frequency = count_frequency(numbers, number)

if frequency == max_frequency and number not in modes:

modes.append(number)

return 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=" ")


OUTPUT:-

8. Given the sets A={“Arush”, “Sunita”, 89, 98.5} & B={89, “DSEU”}
Write a program to find the following:
a) Union of two sets
b) Intersection of two sets
c) Symmetric Difference of two sets
d) Difference of two sets

# sets A={“Arush”, “Sunita”, 89, 98.5} & B={89, “DSEU”}

a = {"Arush", "Sunita", 89, 98.5}

b = {89, "DSEU"}

print("A U B : ", a.union(b))

print("A ∩ B : ", a.intersection(b))

print("A Δ B : ", a.symmetric_difference(b))

print("A - B : ", a.difference(b))


OUTPUT:-

You might also like