1. Home
  2. Docs
  3. Learn Python By Examples
  4. Learn Python by examples – Ex-11-15

Learn Python by examples – Ex-11-15

Learn Python by examples – Ex-11-15

 
Note there are multiple ways to solve a problem.
Here is one solution.
 

Exercise 11: Write a program to print biggest of two numbers given by user. Ex : 100, 23 => 100
 

Solution:
 

Concept:

Use of if -else conditional statement.
Use of max() built-in function.
 

#if - else conditional statement

a = int(input('Enter first number :>'))

b = int(input('Enter  second number :>'))

if a > b:
    print(a, ' is bigger')
else:
    print(b,' is bigger ')

Output:

Enter first number :>100
Enter  second number :>23
100  is bigger

#option 2:

#using max() built-in function.


a = int(input('Enter first number :>'))

b = int(input('Enter  second number :>'))

print(max(a,b), 'is the biggest')

Output:

Enter first number :>100
Enter  second nuber :>23
100 is the biggest

 
 

Exercise 12: Write a program to print smallest of three numbers given by user. Ex 22, 9, 100 => 9
 
Concept:

use of if-elif-else conditional statement,
use of ‘and’ Boolean operator.
use of min() built-in function.

 
Solution:
 

#option 1: using while loop

#if-elif-else and bolean types and, or, not

a = int(input('Enter first number :>'))

b = int(input('Enter second number :>'))

c = int(input('Enter third number :>'))

if a < b and a < c:
    print(a, ' is smallest')

elif b < c:
    print(b, ' is smallest')

else:
    print(c, 'is smallest')


Output:

Enter first number :>22
Enter second nuber :>9
Enter third number :>100
9  is smallest

#option 2:

#using min() built-in function


a = int(input('Enter first number :>'))

b = int(input('Enter second number :>'))

c = int(input('Enter third number :>'))

print(min(a,b,c), ' is the smallest')


Output:

Enter first number :>22
Enter second nuber :>9
Enter third number :>100
9  is the smallest


 
 
Exercise 13: Write a program to ask user to pick color from a list, validate for user input
colors = [‘Blue’,’Green’,’Orange’,’Red’]
 
Solution:

 
Concept:

User input validation.
use of ‘or’ Boolean operator.
 


#nested if-elif , bolean types or

colors = ['Blue','Green','Orange','Red']

color = int(input('Enter a number between 1-4, to pick a color :>'))

if color > 4 or color < 1:
    print('Sorry !, Invalid choice ')
    
else:
    color -= 1 #index starts at 0
    print('you picked {} color'.format(colors[color]))

Output:

Enter a number between 1-4, to pick a color :>3
you picked Orange color


 
 

Exercise 14: Write a program to find all numbers divisible by 7 but are not a multiple of 2, between 120 and 220.
 
Solution:

 
Concept:

for loop with range(),

use of modulus %, ‘and’ Boolean operators.

 


#modulus, comparison operators, boolean operator and

for num in range(120,220):
    if num % 7 == 0 and num % 2 != 0:
        print(num, end= ' ')

Output:

133 147 161 175 189 203 217 

 
 

Exercise 15: Write a program to check truthness of a variable

 
Solution:

 
Concept:
Truethness of variable,
use of True, False,
empty strings – ”, dictionary – {}, list -[], set-() equates to False.

 

#True, False, None, '', {},[],()

x = 10

y = ''

if x != False:
    print('x has a value',x)

else:
    print('x has no value',x)
    
if y == True:
    print('y has a value',y)

else:
    print('y has no value',y)


Output:

x has a value 10
y has no value