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

# Learn Python by examples – Ex-16-20

## Learn Python by examples – Ex-16-20

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

Exercise 16: Write a program, say hello, using function

Solution:

Concept:

Simple function definition and calling function.

```# function

def greet():
print('Hello !')

greet()

Output:

Hello !

```

Exercise 17: Write a program add two numbers given by user, Ex: 30, 77 =>107

Concept:

Simple function, with two parameters
Handling input parameters and doing arithmetic operation.

Solution:

```#passing arguments, and calling function

c = a + b

print('{} + {} = {}'.format(a,b,c))

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

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

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 :>30
Enter second number :>77
30 + 77 = 107

```

Exercise 18: Write a program to check, given number is odd or even

Solution:

Concept:
Defining small function which returns a value,
return value is used conditional if-else.

```
# return from function

def is_even(num):
if num % 2 == 0:
return True
else:
return False

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

if is_even(a):
print(' {} is even'.format(a))

else:
print(' {} is odd'.format(a))

Output:

Enter a number :>4
4 is even

Enter a number :>9
9 is odd

```

Exercise 19: Write a simple calculator program for, addition, subtraction, multiplication, division.

Solution:

Concept:

Defining small functions, and calling functions in user interface,
Based on user input.

```
#arguments to func, return, and calling, in interface

return a + b

def sub_num(a,b):

return a - b

def mul_num(a,b):

return a * b

def div_num(a,b):

return a / b

def my_calculator():

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

y = int(input('Enter a second number :>'))

result = None

if choice == 1:

print('{} + {} = {}'.format(x,y,result))

elif choice == 2:

result = sub_num(x,y)

print('{} - {} = {}'.format(x,y,result))

elif choice == 3:

result = mul_num(x,y)

print('{} * {} = {}'.format(x,y,result))

elif choice == 4:

if y != 0:

result = div_num(x,y)
print('{} / {} = {}'.format(x,y,result))
else:
print('Can't divide by zero')

else:

print('Invalid choice ')

my_calculator()

Output:

Enter a first number :>5
Enter a second number :>2
5 / 2 = 2.5

```

Exercise 20: Write a program print prime numbers up to 100

Solution:

Concept:
using for-loop, if-else in function.

```# for, if , inside function

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,end = ' ')

Output:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

```