0% found this document useful (0 votes)
9 views8 pages

pup_ass

The document contains a series of Python programming exercises and their corresponding solutions. Each exercise focuses on different programming concepts such as calculating distances, summing numbers, checking evenness, working with loops, and handling data structures. The solutions include code snippets and sample outputs for each task.

Uploaded by

lbhifreefire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

pup_ass

The document contains a series of Python programming exercises and their corresponding solutions. Each exercise focuses on different programming concepts such as calculating distances, summing numbers, checking evenness, working with loops, and handling data structures. The solutions include code snippets and sample outputs for each task.

Uploaded by

lbhifreefire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Q1.

Write a program to compute the distance between two points taking


input from the user. (Pythagorean Theorem)

Program :

X1 = float(input(“Enter x coordinate of the first point : “))

Y1= float(input(“Enter y coordinate of the first point : “))

X2= float( input(“Enter x coordinate of the second point : “))

Y2=float( input(“Enter y coordinate of the second point : “))

Distance =((x2 – x1)**2 + (y2 – y1)**2)**(1/2)

Print(“The distance between the two points is: “,distance,”units”)

Output :

Enter x coordinate of the first point : 4

Enter y coordinate of the first point : 6

Enter x coordinate of the second point : 5

Enter y coordinate of the second point : 6

The distance between the two points is: 1.0 units

Q2: Write a program add.py that takes 2 numbers as command line


arguments and prints their sum.

Program :

num1, num2=5,6

print(“The sum is”,num1+num2)

Output :

The sum is 11
Q3: Write a program to check whether the given number is an even number
or not.

Program:

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

if num % 2 == 0:

print(num,”is even.”)

else:

print(num,”is odd.”)

Output :

Enter a number: 39

39 is odd.

Q4: Using a for loop, write a program that prints out the decimal equivalents
of ½, 1/3, ¼, and 1/10.

Program:

fractions = [1/2, 1/3,1/4, 1/10]

for i in fractions:

print(i)

Output :

0.5

0.3333333333333333

0.25

0.1
Q5: Write a program using a for loop that loops over a sequence.

Program:

sequence = [1, 2, 3, 4, 5]

for num in sequence:

print(num)

Output :

Q6: Write a program using a while loop that asks the user for a number and
prints a countdown from that number to zero.

Program:

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

while number >= 0:

print(num)

num -= 1

Output :

Enter a number : 3

Q7: Find the sum of all the primes below two million.
Program :

def prime(n):

if n < 2:

return False

for i in range(2, int(n**0.5) + 1):

if n % i == 0:

return False

return True

total = sum(n for n in range(2, 2000000) if prime(n))

print(“The sum of all primes below two million is: “,total)

Output :

The sum of all primes below two million is: 142913828922

Q8: By considering the terms in the Fibonacci sequence whose values do not
exceed four million, find the sum of the even-valued terms.

Program:

a,b = 1, 2

even_sum = 0

while b <= 4000000:

if b % 2 == 0:

even_sum += b

a, b = b, a + b

print(“The sum of even Fibonacci numbers below four million is: “,even_sum)

Output :

The sum of even Fibonacci numbers below four million is: 4613732
Q9: Write a program to count the numbers of characters in a string and store
them in a dictionary data structure.

Program:

s=eval(input(“Enter a string :”))

d=dict()

for I in s:

C=s.count(I)

D[I]=c

print(d)

Output:

Enter a string :”hello world”

{‘h’: 1, ‘e’: 1, ‘l’: 3, ‘o’: 2, ‘w’: 1, ‘r’: 1,’d: 1}

Q10: Write a program to use split and join methods in the string and trace a
birthday with a dictionary data structure.

Program:

birthdays = {“aman”: “01/01/2000”, “chirag”: “02/02/1995”}

name = input(“Whose birthday do you want to look up? “)

print(birthdays.get(name, “Birthday not found.”))

sentence = “Split this sentence into words.”

words = sentence.split()

print(“ “.join(words))

Output:

Whose birthday do you want to look up? Chirag

02/02/1995

Split this sentence into words.


Whose birthday do you want to look up? Rahul

Birthday not found.

Q11: Write a program combining lists that combines these lists into a
dictionary.

Program:

keys = [“Name”, “Age”, “City”]

values = [“Alice”, 25, “New York”]

Combined = dict(zip(keys, values))

print(Combined)

Output:

{‘Name’: ‘Alice’, ‘Age’: 25, ‘City’: ‘New York’}

Q12: Write a function ball_collide that takes two balls as parameters and
computes if they are colliding.

Program:

def collide(ball1, ball2):

x1,y1,r1 = ball1

x2,y2,r2 = ball2

distance=((x2-x1)*2 + (y2-y1)*2)**(1/2)

if distance<= r1+r2:

print(“True”)

else:

print(“false”)

ball1=eval(input(“Enter the x,y,r of ball1 in tuple :”))

ball2=eval(input(“Enter the x,y,r of ball2 in tuple :”))

collide(ball1,ball2)
Output:

Enter the x,y,r of ball1 in tuple (0,0,1)

Enter the x,y,r of ball2 in tuple (4,5,5)

True

Q13: Find the mean, median, and mode for a given set of numbers in a list.

Program:

from statistics import mean, median, mode

N = [1, 2, 3, 4, 4]

print(“mean=”,mean(N),”median=”,median(N),”mode=”,(mode(N)))

Output:

Mean= 2.8 median= 3 mode= 4

Q14: Write a function nearly_equal to test whether two strings are nearly
equal.

Program:

def nearly_equal(a, b):

count=0

if len(a) != len(b):

return False

for i in range(len(a)) :

if a[i]==b[i] :

count += 1

if count*80/100>=len(a) :

return True
print(nearly_equal(“apple”, “apble”))

Output

True

Q15: Write a function dups to find all duplicates in a list.

Program:

def dups(lst):

l=[]

for item in lst:

if lst.count(item) > 1:

if item not in l:

l.append(item)

for i in l:

print(i)

dups([1, 2, 3, 2, 4, 4,5])

Output :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy