create account

Grammar of a programming language - A look into Python (Part 2) by rahim.rahman

View this thread on: hive.blogpeakd.comecency.com
· @rahim.rahman ·
$0.03
Grammar of a programming language - A look into Python (Part 2)
<html>
<p><img src="https://i.imgsafe.org/e2/e2ee102fec.png" width="601" height="203"/></p>
<h2>Writing up the codes within a new File</h2>
<p>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 <strong>File</strong> menu and choose <strong>New File</strong>. 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.</p>
<p><img src="https://i.imgsafe.org/ff/ff42458a6c.jpeg" width="805" height="372"/></p>
<p>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:</p>
<p><img src="https://i.imgsafe.org/01/01257a17e4.jpeg" width="361" height="254"/></p>
<p>Once you have finished typing the code, you need to choose <strong>Save As</strong> from the <strong>File</strong> 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 <strong>test1.py</strong>. An important point to take note here is that; ensure that <strong>.py </strong>extension is typed when clicking the save button. Otherwise, the file may not run properly.&nbsp;</p>
<p>Now, lets run the file by clicking the <strong>Run Module</strong> within the <strong>Run</strong> menu button. The output of this file will be displayed in the main menu of IDLE. For this example, you will get this output:</p>
<p><img src="https://i.imgsafe.org/01/018f50c513.jpeg" width="675" height="292"/></p>
<h2>Code Layout</h2>
<p>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 <strong>colon</strong> <strong>: , </strong>subsequent new line of code must have spaces before they begin. Typically, this will be <strong>4 spaces or 1 tab</strong>. Typical cases that require colon are such that when you have to write the following commands in your code:</p>
<ol>
  <li>If statement</li>
  <li>While loop</li>
  <li>For loop</li>
  <li>Defining a function</li>
  <li>Defining a class</li>
</ol>
<p>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.</p>
<h3><code>If Statement</code></h3>
<p>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 <strong>if, </strong>which is then<strong> </strong>followed by an <strong>expression, </strong>and subsequently a <strong>colon</strong>. Following that, you will need to write the body of code on a new line with spaces/tab. Otherwise, it will not work.&nbsp;</p>
<p>For this example, we will define two integers which are 10 &amp; 5. Let's define them by letting a=10 &amp; b=5. We are using if statement to test whether <strong>a</strong> is either bigger, smaller or equal to <strong>b</strong>. 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 <strong>if</strong>. Any nested if statement will be written as <strong>elif</strong>, and the final if condition will be written as <strong>else</strong>. Lets try it out in Python:</p>
<p><img src="https://i.imgsafe.org/02/029e3db780.jpeg" width="486" height="228"/></p>
<p><img src="https://i.imgsafe.org/02/029ea6bc90.jpeg" width="154" height="33"/></p>
<p>As expected, the number 10 is bigger than 5. Hence, the print statement underneath the if a&gt;b line is displayed in main IDLE window.</p>
<p><code><strong>While loop</strong></code></p>
<p>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.</p>
<p>Lets use a=10 &amp; b=5, just like in previous example. We are going to<strong> subtract 1</strong> from <strong>a</strong> until it is no longer bigger than <strong>b</strong>. 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:</p>
<p><img src="https://i.imgsafe.org/03/035604208d.jpeg" width="506" height="184"/></p>
<p><img src="https://i.imgsafe.org/03/035629d98c.jpeg" width="63" height="104"/></p>
<p>As you can see, the print statement stops displaying <strong>a</strong> at 5. This is because the while statement is no longer true when a=5. &nbsp;</p>
<p><code><strong>For loop</strong></code></p>
<p>Another way to perform repetitive thing is by using a<strong> for loop</strong>. 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 <strong>nested if statement</strong> which have a <strong>break</strong> command. In other words, it's like saying; if <strong>a</strong> is no longer bigger than <strong>b</strong>, stop the while loop. The codes will be as followed:</p>
<p><img src="https://i.imgsafe.org/0b/0b372b8372.jpeg" width="389" height="194"/></p>
<p><img src="https://i.imgsafe.org/0b/0b3742cf9f.jpeg" width="50" height="92"/></p>
<p>As you can see, we can achieve the desired result equally with a for loop or a while loop.&nbsp;</p>
<p><code><strong>Function</strong></code></p>
<p>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:</p>
<ol>
  <li>Start with the word <strong>def</strong>.<strong> </strong>This is a way to tell Python that we are going to create a function.</li>
  <li>Followed by the <strong>function name</strong>. You can name whatever your want.</li>
  <li>Then, you need to specify the <strong>required inputs</strong> for the function .</li>
  <li>On a new line with spaces/tab, write the <strong>body</strong> of the function.</li>
  <li>Finally, you can return the desired <strong>output</strong>.</li>
