Es. Python tutorial - Funzioni

Es. Python tutorial - Funzioni Es. Python tutorial - Funzioni

Es. Python tutorial - Funzioni

Versione italiana

Esercizi

  1. Esercizio 1: Somma Scrivi una funzione somma(a, b) che restituisce la somma di due numeri.

  2. Esercizio 2: Massimo Scrivi una funzione massimo(x, y) che restituisce il valore massimo tra due numeri.

  3. Esercizio 3: Parole lunghe Scrivi una funzione parole_lunghe(lista_parole, lunghezza) che restituisce una lista di parole che hanno una lunghezza maggiore di un valore specificato.

  4. Esercizio 4: Fattoriale Scrivi una funzione fattoriale(n) che restituisce il fattoriale di un numero intero non negativo.

  5. Esercizio 5: Palindromo Scrivi una funzione è_palindromo(parola) che restituisce True se la parola è un palindromo, altrimenti False.

  6. Esercizio 6: Fibonacci Scrivi una funzione fibonacci(n) che restituisce il n-esimo numero della sequenza di Fibonacci.

  7. Esercizio 7: Conta vocali Scrivi una funzione conta_vocali(stringa) che restituisce il numero di vocali in una stringa.

  8. Esercizio 8: Inverso Scrivi una funzione inverso(lista) che restituisce una nuova lista con gli elementi in ordine inverso.

  9. Esercizio 9: Unione di liste Scrivi una funzione unione_liste(lista1, lista2) che restituisce una lista contenente gli elementi unici di entrambe le liste.

  10. Esercizio 10: Media Scrivi una funzione media(lista_numeri) che restituisce la media dei numeri in una lista.

Risposte

  1. def somma(a, b):
        return a + b
  2. def massimo(x, y):
        return x if x > y else y
  3. def parole_lunghe(lista_parole, lunghezza):
        return [parola for parola in lista_parole if len(parola) > lunghezza]
  4. def fattoriale(n):
        if n == 0:
            return 1
        else:
            return n * fattoriale(n - 1)
  5. def è_palindromo(parola):
        return parola == parola[::-1]
  6. def fibonacci(n):
        if n <= 0:
            return 0
        elif n == 1:
            return 1
        else:
            return fibonacci(n - 1) + fibonacci(n - 2)
  7. def conta_vocali(stringa):
        vocali = 'aeiouAEIOU'
        return sum(1 for char in stringa if char in vocali)
  8. def inverso(lista):
        return lista[::-1]
  9. def unione_liste(lista1, lista2):
        return list(set(lista1) | set(lista2))
  10. python def media(lista_numeri): return sum(lista_numeri) / len(lista_numeri) if lista_numeri else 0

English version

Exercises

  1. Exercise 1: Sum Write a function sum(a, b) that returns the sum of two numbers.

  2. Exercise 2: Maximum Write a function maximum(x, y) that returns the maximum value of two numbers.

  3. Exercise 3: Long Words Write a function long_words(word_list, length) that returns a list of words that have a length greater than a specified value.

  4. Exercise 4: Factorial Write a function factorial(n) that returns the factorial of a non-negative integer.

  5. Exercise 5: Palindrome Write a function is_palindrome(word) that returns True if the word is a palindrome, otherwise False.

  6. Exercise 6: Fibonacci Write a function fibonacci(n) that returns the nth number in the Fibonacci sequence.

  7. Exercise 7: Count Vowels Write a function count_vowels(string) that returns the number of vowels in a string.

  8. Exercise 8: Inverse Write a function inverse(list) that returns a new list with the elements in reverse order.

  9. Exercise 9: Union of Lists Write a function union_lists(list1, list2) that returns a list containing the unique elements of both lists.

  10. Exercise 10: Average Write a function average(list_numbers) that returns the average of the numbers in a list.

Answers

def sum(a, b):
return a + b
def max(x, y):
return x if x > y else y
def long_words(wordlist, length):
return [word for word in wordlist if len(word) > length]
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
def is_palindrome(word):
return word == word[::-1]
def fibonacci(n):
if n <= 0:
 return 0
 elif n == 1:
 return 1
 else:
 return fibonacci(n - 1) + fibonacci(n - 2)
def voice_count(string):
vowels = 'aeiouAEIOU'
return sum(1 for char in string if char in vowels)
def inverse(list):
return list[::-1]
def union_lists(list1, list2):
return list(set(list1) | set(list2))
def average(list_numbers):
return sum(number_list) / len(number_list) if number_list else 0

Commenti