Exercises Python

In this lesson we will cover some simple exercises on the Python language.

Exercises Python – first exercise

Take as input the hypotenuse and the cathetus of a right triangle and the side of a square. Then check if the two figures are equivalent.

First of all, we ask you to enter the input data, in this case the cathetus, the hypotenuse and the side of a square.

hypotenuse = int(input(‘Insert hypotenuse:’))
catheto = int(input(‘Insert the catheto:’))

side = int(input(‘Insert side of square:’))

Then we calculate the second side of the right triangle.

To perform this calculation we need to use the square root or the sqrt function. So we need to import the math module.

We insert the print (cathetus) only to verify the result, then we will delete it.

Here is the code of the first of the exercises in Python that I propose to you in this lesson:


import math

hypotenuse = int(input('Insert hypotenuse:'))
catheto = int(input('Insert the catheto:'))
side = int(input('Insert side of square:'))

catheto2 = math.sqrt(hypotenuse ** 2-cathetus ** 2)

print(cateto2)

But be careful, if we enter a value of the hypotenuse lower than the cathetus, an error message will clearly result. In fact it is not possible to make the square root of a negative number.

Then we could modify the code in order to carry out a check on the data entered in the input.

So let’s modify the code like this:


import math

hypotenuse = int(input('Insert hypotenuse:'))
catheto = int(input('Insert the catheto:'))

side = int(input('Insert side of square:'))

if hypotenuse> cathetus:
    catheto2 = math.sqrt(hypotenuse ** 2-cathetus ** 2)
    print(cateto2)
else:
    print('The data entered is invalid')

Now it remains for us to calculate the areas of the two geometric figures.

Then we proceed to calculate the area of ​​the right triangle only if the hypotenuse is greater than the cathetus. Then we also calculate the area of ​​the square and finally we evaluate if the two figures are equivalent.

Here is the complete code:


import math

hypotenuse = int (input ('Insert hypotenuse:'))
catheto = int (input ('Insert the catheto:'))

side = int (input ('Insert side of square:'))

if hypotenuse> cathetus:
    catheto2 = math.sqrt (hypotenuse ** 2-cathetus ** 2)
    area_t = catheto * catheto2 / 2
    area_q = side ** 2
    if area_q == area_t:
        print ('They are equivalent!')
    else:
        print ('They are not equivalent!')
else:
    print ('The data entered is invalid')

Exercises Python – Second exercise

We continue with other exercises in Python always using conditional statements.

Input the price of a product, then apply a 20% discount if the price is higher than € 50.00, 10% otherwise. Display the discounted price in output.

The algorithm is easily solved with the use of conditional statements.

Just subtract 20% of the product price from the initial price.

Ultimately I can write:


price = float(input('Enter price:'))
if price > 50:
    price -= price * 20/100
else:
    price -= price * 10/100

print('The discounted price is:', price)

Or even more simply:


price = float (input ('Enter price:'))
if price > 50:
    price *= 80/100
else:
    price *= 90/100

print('The discounted price is:', price)

And therefore also price *= 0.8.

In this lesson we have developed some simple exercises on the Python language, in the next lessons we will deal with others.

Some useful links

Python tutorial

Python Compiler

Install Python

Variables

Assignment operators

Strings

Casting

How to find the maximum of N numbers

How to use the math module

Bubble sort

Matplotlib Plot

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *