Challenging Python Programs

Question 1:

Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,
between 2000 and 3000 (both included).
The numbers obtained should be printed in a comma-separated sequence on a single line.

Hints:
Consider use range(begin, end) method

Solution :

a = []
for i in range(2000, 3001):
    if (i%7==0) and (i%5!=0):
        a.append(str(i))
print(','.join(a))

Question 2 :

Write a program which can compute the factorial of a given numbers.
The results should be printed in a comma-separated sequence on a single line.
Suppose the following input is supplied to the program:
8
Then, the output should be:
40320

Solution :

def fact(x):
    if x == 0:
        return 1
    return x * fact(x - 1)

x=int(input("Enter Number: "))
print(fact(x))

Question 3 :

With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.
Suppose the following input is supplied to the program:
8
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
Consider use dict()

Solution :

n=int(input())
a=dict()
for i in range(1,n+1):
    a[i]=i*i
print(a)

Question 4 :

Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.
Suppose the following input is supplied to the program:
34,67,55,33,12,98
Then, the output should be:
[’34’, ’67’, ’55’, ’33’, ’12’, ’98’]
(’34’, ’67’, ’55’, ’33’, ’12’, ’98’)

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
tuple() method can convert list to tuple.

Solution :

values=input("Enter Values: ")
list1 = values.split(",")
tup1 = tuple(list1)
print(list1)
print(tup1)

Question 5 :

Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.
Suppose the following input is supplied to the program:
without,hello,bag,world
Then, the output should be:
bag,hello,without,world

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Solution :

items=[x for x in input("Enter Words :").split(',')]
items.sort()
print(','.join(items))

Question 6 :

Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.
Suppose the following input is supplied to the program:
Hello world
Practice makes perfect
Then, the output should be:
HELLO WORLD
PRACTICE MAKES PERFECT

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Solution :

lines = []
while True:
    s = input("Enter Lines :")
    if s:
        lines.append(s.upper())
    else:
        break
for sentence in lines:
    print(sentence)
# Leave input blank to stop

Question 7 :

Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.
The numbers obtained should be printed in a comma-separated sequence on a single line.

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Solution :

values = []
for i in range(1000, 3001):
    s = str(i)
    if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0):
        values.append(s)
print(",".join(values))

Question 8 :

Write a program that accepts a sentence and calculate the number of letters and digits.
Suppose the following input is supplied to the program:
hello world! 123
Then, the output should be:
LETTERS 10
DIGITS 3

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Solution :

s= input("Enter a Sentence with Digits :")
d={"DIGITS":0, "LETTERS":0}
for c in s:
    if c.isdigit():
        d["DIGITS"]+=1
    elif c.isalpha():
        d["LETTERS"]+=1
    else:
        pass
print("LETTERS", d["LETTERS"])
print("DIGITS", d["DIGITS"])

Question 9 :

Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.
Suppose the following input is supplied to the program:
Hello world!
Then, the output should be:
UPPER CASE 1
LOWER CASE 9

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Solution :

s = input("Enter a Sentence: ")
d={"UPPER CASE":0, "LOWER CASE":0}
for c in s:
    if c.isupper():
        d["UPPER CASE"]+=1
    elif c.islower():
        d["LOWER CASE"]+=1
    else:
        pass
print("UPPER CASE", d["UPPER CASE"])
print("LOWER CASE", d["LOWER CASE"])

Question 10 :

Write a python program to create a guessing game.

Hints:
Use random module

Solution :

import random
# to create a range of random numbers between 1-10
n = random.randrange(1,10)
# to take a user input to enter a number
guess = int(input("Enter any number: "))
while n!= guess: # means if n is not equal to the input guess
    # if guess is smaller than n
    if guess < n:
        print("number is more than",guess)
        # to again ask for input
        guess = int(input("Enter number again: "))
    # if guess is greater than n
    elif guess > n:
        print("number is less than",guess)
        # to again ask for the user input
        guess = int(input("Enter number again: "))
    # if guess gets equals to n terminate the while loop
    else:
        break
print("you guessed it right!!")

Follow us on Instagram for all your Queries

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1431

Leave a Reply