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
‘#Lists’ Articles at TC101 Fall 2015
Introduction to Programming Python and C++

Tag Archives: #Lists

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.

WSQ10 – Lists

This is the program working. I took some code from Paul´s blog https://pololarinette.wordpress.com/2015/10/15/wsq10-lists/ because i had some issues understanding the formula of the standart deviation so what i did was o import a library that already included the formula and i used it for the calculation.

If you want to see the code here is the link to Github:
https://github.com/Jocapear/TC1014/blob/master/WSQ10.py

Lists!

For this weekly WSQ which is #10 We will be using  arrays in order to create a program that asks the user for 10 numbers  (floating point). Store those numbers in a list. Show to the user the total, average… Continue Reading →

Lists!

For this weekly WSQ which is #10 We will be using  arrays in order to create a program that asks the user for 10 numbers  (floating point). Store those numbers in a list. Show to the user the total, average… Continue Reading →

Roll the DIEZI read the book and looked at some websites to…

Roll the DIEZ

I read the book and looked at some websites to complete my understanding about vectors and arrays.

Websites:

 http://www.cplusplus.com/reference/list/list/

https://www.programarya.com/Cursos/C++/Estructuras-de-Datos/Arreglos-o-Vectores

Code

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).