by Cristina | May 29, 2021 | Python
In this lesson I will do some examples of using the for loop with the list element in Python.
For list Python – Print a list in reverse order using the for loop
This problem can have several solutions, we will analyze some of them.
The first solution makes use of the reversed() function, to invert a list, the second one uses the length of the list, the len() function.
Let’s start by analyzing the first solution in detail.
First we create a list of elements: numbers = [1, 2, 3, 4, 5], then we use the existing function reversed() to reverse the elements of a list. So we print the inverted elements using the for loop in Python on the list.
Here is a possible solution:
numbers = [1, 2, 3, 4, 5]
numbers_reversed = reversed(numbers)
for number in numbers_reversed:
print(number)
Let’s now work out a second solution, calculating the length of the list from which we will subtract one. In this way we find the index of the last element of the list. Then we will use the appropriate for loop.
Here is the complete code of the second solution to our algorithm that uses the for on lists in Python:
numbers = [1, 2, 3, 4, 5]
n_numbers = len(numbers) - 1
for i in range (n_numbers, -1, -1):
print(numbers[i])
For list Python – Compute the cube of all elements of a list
Let’s create a list of integers. With the for loop we calculate the cube of each element and print the list. We use the len function to determine the length of the list. Finally we cycle through the modified list to print the elements.
numbers = [1, 2, 3, 4, 5]
for i in range(len(numbers)):
numbers[i] = numbers[i] * numbers[i] * numbers[i]
for number in numbers:
print(number)
A second solution involves simply using the function in Python to calculate the powers of a number, the pow() function.
In our case we will need to raise to the cube. Then we cycle through the list with the for loop in Python.
Here is the complete sample code:
numbers = [1, 2, 3, 4, 5]
for i in range(len(numbers)):
numbers[i] = pow(numbers[i],3)
for number in numbers:
print(number)
These are just some simple examples of using the for loop with lists in Python, in the next lessons we will develop many other examples.
Useful links
Tutorial Python
by Cristina | May 29, 2021 | Python
Integrate a Python Compiler Online to test the code directly.
Below you will find the basic version, scrolling the page you will find the compiler with the integration of some fundamental libraries for automatic calculation.
Basic version of the Python Compiler Online:
Write Python code using one of the most popular built-in compilers.
Python Compiler Online with Libreries
We can also use libraries like NumPy, Pandas, Matplotlib, Seaborn. Here is an advanced version of the Python Compiler with an example on the logarithmic scale:
About Python
Python is a very popular programming language created by Guido van Rossum. It was released in 1991 and has been increasingly adopted in a variety of fields since then.
Python, in fact, is a generic and high-level programming language widely used both for Web development and for the field of data science and machine learning. So it allows you to create mobile apps using cutting-edge frameworks such as Flask and Diango, and on the other hand it allows you to use libraries such as NumPy, Pandas, Matplot, Seaborn, etc …So we can say that sit is suitable both in the field of web development and in the analysis of data and automatic access.
Simple examples – Python Compiler Online:
Try some simple examples in the Python Compiler to test the programs.
First example:
Write a Python program using Pandas to create and display a one-dimensional array-like object containing an array of data.
Solution:
import pandas as pd
ds = pd.Series([1, 3, 5, 7, 9])
print(ds)
Second example:
Generate a sequence of numbers from x to y excluding y, in increments of 1.
x = 1
y = 15
for a in range(x,y):
print(a)
Third example to try in the Python online compiler:
Make a Python program that returns the total of odd and even numbers of series of numbers.
numbers = [11, 6, 9, 40, 5, 14]
count_odd = 0
count_even = 0
for x in numbers:
if x % 2:
count_odd += 1
else:
count_even += 1
print("Count even numbers :", count_even)
print("Count odd numbers :", count_odd)
Useful links
Python tutorial
1 – Introduzione al linguaggio Python
2 – Le variabili
3 – Operatori aritmetici e di assegnazione
4 – Stringhe
5 – Casting
6 – Input e print
7 – Primi esercizi in Python
8 – Errori in Python
9 – Script Python
10 – Scambio di variabili
11 – Modulo math