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.
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.
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.
If you want to see the code here is the link to Github:
https://github.com/Jocapear/TC1014/blob/master/WSQ10.py
So the code will look like this:
#include <iostream> #include <math.h> using namespace std; float aver(float x){ return (x/10); } int main (){ float array1[10]; float suma=0; float square=0, deviation; for (int i=0; i<10; i++) { cout<<"Please introduce the value number "<<(i+1)<<" : "; cin>>array1[i]; } for (int i=0; i<10; i++) { suma+=array1[i]; square+=(array1[i])*(array1[i]); } deviation=sqrt(square/10); cout<<""<<endl; cout<<"The addition is equals to: "<<suma<<endl; cout<<"The average of the numbers you introduce is: "<<aver(suma)<<endl; cout<<"The standard deviation of the numbers you introduce is: "<<deviation<<endl; return 0; }
And the execution will be as the following:
Once more, thank you for your attention and as always I will be leaving the codes on my GitHub account.
Remember to keep practicing!
-The Admin
]]>So the code will look like this:
#include <iostream> #include <math.h> using namespace std; float aver(float x){ return (x/10); } int main (){ float array1[10]; float suma=0; float square=0, deviation; for (int i=0; i<10; i++) { cout<<"Please introduce the value number "<<(i+1)<<" : "; cin>>array1[i]; } for (int i=0; i<10; i++) { suma+=array1[i]; square+=(array1[i])*(array1[i]); } deviation=sqrt(square/10); cout<<""<<endl; cout<<"The addition is equals to: "<<suma<<endl; cout<<"The average of the numbers you introduce is: "<<aver(suma)<<endl; cout<<"The standard deviation of the numbers you introduce is: "<<deviation<<endl; return 0; }
And the execution will be as the following:
Once more, thank you for your attention and as always I will be leaving the codes on my GitHub account.
Remember to keep practicing!
-The Admin
]]>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
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
]]>