Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def f1(n: int):
- n *=2
- # print(n)
- return n
- # una funzione riceve sempre l'indirizzo dell'oggetto esterno
- # passaggio by sharing (by object reference)
- # quando si passa un immutable la funzione non potrà
- # modificarlo
- numero = 34
- f1(numero) # numero vale ancora 34
- # questo è il modo idiomatico per la modifica
- numero = f1(numero) # ora numero vale 68
- # print(numero)
- # passando um mutable la funzione riceve
- # anche in questo caso l'indirizzo dell'oggetto
- # esterno ma potrà modificarne il CONTENUTO
- def f2(l: list[str]):
- l.append("hello")
- l = []
- return l
- elenco: list[str] = ["Salve", "Buenas dias"]
- f2(elenco)
- # print(elenco)
- # i parametri con valore di default vanno messi tutti per ultimi
- def disegna(figura: str, colore_tratto:str = 'black',
- colore_sfondo: str|None = None):
- print("Tratto: " + colore_tratto)
- print("Sfondo: " + (colore_sfondo if colore_sfondo is not None else "None"))
- return
- disegna(figura="q")
- print("-"*20)
- disegna(figura="q", colore_sfondo="giallo")
- print("-"*20)
- disegna(figura="q", colore_tratto="giallo")
- print("-"*20)
- def f3(a=0, b=1, c=2, d=3, e=4):
- print (f"a={a} b={b} c={c} d={d} e={e}")
- f3(12, 23)
- # def funzione_di_calcolo_key(ele: dict[str, str|int]):
- # return ele["eta"]
- # lambda function
- utenti = [
- {"nome": "Alice", "eta": 30},
- {"nome": "Bob", "eta": 25},
- {"nome": "Charlie", "eta": 35}
- ]
- # Ordina la lista in base al valore della chiave "età"
- utenti_ordinati = sorted(utenti, key= lambda ele: ele["eta"] )
- print(utenti_ordinati)
- utenti = [
- {"id": 1, "nome": "Mario", "cognome": "Rossi", "username": "mrossi"},
- {"id": 2, "nome": "Luigi", "cognome": "Verdi", "username": "lverdi"}
- # ecc...
- ]
- def ottieni_estrattore(formato_richiesto):
- match formato_richiesto:
- case "nome_completo":
- return lambda utente: f"{utente['nome']} {utente['cognome']}"
- case "id_formattato":
- return lambda utente: f"USER-{utente['id']:05d}"
- case _:
- return lambda utente: utente.get('username', 'Anonimo')
- def ottieni_estrattore2(formato_richiesto):
- match formato_richiesto:
- case "nome_completo":
- def estrai_nome(utente):
- return f"{utente['nome']} {utente['cognome']}"
- return estrai_nome
- case "id_formattato":
- def estrai_id(utente):
- return f"USER-{utente['id']:05d}"
- return estrai_id
- case _:
- def estrai_default(utente):
- return utente.get('username', 'Anonimo')
- return estrai_default
- estrattore = ottieni_estrattore("id_formattato")
- for utente in utenti:
- print(estrattore(utente))
- # dati_grezzi = [" mela ", "kiwi", " BanAna ", "te"]
- #
- # lambda1 = lambda s: s.strip().lower() if len(s.strip().lower()) > 4 else "TROPPO CORTA"
- # # Output: ['TROPPO CORTA', 'TROPPO CORTA', 'banana', 'TROPPO CORTA']
- #
- # lambda1_ottimizzata = lambda s: pulita if len(pulita := s.strip().lower()) > 4 else "TROPPO CORTA"
- #
- # f = lambda x: [i for i in range(x)]
- # print(f(5))
- # lambda1 = lambda x: (y := x + 1) * 2
- # print(y)
- #
- # lambda2 = lambda x: x if x > 0 else 0
- # lambda3 = lambda x: [i for i in range(x)]
Advertisement
Add Comment
Please, Sign In to add comment