Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

Quantum Computing :Computational Thinking / Introduction to Coding

 LOOP

Believe it or not, you already know how to use a loop! Here is an example: Count from 0 to 10 by 2. The result is simple enough: 0, 2, 4, 6, 8, 10. What about counting by 3s? 0, 3, 6, 9. This is a loop! A loop tells you where to start, where to stop, and what size step to use.

There are 2 main types of loops:  1. for loops. This kind of loops use the the for instruction as their keyword and are normally used when we know exactly how many times we want to repeat executing the same block of code beforehand. We would describe it by saying "for every thing in our list, we are going to do X". 2. while loops. They use while as their keyword. When using while loops we may not exactly know how many times the loop needs to be repeated beforehand. The loop will be repeated until a certain condition is met. We would describe this by saying "while this statement is true, we are going to do X".   It is very important to close a loop! If a loop does not have a specific end point, or the condition is not met in a while loop, then the loop can run forever.   

Click on the link below to access the notebook used in the video. We encourage you to make a copy of it for yourselves and start working with loops practically.  

Make sure you can identify all the parts of the loop, and understand how both types of loops work.

 If: Conditional Statements


There will be times when we want to make decisions during executions of our code. For example, based on a student's grade we may want to print out "pass" or "fail". There is an instruction in Python and many other programming languages called "if" that makes the decision for us. Each "if" instruction has a conditional statement, as well as a code block. Whenever the execution flow of the program arrives at the "if" instruction, the conditional statement will be evaluated. If condition turns out to be true, its code block will be executed.  

You may notice a similarity between "if" and "while". Both check the if the condition is true before running the code block. However, there will be no repetition or iteration for "if". The conditional statement for "if" will be evaluated once; if it turns out to be true, the code block will be executed.  

For the "while" condition, once the condition is true, its code block will be repeated until the condition becomes false.  Make a copy of the below notebook and check out how the if statement is implemented. 

FUNCTION


Writing code is similar to writing an essay. The goal of an essay is to convey a particular idea or argument. In order to make that as clear as possible, there is a widely accepted structure of how you present this information. Essays are also supposed to be self-contained; you should be able to understand the purpose of the essay and its content without too many external supporting materials.   

Programming has many of the same features. There is a widely accepted way to structure code. Two key features of code structure are comments and functions. Comments are notes left by the author to explain what each part of the program does. These are written as "#" followed by text. For example: 

 #  this loop takes in a list of people's birthdays  

# it calculates their current ages in days  

# it returns a list of their current ages  

Functions are the other key feature of well-written code. Functions let us separate a piece of code and put a name on it so we can call it  whenever needed. This is just like a variable: rather than writing out the value of a variable every time, we name it so we can easy access that information. Besides avoiding repetition and making the code more readable, functions let us break the program into smaller parts and work on each part one at a time.   

 Try copying the file below and go through the code. Make sure you understand each line of the code, as well as the input and outputs of the function.   


Python programming has so much more to it than what we have covered, but this is enough to start writing Python programs. Whenever you faced a problem while coding, ask for help or search online. A good thing about a programming language like Python is that there are many resources available; the most common is Stack Overflow. 


Post a Comment

0 Comments