Python Fibonacci

Python Fibonacci

In this lesson we will analyze some algorithmsin Python to check if a number belongs to the Fibonacci sequence. In the previous lesson we studied how to print a Fibonacci sequence of variable length, each time decided by the user. We have both an iterative and a recursive solution, using functions as well. In this … Leggi tutto

Fibonacci sequence in Python

Fibonacci sequence in Python

Fibonacci sequence in Python can have different implementation algorithms. Recall that the Fibonacci sequence is a sequence of positive integers in which each number starting from the third is the sum of the previous two except the first two which are 1, 1. For example, if N = 9, the terms of the sequence are: … Leggi tutto

How to generate random numbers in Python

How to generate random numbers in Python? Random numbers are used for many purposes, let’s take some examples to better understand how are work. First example about how to generate random numbers in Python Write a program that generates n random numbers in an interval set by the user and displays them in output. So, … Leggi tutto

Python random number

Random Python

In this lesson we study ho to generate a Python random number. Random numbers in Python, i.e. pseudorandom numbers, are used by first importing the random module. Then we just put in the script: import random. If you also try to type help (‘random’) in interactive mode, you will see the specifications of this module, … Leggi tutto

Machine Learning with Python

Machine Learning with Python

How implement Machine Learning algorithms with Python? Python is also one of the most used programming languages ​​in the world. In fact, this language offers essential libraries for making statistics, for processing images or even for data analysis. Furthermore, Python is characterized by having a simple but feature-rich syntax. Certainly being an interpreted language it … Leggi tutto

Machine Learning

Machine Learning

Today we often hear about Machine Learning and Artificial Intelligence. Machine Learning algorithms are currently used in various fields. For example, we find applications in online shopping, in interactions with social media, in financial services, in health care, in the marketing sector to manage targeted advertising, etc. Therefore it is essential to know the term … Leggi tutto

Test Python

Test Python

Here is a test to evaluate the skills acquired on the Python language, specifically this is a quiz about the lambda function. Evaluate your skills on basic and advanced Python concepts with tests created on Creative Coding. [ays_quiz id=’3′]  Write in the comments any doubts or difficulties or if something is wrong in your opinion!Follow … Leggi tutto

Factorial

Factorial

In this lesson we will calculate the factorial of a number in Python using various methods. First let’s give the definition. In mathematics, the factorial of a natural number n is the product of positive integers less than or equal to this number and is denoted by n! So for example 5! = 5 * … Leggi tutto

Python lambda

Python lambda

In this article we study Python lambda function, i.e. anonymous functions. In previous lessons we saw how to create functions using the keyword def followed by the function name and arguments if there are any. On the contrary, the lambda function does not have a name, but only arguments and a single calculation expression. The … Leggi tutto

Python matrix

Python matrici

In Python we can create matrix using a double for loop, or we can just use the Numpy library. In this lesson we will create arrays using iterative instructions, later we will see the use of Numpy. Python matrix – first exercise In this first exercise we use a double for loop to create arrays … Leggi tutto

Python Quicksort

Quicksort Python

We implement the Python Quicksort algorithm, also known as the sorting algorithm which is based on the divide and conquer approach! Its operation is based on the pivot, which is an element that can be selected in various ways. Throughout this guide we will study the various ways. In fact, for example, the pivot can … Leggi tutto

Python Merge Sort

Python Merge Sort

We develop the Python Merge Sort algorithm, one of the most famous sorting algorithms that uses the divide and conquer method, as well as the Quick Sort. First of all we explain how this algorithm works. First we divide the list into two sublists and then gradually into even smaller parts. Then we use a … Leggi tutto

Python Insertion Sort

Insertion Sort Python

Let’s study the Python Insertion Sort algorithm, a very simple sorting algorithm to implement. The algorithm works in a very similar way to the way we arrange playing cards in our hand. In fact, it is assumed that the first card is ordered, then an unsorted card is taken and placed on its left if … Leggi tutto

Random number file writer

Random number file writer

Random number file writer – In this lesson we will develop a simple algorithm for adding a random number to a file in Python. Suppose we have a file that contains a number. First we print the number contained in the file, then we generate a random number and add it to the number contained … Leggi tutto

How to write to text file

How to Write to Text File in Python

In this lesson we will study how to write to text file in Python, using the append mode and we will give some examples. How to write to text file in Python – first example In this first example we will use our address_book.txt file which already contains data and add new contacts. For example, … Leggi tutto

Python read()

In this lesson we will study the Python read() method. Let’s start with a simple example that uses the address_book.txt file, the quake contains two simple example contacts. We open this file in read mode and then apply the Python read() method. f = open(‘address_book.txt’, ‘r’) contact = f.read() print(contact) f.close() The output will be: … Leggi tutto

Python readlines()

Python readlines

In this lesson we will talk about Python readlines() method, which reads the entire text file and returns a list. Python readlines() – first example In this first example we will read our rubric.txt file using this readlines, let’s see what happens. So here’s a possible example: f = open(‘address_book.txt’, ‘r’) contact = f.readlines() print(contact) … Leggi tutto

Python readline()

Python readline

In this lesson we will study Python readline() method required for reading a text file. This method returns a line of characters, including the newline character, which is the \ n character. Python readline() – first example Let’s take a first example on an address book file that contains the following example contacts: Name: cristina … Leggi tutto