Py06: What IF I want to repeat FOR a WHILE?
Py06: What IF I want to repeat FOR a WHILE?
"First, solve the problem. Then, write the code."
- John Johnson
- John Johnson
Hello, if you've read the last article in the series, you should already know what problem we had discussed in past and even if not, the title of this article should give you some hint.
So, last articles solved the problem of declaring new variables for everything which is related in some manner, by using a list, set, and dict. But if you don't know about them, I would suggest to read it.
If you're reading this that means you already know about the list, set and dict type in Python. So, let's move forward with a question. Before moving on let's talk you about conditionals in Python. The language offers if-else statements for performing something only if a certain condition is true.
I would suggest to open a new file instead of working on the command line as it would be difficult to work there as the commands would be multiline statements. So, create a file named even_odd.py, the objective of this file would be to check if the number is even or odd.
Before starting let's learn one more function, input(). This function is used to take input from the command line. You could it as shown below:
>>> input_integer = int(input("Enter an integer here: "))
Enter an integer here: 5
>>> print(input_integer)
5
What we have done here? Let's break it down, the line, input("Enter an integer here: "), is our function. This function will print the prompt, which is completely optional, on the screen, where the user can input the number. But the number will not be of integer type implicitly, but it will be of string type. So, we've to convert the inputted string to an integer. So, we would pass the input string to function BIF int() which would cast it to an integer. Let's move on with our even_odd.py.
Now, let's decide how would we decide whether a number is even or odd? So, we know if a number is divisible by 2 i.e. input_integer % 2 == 0, remember % operator is used to find the modulus and == is used to check the equality to two variable.
A very important note:
Python control statements work only with proper indentation. Indentation is space preceded by the statement after the block is initiated and removed after the block is completed.
BLOCK ENTRY
BLOCK STATEMENTS
SUB-BLOCK ENTRY
SUB-BLOCK STATEMENTS
SUB-BLOCK EXIT
BLOCK STATEMENTS
BLOCK EXIT
Whatever text editor you're using configure it to set indentation using spaces instead of tabs because tabs might get interpreted as 2 whitespaces while the standard is of 4 whitespaces. So, I would recommend using 4 spaces instead of tabs.
Also, if you are on windows using notepad until now, stop. Notepad is not a good choice for programming. Switch to a better alternative. There are many other alternatives, please Google them.
But if you want a recommendation, I would strongly recommend Sublime Text 3. It is great, has a ton of features and I still find using it several times even when I have PyCharm IDE.
Many of you would like to try PyCharm, but I would recommend strongly against it, even though it is great, it would hinder your learning process and you would find a lot of trouble remembering the syntax after using it.
But I looked and really want to try it. No, you don't wanna try PyCharm. Never use an IDE until your completely comfortable with the language and can code any program without the use of an IDE.
Let's move on with Python Control Statements.
IF-Condition Statements
- if conditions are used to check a condition or check the boolean evaluation of an expression
- the condition is evaluated and if they are found to be true then the true statement is executed otherwise else statement is executed.
- the first condition precedes the if keyword and if there are multiple conditions then they are written after elif keyword, which is a short form of else-if.
- an else statement is also provided in case if no condition is found to true then, else statement is executed.
- the condition used should be a proper boolean statement, which gets evaluated in True or False or None
- None is the way of Python to tell that no present.
- the block statements are ended by a : (colon)
Looping Statement:
Every programming language has a functionality of looping statement. Looping statements are the statements which are designed to run for more than one time. Python has two types of looping statements, while loop and for loop.
Let's start with the while loop,
while Loop statement
- while loop also works on a condition
- while loop run the statements until a condition is satisfied.
- when while exit after executing the statements, it again checks for the condition and if find it to be false, it exits the loop,
- if the condition is found to be true the loop might run endlessly.
- the while statements are ended by a : (colon)
- while condition:
[True Statement]
for Loop statement
- for loop is used to iterate over a iterables
- Yes, you've read that word before.
- Iterables are those object which allows to run over.
- You can go to each element one-by-one during iteration.
- There are many iterable in python
- String, List, Tuple, Dictionary, Sets are the example of iterable Python allow to iterate.
- String gives it characters one by one while iterating
- Others also provide their elements while iterating
- for loop execute the statements after visiting each element in the iterables
- When the elements in iterables exhaust the for loop exits.
- the for statements are ended by a : (colon)
- for i in iterable:
[Statements to be executed]
Looping Control Keywords:
While you might be enjoying the looping, sometimes we also need to have control over them. So, we have two loop control keyword.
- break
- break statement exit the loop prematurely
- whenever the interpreter gets the keyword break, it actually breaks the loop and exits it
- it is very important to have an exit condition in infinite loops
- while condition_1:
statement_1:
if condition_2:
break
statement_2: - Here, if condition_1 if true, statement_1 will be executed. Then condition_2 will be checked and if it is found to be true the loop will break and statement_2 will not be executed. Otherwise, statement_2 will be executed.
- continue statement take the execution of the loop to the start
- it skips the following statements and continues the loop for next iteration
- Let's understand everything with an example of finding whether the number is prime or not.
The above link will take you to the prime number program. Read it, understand it and then perform the exercise
Try It Yourself:
Print all the armstrong numbers from 1 to 1000.
[EDIT]: The upper limit of the range is changed from 100 to 1000 because there are very few Armstrong numbers between 1 to 100.
Reference for armstrong numbers: Armstrong Numbers - https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html
[EDIT]: The upper limit of the range is changed from 100 to 1000 because there are very few Armstrong numbers between 1 to 100.
Reference for armstrong numbers: Armstrong Numbers - https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html
Happy Pythoning!!
Comments
Post a Comment