</ol>
<p>It is best to demonstrate this with an example and here it goes:</p>
<p><img src="https://i.imgsafe.org/0c/0c8e570272.jpeg" width="580" height="208"/></p>
<p><img src="https://i.imgsafe.org/0c/0c8e69f3c5.jpeg" width="149" height="66"/></p>
<p>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 &amp; 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.</p>
<p>That's all from me for this post. Do let me know what do you all think. I'll gladly write more posts soon.</p>
<p>Cheers,</p>
<p>@<a href="https://steemit.com/@rahim.rahman">rahim.rahman&nbsp;</a></p>
</html>
👍  , , , ,
properties (23)
authorrahim.rahman
permlinkgrammar-of-a-programming-language-a-look-into-python-part-2
categorypython
json_metadata{"tags":["python","programming","ifstatement","forloop","whileloop"],"image":["https://i.imgsafe.org/e2/e2ee102fec.png","https://i.imgsafe.org/ff/ff42458a6c.jpeg","https://i.imgsafe.org/01/01257a17e4.jpeg","https://i.imgsafe.org/01/018f50c513.jpeg","https://i.imgsafe.org/02/029e3db780.jpeg","https://i.imgsafe.org/02/029ea6bc90.jpeg","https://i.imgsafe.org/03/035604208d.jpeg","https://i.imgsafe.org/03/035629d98c.jpeg","https://i.imgsafe.org/0b/0b372b8372.jpeg","https://i.imgsafe.org/0b/0b3742cf9f.jpeg","https://i.imgsafe.org/0c/0c8e570272.jpeg","https://i.imgsafe.org/0c/0c8e69f3c5.jpeg"],"links":["https://steemit.com/@rahim.rahman"],"app":"steemit/0.1","format":"html"}
created2017-09-07 04:35:03
last_update2017-09-07 04:35:03
depth0
children3
last_payout2017-09-14 04:35:03
cashout_time1969-12-31 23:59:59
total_payout_value0.022 HBD
curator_payout_value0.003 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8,044
author_reputation3,703,224,567
root_title"Grammar of a programming language - A look into Python (Part 2)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,113,388
net_rshares8,565,099,790
author_curate_reward""
vote details (5)
@phil-coding ·
Your post is really good!! :D And in general very helpfull information Thank you!!
👍  ,
properties (23)
authorphil-coding
permlinkre-rahimrahman-grammar-of-a-programming-language-a-look-into-python-part-2-20170907t101242377z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2017-09-07 10:14:18
last_update2017-09-07 10:14:18
depth1
children1
last_payout2017-09-14 10:14:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length82
author_reputation4,773,758,818
root_title"Grammar of a programming language - A look into Python (Part 2)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,135,457
net_rshares2,193,573,263
author_curate_reward""
vote details (2)
@rahim.rahman ·
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.
👍  
properties (23)
authorrahim.rahman
permlinkre-phil-coding-re-rahimrahman-grammar-of-a-programming-language-a-look-into-python-part-2-20170907t105321346z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2017-09-07 10:53:27
last_update2017-09-07 10:53:27
depth2
children0
last_payout2017-09-14 10:53:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length153
author_reputation3,703,224,567
root_title"Grammar of a programming language - A look into Python (Part 2)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,138,155
net_rshares1,143,213,104
author_curate_reward""
vote details (1)
@steemitboard ·
Congratulations @rahim.rahman! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstcommented.png)](http://steemitboard.com/@rahim.rahman) 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](https://steemit.com/@steemitboard)

If you no longer want to receive notifications, reply to this comment with the word `STOP`

> By upvoting this notification, you can help all Steemit users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
👍  
properties (23)
authorsteemitboard
permlinksteemitboard-notify-rahimrahman-20170907t224317000z
categorypython
json_metadata{"image":["https://steemitboard.com/img/notifications.png"]}
created2017-09-07 22:43:15
last_update2017-09-07 22:43:15
depth1
children0
last_payout2017-09-14 22:43:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length693
author_reputation38,975,615,169,260
root_title"Grammar of a programming language - A look into Python (Part 2)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,195,489
net_rshares899,482,391
author_curate_reward""
vote details (1)