by Cristina | Jun 5, 2021 | Python
I propose solutions to solve the algorithm that finds in Python the max number among three numbers taken as input, without using lists. As mentioned in previous lessons, there is almost always more than one solution to solve a problem.
Python max number of 3 numbers
Given three integers a, b and c, find the maximum value.
First solution to find the max number of three numbers in Python.
In the first solution I first check:
- a > b if it’s true I pass to the condition a > c (it is in fact taken for granted that b cannot be the greater):
- a > c if true, print the value a; otherwise it prints the value c.
- a > b if it’s false I pass to the condition b > c (it is in fact assumed that a cannot be the greater):
- b > c if it is true, print the value b; otherwise it prints the value c.
a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))
c = int(input('Enter the third number: '))
if a > b:
if a > c:
print('The largest value is a: ', a)
else:
print('The largest value is c: ', c)
else:
if b > c:
print('The largest value is b: ', b)
else:
print('The largest value is c: ', c)
Let’s try it in the compiler:
Second solution max number of 3 numbers in Python
In the second solution I will use the logical operator and, in order to create a more streamlined structure. In fact, if a > b and a > c are both true then a is the greater; otherwise a is certainly not the largest and then just check if b is greater than c and based on this last condition, the largest is established.
So here is a possible solution:
a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))
c = int(input('Enter the third number: '))
if a > b and a > c:
print('The largest value is a: ', a)
else:
if b > c:
print('The largest value is b: ', b)
else:
print('The largest value is c: ', c)
Also try this solution in the compiler above.
Third solution to find the max number of 3 numbers in Python, with the use of elif
This solution is similar to the second one but uses the elif statement that we remember stands for else if.
a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))
c = int(input('Enter the third number: '))
if a > b and a > c:
print('The largest value is a: ', a)
elif b > c:
print('The largest value is b: ', b)
else:
print('The largest value is c: ', c)
Check of equality before finding the max
Finally I present a solution where I check if the numbers are all the same and thus display an appropriate message.
a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))
c = int(input('Enter the third number: '))
if a==b and b==c:
print('The numbers are the same')
elif a > b and a > c:
print('The largest value is a: ', a)
elif b > c:
print('The largest value is b: ', b)
else:
print('The largest value is c: ', c)
Fourth solution to find the max number of 3 numbers in Python, with the use of elif
Finally, here is a final solution that makes use of the max variable.
a, b, c = int(input('Insert the first number:')), int (input ('Insert the second number:')), int (input ('Insert the third number:'))
max = a
if b> max:
max = b
if c> max:
max = c
print('The largest value is:', max)
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
by Cristina | Jun 5, 2021 | Python
In this lesson we will write a Python program to find the maximum between two numbers, in order to put into practice what we have studied so far.
Python Program on the maximum of two numbers
Given two integers a and b, find the maximum value in Python. If the numbers are the same, also display the message: ‘the numbers are the same‘.
In particular, I propose a couple of solutions. In the first I use elif, while in the second I use nested if statement.
First solution with the use of elif
I present a very simple first solution with the use of the elif instruction to find the maximum of 2 numbers in Python:
a = int(input('Insert the first number:'))
b = int(input('Insert second number:'))
if a > b:
print('The largest value is a:', a)
elif a == b:
print('The numbers are the same')
else:
print('The largest value is b:', b)
Second solution with the use of nested if statement
I present a second solution, which is also very simple, with the use of the nested if statement to find the maximum of 2 numbers in Python:
a = int(input('Insert the first number:'))
b = int(input('Insert second number:'))
if a > b:
print('The largest value is a:', a)
else:
if a == b:
print('The numbers are the same')
else:
print('The largest value is b:', b)
Furthermore, thanks to multiple assignment, a topic I covered in the lesson on the exchange of values, the input values a and b can also be taken in this way:
a, b = int(input (‘Insert the first number:’)), int(input (‘Insert the second number:’))
This is just an example of a possible resolution of the algorithm for calculating the maximum between two numbers in Python, propose yours in the comments below.
Conclusion
In this lesson we have seen a simple algorithm to find the maximum between two numbers, in the next lessons we will see how to find the maximum between N numbers.
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
by Cristina | Jun 5, 2021 | Python
In this lesson I will give examples using nested if statement in Python.
Defining nestings means, in a nutshell, that you can insert ifs within other ifs or within the elif or even within the else.
Indentation – nested if statement in Python
What do you need to watch out for when using the nested if statement in Python? Definitely indentation.
Let’s take a trivial example to illustrate how to nest if statement in Python.
We take as input a number a, if it’s positive we display the message ‘a is positive‘, we also check if it is greater than 100 and if it’s true we display the message ‘and it’s also greater than 100‘. Otherwise, if a is not positive, we simply display the message ‘a is negative‘.
Here is a possible solution to the proposed algorithm:
a = int(input('Enter a positive or negative integer:'))
if a >= 0:
print('a is greater than zero')
if a> 100:
print('and is even greater than 100')
else:
print('a is negative')
We extend the problem by asking to also display the message that ‘a is not greater than 100’. To do this we need to add an else to the second if.
Here is a possible implementation of the code:
a = int(input ('Enter a positive or negative integer:'))
if a >= 0:
print('a is greater than zero')
if a> 100:
print('and is even greater than 100')
else:
print('a is negative')
else:
print('a is negative')
We add still more messages, for example if the number is negative and is even greater than -100.
Let’s add what was said in the previous code:
a = int(input('Enter a positive or negative integer:'))
if a >= 0:
print('a is greater than zero')
if a> 100:
print('and is even greater than 100')
else:
print('a is negative')
else:
print('a is negative')
if a> -100:
print('and is greater than -100')
elif a == - 100:
print('and is equal to -100')
else:
print('and is less than -100')
And so on. So in Python it is possible to use nested if, taking care to maintain correct indentation.
Having said that, however, the advice remains to use nested ifs in moderation.
These are just very simple examples of the use of nested if statement in Python, in the next lesson I will propose some useful exercises to better understand the concept.
Useful links
Python tutorial
Python Compiler
Install Python
Variables
Assignment operators
Strings
Casting
How to find the maximum of N numbers
Python
Bubble sort
Matplotlib Plot
by Cristina | Jun 5, 2021 | Python
The if elif else construct in Python allows the nesting of multiple if statements, i.e. if then otherwise statements.
In fact in the Python language to implement the multiple selection, this construct is used (for those who know other languages it is equivalent to the switch-case).
Syntax if elif else in Python
First of all, we note that the word elif is the abbreviation for else if, that is, otherwise if.
if condition:
instructions_if
elif condition:
instructions_elif
….
elif condition:
instructions_elif
else:
instructions_else
First example elif Python
So let’s create a first example of using the if elif else construct.
Create a program, which reads in input a vote represented by a character between A and E and then prints its meaning.
Let’s consider for example that:
A corresponds to excellent;
B corresponds to distinct;
C corresponds to discrete;
D corresponds to sufficient;
E it corresponds to insufficient.
First of all I take the vote as input and then I compare it with one of the letters to print the corresponding vote in output. If the letter does not match any of the default, an error message will be displayed.
So here is a possible solution that uses the if elif else construct in Python:
grade = input('Enter grade A, B, C, D or E:')
if vote == 'A':
print('The grade', grade, 'matches very good')
elif vote == 'B':
print('The vote', vote, 'matches distinct')
elif vote == 'C':
print('The grade', grade, 'matches fair')
elif vote == 'D':
print('The grade', grade, 'matches sufficient')
elif vote == 'E':
print('The grade', grade, 'matches insufficient')
else:
print('the letter entered does not correspond to any grade')
Second example elif Python
Let’s take another example to better understand the use of the construct.
On a railway line, compared to the full rate, pensioners benefit from a 10% discount, students 15% and finally the unemployed 25%.
Then, by coding pensioners with a P, students with an S and the unemployed with a D, write a program that, once the cost of a ticket and any condition of the user is requested, displays the amount to be paid.
First of all, we take the price and status as input, that is, if the user is unemployed, retired or a student.
Then just check the status and calculate the ticket price accordingly.
So here’s a possible resolution using the if elif else construct in Python:
price = float(input('Enter the ticket price:'))
state = input('Enter the state knowing that
D = Unemployed, P = retired, S = students:')
if state == 'D':
price = price * 75/100
elif state == 'P':
price = price * 90/100
elif state == 'S':
price = price * 85/100
else:
price = price
print('You belong to category', status, 'so you will pay:', price)
If you are already familiar with price = price * 75/100, write price * = 0.75 and so on.
Useful links
Python tutorial
Python Compiler
Install Python
Variables
by Cristina | Jun 5, 2021 | Python
In this lesson I propose some examples on the while in Python, in order to consolidate the argument on iterations.
First example on the while in Python
Input data 10 numbers count how many even and how many odd numbers have been entered.
To count the even and odd numbers we therefore use two variables. We call them, for example, contap and contap. Therefore, if we insert an even number we increase contap, otherwise we increase contap.
So the algorithm could be solved in this way:
i = count_e = count_o = 0
while i < 10:
n = int(input('Insert a number:'))
if n % 2 == 0:
conta_e + = 1
else:
count_o + = 1
i += 1
print('The even numbers entered are:', count_e)
print('The odd numbers entered are:', count_o)
Let's assume now that we want to exclude the number zero from the count of even numbers, simply for educational purposes, since in any case zero is considered an even number. How can we change the algorithm?
i = count_e = count_o = 0
while i < 10:
n = int(input('Insert a number:'))
if n != 0:
if n % 2 == 0:
conta_e += 1
else:
count_o += 1
i += 1
print('The even numbers entered are:', count_e)
print('The odd numbers entered are:', count_o)
Let's make a further modification by now counting how many numbers equal to zero have been entered.
i = count_d = count_p = count_z = 0
while i < 10:
n = int(input('Insert a number:'))
if n == 0:
count_z += 1
elif n % 2 == 0:
conta_e += 1
else:
count_o += 1
i += 1
print ('The even numbers entered are:', count_e, 'The odd numbers entered are:', count_o, 'The numbers equal to zero are:', count_z)
Second example on the while in Python
Calculate and view the 2 times table.
The first procedure that makes use of a single variable can be this:
i = 0
while i <= 20:
print(i)
i += 2
We initialized the variable i to 0 and increased it by two at each iteration until the condition i <= 20 is true.
But we can also think of another possible solution where we use a variable i that we multiply by two.
This variable increases for each operation by 1 until it reaches 10.
i = 0
while i <= 10:
n = i*2
print(2, 'x', i, '=', n)
i += 1
We could also define a constant value so as to easily modify the table.
i = 0
t = 2
while i <= 10:
n = i*t
print(t, 'x', i, '=', n)
i += 1
In this way, for example, if we wanted to create the table of 9, it would be enough to change t = 2 with t = 9.
In the next lesson we will cover yet more examples of the while loop in Python.
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