Python prime numbers from 2 to N

Python prime numbers

In the previous Python lesson we studied prime numbers, here is the link of the lesson. Now we will develop a Python algorithm for prime numbers from 2 to N. Python prime numbers from 2 to N Write a Python program that determines prime numbers from 2 to N. Let’s start thinking about a possible … Leggi tutto

indefinite loop Python

indefinite loop Python

In this lesson we will study indefinite loop in Python, that is loops where the number of iterations is not known at the beginning of the loop. Let’s take some examples to better understand what is meant by an indefinite cycle. Indefined loop in Python – first example Insert integers and add them, exit the … Leggi tutto

Friendly numbers

In this lesson we will make a program on friendly numbers in Python, in order to practice with iterative structures. Recall that two numbers are said to be friendly if the sum of the divisors proper to one, but excluding the number itself, is equal to the other number and vice versa. For example, numbers … Leggi tutto

Euclidean algorithm Python

Euclidean algorithm Python

In this lesson we will develop the Euclidean algorithm in Python. Euclid’s algorithm is a method used to find the greatest common divisor between two integers. By greatest common factor, GCD, between two integers we denote the greatest common divisor of both. Euclidean algorithm consists in dividing the two numbers and considering the remainder. The … Leggi tutto

Append Python

Append Python

The append method in Python, discussed in the last lesson, is used to insert items at the end of a list. The syntax of this method is as follows: list.append (element) The method accepts only one parameter, the element to be inserted in what to the list. First exercise with append in Python There is … Leggi tutto

Prime numbers Python

Prime numbers Python

Let’s create a program on prime numbers in Python using the iterative structures studied so far, in order to deepen them. So, let’s remember the definition of prime number: A number is prime when it has only two divisors: one and itself. So, every natural number greater than 1 that is divisible only by 1 … Leggi tutto

Python while loop exercises

Python while loop exercises

Let’s improve our knowledge in Python with some while loop exercises. In particular, today we will face an exercise that also deals with the exchange of variables as a topic. Python while loop exercises – first exercise Write a program that, by reading two integers, subtracts the lesser from the greater until their difference becomes … Leggi tutto

number guessing game Python

number guessing game Python

In this tutorial we will make the number guessing game in Python, also known as the high-low game. That is, let’s imagine that the computer thinks a number in a certain interval and we have to try to guess it. number guessing game in Python – first solution Guess a number between 1 and 100, … Leggi tutto

Python insert

Python insert

In this lesson, I propose an exercise that still uses the method Python insert. Python Insert – First exercise Insert 20 random numbers from 50 to 150 at the top of the list, with the method Python insert. Display the elements with another cycle. Next, modify each element, subtracting the sum of its digits from … Leggi tutto

Python for loop examples

Python for loop examples

In this Python lesson we will give some for loop examples, in order to better understand how it works and develop logical thinking. Python for loop examples – first example Design an algorithm that writes all pairs of numbers that give 60 as a product. The solution is to use a for loop to find … Leggi tutto

Python add to list

Python add to list

In Python how can we add elements to a list? We have already said that we can use various methods, in this lesson we will see practical examples. Exercise – Python add to list Populate a list of n elements with the first n multiples of 10. Numbers must be inserted at the end of … Leggi tutto

While exercises Python

while exercises Python

I propose other exercises with the while in Python before moving on to other arguments. While exercises Python – First exercise Take 15 numbers as input and calculate the average. First of all we give N the value 15. N therefore represents the quantity of numbers to insert. We use a counter variable that starts … Leggi tutto

Python count list

Python count list

In Python the count method on the list data structure is used to count the number of occurrences of an element in the main list, but not within any sublists. If the element is not found, the value will be 0. Python count list – count the number of occurrences in list We take as … Leggi tutto

Count list Python

Count list Python

Il metodo count sulla struttura dati list in Python serve a contare il numero delle occorrenze di un elemento nella lista principale, ma non all’interno di eventuali sottoliste. Se l’elemento non viene trovato si avrà come valore 0. Count list Python – conteggiare quante volte compare un elemento Prendiamo come riferimento una lista dei voti, … Leggi tutto

Python len list

Python len list

In Python the len method on the list allows us to get the number of elements in a list, ultimately it indicates the length of a list in Python. The syntax of the method is very simple: len(list) The function takes only one parameter, the list to calculate the length of. This parameter is mandatory. … Leggi tutto

Sort dictionary Python

Sort dictionary Python

You can sort a dictionary in Python by key or by value. Let’s do some practical examples of sorting in Python according to our needs. All the proposed examples can be tested in the online compiler that you will find at the following link: Python compiler online. First example – Sort a dictionary in Python … Leggi tutto

Ordinare un dizionario in Python

Ordinare un dizionario in Python

Si può ordinare un dizionario in Python per chiave o per valore. Facciamo alcuni esempi pratici di ordinamento in Python in base alle nostre esigenze. Tutti gli esempi proposti possono essere provati nel compiler online che troverete al seguente link: Python compiler online. Primo esempio – Ordinare un dizionario in Python per chiave In questo … Leggi tutto

Python continue

Python continue

Python continue statement allows us to stop the current iteration to start over from the first statement of the loop (for or while) in which it was entered. Python continue – first example So let’s take a simple example to better understand how it works. We enter numbers, if the number is negative we use … Leggi tutto