Writing up the codes within a new File
Up until now, we have only been using IDLE's main window to write codes. This is totally fine for simple tasks but will be a nuisance once the codes are long and complex. To tackle this issue, a more practical approach would be to write it in a new window where we can edit, save our codes and open it later whenever we want. Thank goodness IDLE has this feature. To begin with, click the File menu and choose New File. Once you do that, a new window will pop up (refer image below) where you can write codes in this new window instead in the main window of IDLE.
Let us now write simple codes in the new window just as a try out. I'll be writing the following codes using what we have learnt so far:
Once you have finished typing the code, you need to choose Save As from the File menu. Choose the folder that you want to place the file and name the file according to your preference. For this example, I have named my file as test1.py. An important point to take note here is that; ensure that .py extension is typed when clicking the save button. Otherwise, the file may not run properly.
Now, lets run the file by clicking the Run Module within the Run menu button. The output of this file will be displayed in the main menu of IDLE. For this example, you will get this output:
Code Layout
So far, we have been writing our codes line by line without worrying about spaces. There will be times when spaces are necessary when writing Python codes. The rule is whenever a line of code requires a colon : , subsequent new line of code must have spaces before they begin. Typically, this will be 4 spaces or 1 tab. Typical cases that require colon are such that when you have to write the following commands in your code:
- If statement
- While loop
- For loop
- Defining a function
- Defining a class
I will try to explain as simple/brief as I can for No.1 till No.4 without causing any confusion. For No.5, it is quite similar to No.4, so I'll skip it.
If Statement
The if statement if useful when you want to know whether an expression is true or not. For example, does 2 is bigger than 3? (Something like that) When writing an if statement, the syntax starts with an if, which is then followed by an expression, and subsequently a colon. Following that, you will need to write the body of code on a new line with spaces/tab. Otherwise, it will not work.
For this example, we will define two integers which are 10 & 5. Let's define them by letting a=10 & b=5. We are using if statement to test whether a is either bigger, smaller or equal to b. The if statement has to be implemented 3 times as there are 3 conditions we are trying to test for. The first if statement always starts with if. Any nested if statement will be written as elif, and the final if condition will be written as else. Lets try it out in Python:
As expected, the number 10 is bigger than 5. Hence, the print statement underneath the if a>b line is displayed in main IDLE window.
While loop
One way to do repetitive thing is by performing a while loop. Supposed if we want to perform addition or subtraction to a number until it reaches certain value, we can definitely do it with a while loop. The loop should keep going until the while statement is no longer true.
Lets use a=10 & b=5, just like in previous example. We are going to subtract 1 from a until it is no longer bigger than b. Since this is a repetitive task, a while loop can be used to solve this. As in if statement, a while loop requires a colon and spaces/tab too. The while loop will look like this:
As you can see, the print statement stops displaying a at 5. This is because the while statement is no longer true when a=5.
For loop
Another way to perform repetitive thing is by using a for loop. The difference between a for loop and a while loop is that, a for loop is used when you know the number of times you want to repeat performing the line of codes. Let say we want to perform the exact same task as in the while loop example, which is to subtract a=10 until it becomes 5. We will need to specify how many times the subtraction will need to be performed. Let say we put it as 10 times. But then, subtracting the number of 10, 10 times with 1 will make it equal to 0. To overcome this issue, we will need to break out from the while loop once the target is already achieved. To do this, we will put a nested if statement which have a break command. In other words, it's like saying; if a is no longer bigger than b, stop the while loop. The codes will be as followed:
As you can see, we can achieve the desired result equally with a for loop or a while loop.
Function
Up to now, we have been executing all of our lines of codes when we run the .py file. If we plan to only run a portion of the codes, we can separate the block of codes into functions and later on choose the function that we intend to run. For simplicity, lets create 2 functions where each function can add and subtract 2 numbers independently. The methods/rules for creating a function is as followed:
- Start with the word def. This is a way to tell Python that we are going to create a function.
- Followed by the function name. You can name whatever your want.
- Then, you need to specify the required inputs for the function .
- On a new line with spaces/tab, write the body of the function.
- Finally, you can return the desired output.
It is best to demonstrate this with an example and here it goes:
Once you run the .py file, nothing will appear in the main IDLE window. The user has to choose which function that needs to be executed. Let say we want to add 4 & 3. Just type add(4,3) in the IDLE and press enter. Similarly, you can type subtract(4,3) if you want to subtract 3 from 4. It is as simple as that.
That's all from me for this post. Do let me know what do you all think. I'll gladly write more posts soon.
Cheers,
Your post is really good!! :D And in general very helpfull information Thank you!!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hi there. Thank you very much for the feedback. I hope my posts are able to let others to learn & understand coding in a simple and not-so technical way.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations @rahim.rahman! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You got a First Reply
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit