Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

1.

Write a python program that finds all numbers that are divisible by 7 but no multiples of 5
between 2000 and 3201.

list = [x for x in range(2000, 3201)if x%7==0 and x%5!=0]
print (list)

2. Write a function in python that generates a dictionary with the format


i:i*I given a certain integer n. for instance, if the function is
called on 3, then the dictionary should have the following format
1:1, 2: 4, 3: 9,

def gen():
n=int(input("input a number: "))
d= dict()
for x in range (1,n+1):
d[x]=x*x
print (d)
gen()

3. Write a function in python that takes in parameter name and age the function should return a
message containing the year when that person with that age turns 100 years old.

from datetime import datetime

def get_user_input():
name = input("what is your name? ")
age = int(input("how old are you? "))
return name, age

def what_year_when_100(age):
return 100 - age + datetime.now().year

if __name__=="__main__":
user_name, user_age = get_user_input()
print(f"{user_name} is going to be 100 by {what_year_when_100(user_age)}")
when_100 = what_year_when_100(user_age)
4. Write a function in python that takes in as input a certain number and
prints out a list of all divisors of that number.

def num():
number = int(input("enter number: "))
print ("\nDivisors  of %d are: " %(number))
for i in range (1, number+1):
if number%i == 0:
print(i, end=" ")

num()

5. Write a function in python that takes in a list as input and returns a new
list that has only even elements in the list.

def lst():
list = [1,4,9,16,25,36,49,64,81,100]
result = []
for num in list:
if num % 2 == 0:
result.append(num)
print (result)

lst()

6. Given 2 lists, a and b, containing integers, no necessarily with the same


length, write a python function that returns a list that contains only the
elements that are common between the two lists.

def div():
a = [2,3,3,4,5,6,6,6,7,8]
b = [1,2,8,9,9,10,11]

newlist = []
for x in b:
if x in a:
if x in newlist:
print("duplicate number")
else:
newlist.append(x)
for y in newlist:
print(y)

div()
7. Write a function in python that takes in a dictionary, and returns the sum
of all the values in a dictionary.

x = {847502: ['a', 1, 50], 847283: ['b', 2, 100], 839529: ['c', 3, 25],
483946: ['d', 4, 50], 493402: ['e', 3, 50], 485034: ['f', 4, 50]}

def Sum_The_Dict(Dictionary):
Total = 0
for val in Dictionary.values():
Total += (val[1]*val[2])
return(Total)

print(Sum_The_Dict(x))

8. Given a number n, write a function in python to determine if the number is


prime.

def Check_for_Prime ():
a1 = input("enter a number to check: ")
i = int(a1)
j = 2
k = ("It is prime")
while j < i:
if i%j == 0:
k = ("Not prime")
j = j+1
print(k)
while True:
Check_for_Prime ()
9. Give a string, write a function in python that takes in a string and if
the length of the string is at least 3, adds “ing” to its end. But if it
already ends with ing adds “ly” instead. And if the length of the string
is less than 3 leave it unchanged. Return the resulting value.

def string_checker(string):
if len(string) < 3:
return string
elif string.endswith('ing'):
return string + 'ly'
else:
return string + 'ing'

while True:
string = input()
print(string_checker(string))

10.Write a function in python that takes a character and returns its ASCII
value.

def ASCII():
c = input("Input a value: ")
print("The ASCII value of '" + c + "' is", ord(c))

ASCII()

11.Write a function in python that takes in an integer n and returns the


first n fibbonnaci members.

def Fibonacci(n):
if n<0:
print("Incorrect input")
elif n==0:
return 0
elif n==1:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)

print(Fibonacci(20))
12.Write a python scrip that implement’s a function that takes as input three
variables and returns the largest of the three. Do this without using
max() function. Hint: use if statement.

def greater(i,j,k):

if (i >= j) and (i >= k):

largest = i

elif (j >= i) and (j >= k):

largest = j

else:

largest = k

print("The largest number between",i,",",j,"and",k,"is",largest)

a = input("input the fist number")
b = input("input the second number")
c = input("input the third number")

greater(a,b,c)

You might also like