pup_ass
pup_ass
Program :
Output :
Program :
num1, num2=5,6
Output :
The sum is 11
Q3: Write a program to check whether the given number is an even number
or not.
Program:
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:
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]
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:
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
if n % i == 0:
return False
return True
Output :
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
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:
d=dict()
for I in s:
C=s.count(I)
D[I]=c
print(d)
Output:
Q10: Write a program to use split and join methods in the string and trace a
birthday with a dictionary data structure.
Program:
words = sentence.split()
print(“ “.join(words))
Output:
02/02/1995
Q11: Write a program combining lists that combines these lists into a
dictionary.
Program:
print(Combined)
Output:
Q12: Write a function ball_collide that takes two balls as parameters and
computes if they are colliding.
Program:
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”)
collide(ball1,ball2)
Output:
True
Q13: Find the mean, median, and mode for a given set of numbers in a list.
Program:
N = [1, 2, 3, 4, 4]
print(“mean=”,mean(N),”median=”,median(N),”mode=”,(mode(N)))
Output:
Q14: Write a function nearly_equal to test whether two strings are nearly
equal.
Program:
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
Program:
def dups(lst):
l=[]
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 :