Basics of Python — Part III

Vijay Gadre
Geek Culture
Published in
6 min readOct 21, 2021

--

LISTS

A List is a type of sequence of data points such as floats, integers, or strings.

You can access the Participants list by indexing the value 1. This means you have extracted the second of the elements in this list variable [‘Leila’].

In addition, there is a way to get to the last element from your list — start counting from the end towards the beginning. Then, you’d need the minus sign before the digit and don’t fall in the trap of thinking we begin enumerating from 0 again! To obtain “Care”, you have to write -1.

HELP YOURSELF WITH METHODS

Here is the syntax that allows you to call ready-made built-in methods that you do not have to create on your own and can be used in Python directly.

After the name of the object, which in this case is the “Participants” list, you must put a dot called a dot operator. The dot operator allows you to call on or invoke a certain method. To call the method “append”, state its name, followed by parentheses.

To insert the name “Dwayne” in our list, you must put the string “Dwayne” in inverted commas between the parentheses.

Alternatively, the same result can be achieved by using the “extend” method. This time, within the parentheses, you’ll have to add brackets, as you are going to extend the “Participants” list by adding a list specified precisely in these parentheses.

LIST SLICING

Many of the problems that must be solved will regard a tiny portion of the data, and in such cases, you can apply slicing.

Imagine you want to use the “Participants” list to obtain a second much smaller list that contains only two names — Leila and Maria. In Pythonic, that would mean to extract the elements from the first and seconds position. To access these elements, we will open square brackets, just as we did with indexing, and write 1 colon 3. The first number corresponds precisely to the first position of interest, while the second number is one position above the last position we need.

TUPLES

Tuples are another type of data sequences, but differently lists, they are immutable. Tuples cannot be changed or modified; you cannot append or delete elements.

The syntax that indicates you are having a tuple and not a list is that the tuple’s elements are placed within parentheses and not brackets.

The tuple is the default sequence type in Python, so if you enlist three values here, the computer will perceive the new variable as a tuple. We could also say the three values will be packed into a tuple.

For the same reason, you can assign a number of values to the same number of variables. On the left side of the equality sign, add a tuple of variables, and on the right, a tuple of values. That’s why the relevant technical term for this activity is tuple assignment.

DICTIONARIES

Dictionaries represent another way of storing data.

Each value is associated with a certain key. More precisely, a key and its respective value form a key-value pair. After a certain dictionary has been created, a value can be accessed by its key, instead of its index!

Similarly, as we could do with lists, we can add a new value to the dictionary in the following way: the structure to apply here is dictionary name, new key name within brackets, equality sign, and the name of the new value.

FOR LOOP

Iteration is a fundamental building block of all programs. It is the ability to execute a certain code repeatedly.

The list “even” contains all the even numbers from 0 to 20. “for n in even”, colon, which would mean for every element n in the list “even”, do the following: print the element.

The command in the loop body is performed once for each element in the even list.

WHILE LOOPS

The same output we obtained in the previous lesson could be achieved after using a while loop, instead of a for loop. However, the structure we will use will be slightly different.

Initially, we will set a variable x equal to zero. And we’ll say: while this value is smaller than or equal to 20, print x.

We want to get the loop to the end. What is supposed to succeed, the loop body in the “while” block, is a line of code that specifies a change in x or what has to happen to x after it is printed. In our case, we will tell the computer to bind x to a value equal to x + 2.

In programming terms, adding the same number on top of an existing variable during a loop is called incrementing. The amount being progressively added is called an increment. In our case, we have an increment of 2.

The Pythonic syntax offers a special way to indicate incrementing: x += 2

LIST WITH RANGE FUNCTION

When you need to randomize data points and lists with data points, you can use Python’s built-in range function.

The syntax of the function is the following: —

The stop value is a required input, while the start and step values are optional. If not provided, the start value will be automatically replaced with a 0, and the step value would be assumed to be equal to 1.

range(10) will provide a list of 10 elements, starting from 0, implied after not indicating a start value, and ending at the tenth consecutive number — 9.

CONDITIONAL STATEMENTS AND LOOPS

You create an iteration that includes a conditional in the loop body. You can tell the computer to print all the even values between 0 and 19 and state “Odd” in the places where we have odd numbers.

Let’s translate this into computational steps.

If x leaves a remainder of 0 when divided by 2, which is the same as to say “if x is even”, then print x on the same line. “Else”, which means unless x is even, or if x is odd, print “Odd”.

--

--

Vijay Gadre
Geek Culture

Data Scientist | Machine Learning Engineer | Artificial Intelligence Engineer