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.

By printing the list…

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

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

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:

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:

And print all of the results:

The final output should look like this:

A screenshot of the whole script:

Make sure to check the following topics from the documentation:

Github link

Best regards,

James Papadimitriou
Technical Evangelist


Very clear and concise, @jpap, thanks!

1 Like