Tutorial: Variables, functions and simple List manipulation [Beginner]

This is going to be a pretty basic guide.
We are going to create a list of random numbers by utilizing the ListOfNumbers action which belongs in the Variables module.
After that we are going to find the sum total of all the numbers in the list, separate them to evens and odds and then add them to different lists.
We are also going to see what functions are and how we can use them to make our scripts more dynamic and intelligible.
Let’s begin.

First of all we create the Random numbers list.
We set the minimum/maximum possible value each number can get and how many numbers we want to generate.
image

By printing the list…
image

We get the following output (10 random numbers between 0 and 100):
image

Now we create a variable to store the sum of numbers and the two separate lists we are going to populate:
image

Time to write our first function…


Note: In programming, a named section of a program that performs a specific task.
In this sense, a function is a type of procedure or routine.
Some programming languages make a distinction between a function, which returns a value
and a procedure, which performs some operation but does not return a value.


Our function here has two input parameters, num1 and num2, adds them and calculates the sum total which is stored in the ‘sum’ variable we declared at the top of our example:
image

Now it is time for our second function.
This function is going to populate the evenList and oddList lists by checking each number from the first list.
The checking mechanism is really simple, modulo utilization (we divide numbers by two and if remainder is greater than 0 then the number is odd).

We can now utilize our two functions.

Now let’s sort our two new lists in ascending order just for fun:
image

And print all of the results:
image

The final output should look like this:
image


A screenshot of the whole script:


Make sure to check the following topics from the documentation:

Github link

Best regards,
J.


James Papadimitriou
Technical Evangelist

3 Likes

Very clear and concise, @jpap, thanks!
Regards,
burque505

1 Like