Python Examples-2

Sevda Sanver
4 min readJun 2, 2022

--

Hi there! 🙌
I am so excited. Now, it is our time to gain hands-on experience and reinforce our Python knowledge.

Yes, we have a long way to go.
But we can do it!
We will develop our skills, and become way better over time if we keep coding and never give up.

Okay. Let’s start with our first question.

Get a number from the user. Find the integers between 0 and that number, and then find the square of these integers. For instance, if the user enters 5; the output should be displayed as 0, 1, 4, 9, 16.

Until the user enters ‘q’, get number(s) from the user. Find the sum of all numbers. Then print the biggest, smallest ones. Lastly, find the average of all values.

Did you see “while True” statement in line 4? It creates an infinite loop. But why did we use it? Any idea?
As we read in the question, we need to ask the user to enter a new input until the user enters q. If the user does not, it will ask for a new number forever. When she enters q, the code will read “break” command and exit from the loop.
We did not convert “userInput” to integer in line 5, because if we did, we would see an error when the user enter q. Therefore, we did it in the 9th line.

Another point that I want to underline;

The first time we run the code, biggest and smallest variables would be None because we assigned “None” to them at the beginning of the code.
We also used “None” in lines 11 and 13 because if we did not assign anything to them, when the if statement part is executed, userInput could not be bigger than biggest or smaller than smallest. In order to execute if statement at the first run, we needed to do that.

New question!
Get a number from the user and find its factorial. Let’s use both while and for loops and see some variations on how to write code. All of them do the same thing, and get the same result.

Get a number and find its aliquots which means the values that can divide the input number.

Do you like Algebra? Let’s go back to our high school times and remember what was Greatest Common Factor (GCF) and Least Common Multiple (LCM).

Quick reminder:
GCF is the largest number that divides evenly into each number in a given set of numbers. LCM is the smallest positive multiple that is common to two or more numbers.

Now we are ready to solve our next question!
Find the GCF and LCM of 2 given numbers.

Before the next question, I would like to underline two things. We can find min value of 2 numbers with a couple of ways. We can use min() function, or compare the numbers with if-statements. Also, we can write it in different ways as seen in line 4, 6, or 8.

Another thing that I would like to add is about the lines between 20 and 26.
If it confuses you, you always can make some table like me while thinking about your next code line. Yeap, you can write it down as a command line. Seeing what you are doing would make everything way clear and easier.

And here is our last question.
Print every 5 numbers from 100 to 200 such as 100, 105, 110 etc.
Skip the 4th,8th,12th,16th… ones.

Well done, well done. You made it.

Keep in mind; today is your day to do better than you did yesterday.

Keep coding! Never ever give up!

I am one of the participants of the SistersLab’s Women in Tech Academy which is supported by the organization “Toplum Gönüllüleri Vakfı” (https://www.tog.org.tr/en/). The project aims to empower 25 women with software training and support them to participate in the workforce in the IT sector. You can find more information about the project here : https://sisterslab.co/women-in-tech-academy/

--

--