Beginner Exercises.
Beginner Exercises are basic exercises for beginners.
There are multiple ways to solve a problem.
Here you find will easy solutions.
The exercises will get you, started with python coding.
First try these on your own, and then checkout solutions.
Exercise 1: Print ‘Hello World’
Solution:
print("Hello World !")
Exercise 2: Say Hello to user, Expected, ‘Hello John’, “Hello Mary” etc…
Solution:
user_name = input("Enter your name :") print("Hello ", user_name)
Exercise 3: Take two numbers from user, print the sum.
Solution:
num1 = int(input(" Enter first number :")) num2 =int(input(" Enter second number :")) sum = num1 + num2 print(str(num1) + " + " + str(num2) + " = " , sum)
Exercise 4: Print even numbers below 100.
Solution:
#using while loop n = 2 while n < 100: print(n) n += 2 # using range(start, end, step) built in for num in range(2,100,2): print(num)
Exercise 5: Print odd numbers below 50, in descending order
Solution:
#using while loop n = 49 while n > 0: print(n) n -= 2 #using range(start, end,step) for num in range(50,0,-1): if num %2 != 0: print(num)
Exercise 6: Print prime numbers below 100.
Solution:
def is_prime(num): divisor = int(num/2)+1 for i in range(2,divisor): if num % i == 0: return False return True for num in range(2,100): if is_prime(num): print(num)
Exercise 7: Take two numbers from user, and print the biggest.
Solution:
n1 = int(input("Enter first number :")) n2 = int(input("Enter second number :")) if n1 >= n2 : print("n1 is the biggest ", n1) else: print('n2 is biggest',n2)
Exercise 8: Take three numbers from user, and print the biggest.
Solution:
n1 = int(input("Enter first number :")) n2 = int(input("Enter second number :")) n3 = int(input("Enter third number :")) if n1 >= n2 and n2 >= n3: print("n1 is the biggest ", n1) elif n2 >= n1 and n2 >= n3: print("n2 is biggest",n2) else: print('n3 is biggest',n3) # You can also use built-in function to get maximum value print(max(n1,n2,n3))
Exercise 9: Ask the user for a number,
to pick the color from the list.
Blue, Green, Orange, Red
and print the color.
Solution:
colors = ['Blue','Green','Orange','Red'] color = int(input('Enter a number between 1 and 4 :')) if color > 4 or color < 1: print('Wrong Choice') else: color -= 1 #index starts at 0 if colors[color] == 'Blue': print ('You picked Blue') elif colors[color] == 'Green': print ('You picked Green') elif colors[color] == 'Orange': print ('You picked Orange') elif colors[color] == 'Red': print ('You picked Red')