Thursday

Learn Python Easy way - Example 3 - Find smallest and largest of given numbers

Let us create a easy example in Python to find out smaller of the two numbers. We will ask the user to input two numbers and then compare them and print out the result.

I have given my logic below, you can do the same program in different ways. Try to make changes to the following code if you have a different algorithm in mind

the first two print statements ask the use to enter one number at a time, and press Enter after each input.
number1 and number2 are integer variables. Here we declare the variables and assign them in the same line. int(input()) is the input() function which waits for the use input, and collects the input as an integer and stores that in the variable number1 and number2 respectively.

Now we will look at the if - elif - else construct. Notice that there are no brackets or parenthesis after if - elif - else. It is not required in Python. However we need to add a Colon : to mark the end of the if - elif - else condition. And the statement follows in the next line with due amount of indentation.
 Indentation serve the same purpose as the brackets or the parenthesis would.

Now to make some coding practice, let us extend the same program to intake three numbers form the user and find out the smallest of them all.




Now let us see a program that will ask the user how many numbers he/she wants to enter and then collect all those numbers and tell the smallest and the largest of them.

This program shows how you can create a useful program in Python with just a few lines of code.



Next

Featured Post

Creating Games in Scratch for ICSE Class 6

An Introduction to Creating Games in Scratch  for ICSE Class 6 Hello friends, I hope you have already read the previous post on An Int...