Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152
‘#Mastery20’ Articles at TC101 Fall 2015, Page 4
Introduction to Programming Python and C++

Tag Archives: #Mastery20

Loops 2.0 #Mastery20

This post will talk about the for loop within a program, as requested by the mastery 20. 

The for loop is used for the same task as the while loop, but its difference radicates in their notation. The notation of the for structure includes an inicialization, an accumulator and the condition; which difffers from the while loop which only asks for the condition. The structure for  the for loop is as follows:

for (initialization; condition; accumulator){

Instructions;

}

First of all, the initialization could be filled with the initialization of a value (int a= 0), a value previously initialized (it could be left blank if the initialization has been done before). Afterwards, the condition should return a boolean value, which could be done by a comparison (==, !=,) or by explicitely typing it as a true or false, which might create an infinite loop or a non used code, respectively. Finally the accumulator would be the increase or decrease of the value initialized, which could be a++ or a–. If the accumulation is done within the instructions, it could be left blank. 

Firstly, the initialization is done, afterwards the condition is tested. If false, the for ends and further instructions are executed. If true, the instructions within the loop are executed once, the accumulation is done and the condition is tested again for further execution.

In case you wish to stop the flow of a loop or avoid an infinite loop, a break command would be useful. A break stops the flow of the loop and jumps to execute the following instructions.

Here’s an image of a code for a succesion of numbers with a for loop and a break which stops the succesion:

 

Masteries 20 and 23 – Loop for and creation and use of lists

I want to start explaining the lists because to use the loop for you have to know how to create lists. First of all, a list is a variable that countains several values, they can be integers or strings. The list can be differentiated from a common variable because they use “[]” and the values are separated from commas. Here is an example:
friends = [“maria”,”pepe”,”juan”,”roberto”]

creating lists

There are two ways of creting a list:
this_is_a_list = list()
this_is_a_list = []
Lists are created in the same way as a variable, but here you can choose for giving the variable a value of a list function or the value of an empty list.

using lists

If you want to take just one value of the list you have to use the variable with the position you want to take the value from. Let´s take the list of friends as an example, if i want to print “juan” i have to write this:
print (friends[2])
juan
As you can see “juan” is the third element on the list but i wrote 2. That happens because Python takes the first element as a 0, the second as a 1 and go on. If i want to take the fifth element on the list i have to write 4 because we are also counting the 0. If you want to add elements to the list you can use .append, it push the list one slot more and then add the value you want. For example if i want to add a new friend in my friends list:
friends.append(“jose”)
print (friends)
[maria,pepe,juan,roberto,jose]
Notice that i write .append right next to the list name and the put the value i want to add inside parenthesis.
There is also a way to count the number of elements inside a list. Is a function called len from lenght and is very easy to use. The function returns the number of elements and you can use it to do calculations. Let´s see how many friends i have:
print (len(friends))
5
You could also assign that value to a variable and use it later on.
There are a lot of more uses for a list but im not going to get to deep into it. This are the most common and useful but if you want to learn some more be sure to read the course book.

for loop

The for loop is a packed version of a while loop. The difference between a for loop and a while loop is that the for loop works with an iterator (a counter that works automatically), it works with lists and ranges (and more), and also the condition does not returns a boolean value . This is an example of the for loop:
for i in friends:
     print (i,”is my friend”)
maria is my friend
pepe is my friend
juan is my friend
roberto is my friend
jose is my friend
The “i” in the for loop works exactly as a function, it takes a value from somewhere and sustitute that value inside the block. The condition is to take all the values of the list friends. The “i” is going to take the value from the first element and then sustitute that value inside the print function, when it ends it will move to the next value until the list is over.
Another way to use the for loop is to use a range. Instead of using a list as a condition we can tell “i” to take the value from a range of numbers. This is an example of a for loop with a range:
for i in range (1,3):
   print (i)
1
2
The loop printed only 1 and 2 but not 3 because the range is exclusive, this means that does not include the last number. It takes all the values from 1 to 3 without incluiding the 3, its like saying 1<=i>3 (“i” is greater or equal than 1 and less than 3 but not 3). Always add 1 to your real range because the loop is not going to take the last value, this is very important to undertand because by knowing you could avoid a lot of mistakes and hours of trying to fix a program.
You should also know that the “i” can be replaced by any other letter you want as you include it inside the loop.
There are some more iterators but these are the most common and useful for us. If you want to know more about this, you should read the course book.

Use of loops with “for”

Lo dicho es ejecutado repetidamente mientras la condición resulte verdadera o hasta que la evaluación de la condición resulte falsa. Iniciación del loop, declararlo, condiciones iniciales del loop (número de repeticiones). Condición, que establece la condición de salida (hasta cuando se… Continue Reading →

#Mastery20

Use of loops with “for”

MASTERIES #19 & #20: Use of loops with “while” and “for”

  (The credit of the background images goes to https://flic.kr/p/dAus13 and https://flic.kr/p/hiFuew ) So this are Masteries #19 & #20 and I’m going to tell you about the loops while and for. Let me tell you first that a loop is “is a way of repeating a statement a number of times until some way of ending the […]

#Mastery20 – Use of loops with “for”

Hey! Here is my on how to use loops with for. Loops are used to repeat a group code as many times as wanted. This is one of the basic and most useful tasks in c++. A loop permits you to do a very small and simple task and summing up it gets into up to a big and complex task.

 

How to write a for loop?

 

The structure of a for loop is the following:

for (variable; condition; variable update) {

Things to do when the condition is true

}

 

Notice that in the true part (in between parenthesis) the second section, tells the program that while the conditional expression is true the loop should continue to repeat itself. It is very important for the conditions to be separated in parenthesis as we see en the example before.

 

For example, lets do a program in which we make use of this loop.

 Hey! check out this page I found about usual problems programmers usually have when coding.

http://www.cprogramming.com/beginner_programming_mistakes.html

 

96

Normal
0

21

false
false
false

ES-TRAD
X-NONE
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:”Tabla normal”;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:Calibri;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-fareast-language:EN-US;}

 WATCH MY VIDEO 

#Mastery20 and #Mastery23

Hey today we are going to learn how to : Use of loops with “for” Creation and use of lists in Python #Mastery20 : #Mastery23 : Have a nice day !

WSQ07 – Sum of Numbers

The program asks for a range of integers, asking the user to give the lower and upper value of the range and then prints the sum of the numbers in that range in an inclusive manner. The new code I used for this assignment was range(), lists and a for- loop. Range() is a function that, […]

The loops: For and while masteries 19, 20

With this program the conditional loop while and the loop with range of values are executed; te program basicaly asks for a number to make the factorial of it with the For, yet it does not allows you to type… Continue Reading →

What should you work on?

Week #12 and more partial exams for you.

For this week's readings:
C++ (TC1017) should either be looking at support for your project, ImageMagick C++ libraries are a good start.
Python (TC1014) should be finishing chapter 11 (Dictionaries).