Docsity

Esercitazioni di programmazione in Python, Esercizi di Programmazione Avanzata

Toggle sidebar
bg1
esercitazione-per-il-compito
June 10, 2024
[2]: def fun(a,b):
res=(a-b)**2
return res
fun(5,3)
[2]: 4
[3]: def fun(*lista):
for iin lista:
if len(i)>5:
print(i)
fun('ciao','dio','castoro')
castoro
[13]: def fun(a,/,*,b):
return a**2,b**0.5
fun(5,b=4)
[13]: (25, 2.0)
[15]: def fun(_conv,temp):
if _conv=='FC':
return (temp-32)*5/9
else:
return (temp*9/5)+32
fun('CF',0)
[15]: 32.0
[20]: def fun(k):
if k==1:
return 1
else:
return k+fun(k-1)
fun(5)
[20]: 15
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Anteprima parziale del testo

Scarica Esercitazioni di programmazione in Python e più Esercizi in PDF di Programmazione Avanzata solo su Docsity!

esercitazione-per-il-compito

June 10, 2024

2: def fun(a,b): res= (a-b)** return res fun(5,3)

3: def fun(*lista): for i in lista: if len(i)>5: print(i) fun('ciao','dio','castoro')

castoro

[13]: def fun(a,/,,b): return a2,b*0. fun(5,b=4)

[13]: (25, 2.0)

15: def fun(_conv,temp): if _conv=='FC': return (temp-32)5/ else : return (temp9/5)+ fun('CF',0)

20: def fun(k): if k==1: return 1 else : return k+fun(k-1) fun(5)

[25]: def fun(a,b,c): if (a>b and ac and a1,k)) fun([4,4,3,1,2,1,1,5])

[68]: [4, 4, 1, 1, 1]

[72]: def fun (a1,a2,a3): print('a1',a1.upper()) print('a2',a2.upper()) print('a3',a3.upper()) diz={'a1':'ciao','a2':'suca','a3':'casa'} fun(**diz)

tips.shape

83: (244, 7)

81: tip day 0 1.01 Sun 1 1.66 Sun 2 3.50 Sun 3 3.31 Sun 4 3.61 Sun .. … … 239 5.92 Sat 240 2.00 Sat 241 2.00 Sat 242 1.75 Sat 243 3.00 Thur

[244 rows x 2 columns]

82: tip day 0 1.01 Sun 1 1.66 Sun 2 3.50 Sun 3 3.31 Sun 4 3.61 Sun .. … … 239 5.92 Sat 240 2.00 Sat 241 2.00 Sat 242 1.75 Sat 243 3.00 Thur

[244 rows x 2 columns]

86: count 244. mean 2. std 0. min 1. 25% 2. 50% 2.

[89]: a=tips[(tips['total_bill']>20)&(tips['sex']=='Female')]

a [90]: a.to_csv('a.csv',index= False ) [95]: tips['Proportion']=tips['tip']/tips['total_bill'] tips

  • 75% 3.
  • max 6.
  • Name: size, dtype: float
  • 4 24.59 3.61 Female No Sun Dinner [89]: total_bill tip sex smoker day time size
  • 11 35.26 5.00 Female No Sun Dinner
  • 21 20.29 2.75 Female No Sat Dinner
  • 33 20.69 2.45 Female No Sat Dinner
  • 52 34.81 5.20 Female No Sun Dinner
  • 57 26.41 1.50 Female No Sat Dinner
  • 72 26.86 3.14 Female Yes Sat Dinner
  • 73 25.28 5.00 Female Yes Sat Dinner
  • 85 34.83 5.17 Female No Thur Lunch
  • 94 22.75 3.25 Female No Fri Dinner
  • 102 44.30 2.50 Female Yes Sat Dinner
  • 103 22.42 3.48 Female Yes Sat Dinner
  • 104 20.92 4.08 Female No Sat Dinner
  • 114 25.71 4.00 Female No Sun Dinner
  • 119 24.08 2.92 Female No Thur Lunch
  • 125 29.80 4.20 Female No Thur Lunch
  • 131 20.27 2.83 Female No Thur Lunch
  • 143 27.05 5.00 Female No Thur Lunch
  • 155 29.85 5.14 Female No Sun Dinner
  • 157 25.00 3.75 Female No Sun Dinner
  • 186 20.90 3.50 Female Yes Sun Dinner
  • 197 43.11 5.00 Female Yes Thur Lunch
  • 214 28.17 6.50 Female Yes Sat Dinner
  • 219 30.14 3.09 Female Yes Sat Dinner
  • 229 22.12 2.88 Female Yes Sat Dinner
  • 238 35.83 4.67 Female No Sat Dinner
  • 240 27.18 2.00 Female Yes Sat Dinner
  • 0 16.99 1.01 Female No Sun Dinner 2 0. [95]: total_bill tip sex smoker day time size Proportion
  • 1 10.34 1.66 Male No Sun Dinner 3 0.
  • 2 21.01 3.50 Male No Sun Dinner 3 0.
  • 3 23.68 3.31 Male No Sun Dinner 2 0.
  • 4 24.59 3.61 Female No Sun Dinner 4 0.

sex Female 2. Male 3.

:

sex time Female Dinner 3. Lunch 2. Male Dinner 3. Lunch 2.

Sat 87 Sun 76 Thur 62 Fri 19 Name: count, dtype: int

:

118: size day 1 Sat 2 Thur 1 Fri 1 2 Sat 53 Thur 48 Sun 39 Fri 16 3 Sat 18 Sun 15 Thur 4 Fri 1 4 Sun 18 Sat 13 Thur 5 Fri 1 5 Sun 3 Sat 1

Thur 1 6 Thur 3 Sun 1 Name: count, dtype: int

:

120: day smoker Fri Yes 15 No 4 Sat No 45 Yes 42 Sun No 57 Yes 19 Thur No 45 Yes 17 Name: count, dtype: int

size 1 1. 2 5. 3 10. 4 9. 5 5. 6 6.

[6]: tips.apply('median',numeric_only= True )

[6]: total_bill 17. tip 2. size 2. dtype: float

[26]: def fun(x): res=x*2 -5x + return res lista=[1,2,3,4] a=list(map(fun,lista)) print(a) import matplotlib.pyplot as plt plt.plot(lista,a,) plt.title('grafico compito')

341 Gentoo Biscoe 50.4 15.7 222. 342 Gentoo Biscoe 45.2 14.8 212. 343 Gentoo Biscoe 49.9 16.1 213.

body_mass_g sex 0 3750.0 Male 1 3800.0 Female 2 3250.0 Female 3 NaN NaN 4 3450.0 Female .. … … 339 NaN NaN 340 4850.0 Female 341 5750.0 Male 342 5200.0 Female 343 5400.0 Male

[344 rows x 7 columns]

15: species object island object bill_length_mm float bill_depth_mm float flipper_length_mm float body_mass_g float sex object dtype: object

C:\Users\HP\anaconda3\Lib\site-packages\seaborn\axisgrid.py:118: UserWarning: The figure layout has changed to tight self._figure.tight_layout(*args, **kwargs)

3:

C:\Users\HP\anaconda3\Lib\site-packages\seaborn\axisgrid.py:118: UserWarning: The figure layout has changed to tight self._figure.tight_layout(*args, **kwargs)

12:

C:\Users\HP\anaconda3\Lib\site-packages\seaborn\axisgrid.py:118: UserWarning: The figure layout has changed to tight self._figure.tight_layout(*args, **kwargs)

18:

C:\Users\HP\anaconda3\Lib\site-packages\seaborn\axisgrid.py:118: UserWarning: The figure layout has changed to tight self._figure.tight_layout(*args, **kwargs)

16:

20: g = sns.FacetGrid(pingu, col="species", hue="sex") g.map_dataframe(sns.scatterplot, x="body_mass_g", y="flipper_length_mm") g.add_legend()

C:\Users\HP\anaconda3\Lib\site-packages\seaborn\axisgrid.py:118: UserWarning: The figure layout has changed to tight self._figure.tight_layout(*args, **kwargs)

20:

Text(0.9768757206889681, -0.5056815463593757, '(5500, 6000]'), Text(1.0895742752636806, -0.15108904223545, '(2500, 3000]'), Text(1.0998143634288768, -0.02020806758536635, '(6000, 6500]')])

C:\Users\HP\anaconda3\Lib\site-packages\seaborn\axisgrid.py:118: UserWarning: The figure layout has changed to tight self._figure.tight_layout(*args, **kwargs)

35:

[1]: a=int(input('inserire un numero')) q=a** c=a** print(q,c)

inserire un numero 2 4 8

2: a=int(input('inserire un numero: ')) print(-4*a)

inserire un numero: 2

[5]: a,b=input('inserire due numeri: ').split(',') a=int(a) b=int(b)

inserire una lista di numeri: 5,1, [(5, 125), (1, 1), (2, 8)]

[24]: lista=input('inserire una lista di numeri: ').split(',') lista=list(map(int,lista)) l1=[(i,i**3) for i in lista] l

inserire una lista di numeri: 5,1,

[24]: [(5, 125), (1, 1), (2, 8)]

[28]: lista=input('inserire una lista di stringhe: ').split(',') diz={i:lista.count(i) for i in lista} diz

inserire una lista di stringhe: rosa,giallo,rosa

[28]: {'rosa': 2, 'giallo': 1}

[30]: lista=input('inserire una lista di numeri: ').split(',') lista=list(map(int,lista)) k=int(input('inserire un numero: ')) l1=[(i,j) for i in lista for j in lista if i+j==k] l

inserire una lista di numeri: 1,12,3,4,6, inserire un numero: 5

[30]: [(1, 4), (3, 2), (4, 1), (2, 3)]

[33]: cond= True while cond: t1=tuple(input('Inserire una tupla1: ').split(',')) t2=tuple(input('Inserire una tupla2: ').split(',')) if len(t1)==len(t2): cond= False lista = for i in range(len(t1)): t3 = tuple([t1[i], t2[i], t1[i] if t1[i] < t2[i] else t2[i]]) lista.append(t3) print(lista)

Inserire una tupla1: 4,7,- Inserire una tupla2: 5,3, [('4', '5', '4'), ('7', '3', '3'), ('-2', '4', '-2')]

:

while l<4: tup=input('inserire una tupla di 4 elementi: ').split(',') l=len(tup) tup=tuple(tup) lista=[tup[i] for i in range(0,len(tup)+1,3)] lista

inserire una tupla di 4 elementi: 3,4,5,

2: ['3', '6']

4: diz={i:i**2 for i in range(1,16)} print(diz)

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}

l1=list(diz.keys()) l2=list(diz.values()) l2=l2.sort() for i in l1: for j in l2: diz1={i:j

: lista=list(map(int,input('inserire una lista di numeri: '))).split(',') for i in range(0,len(lista)+1): for j in range(0,len(lista)+1): if lista[i]*lista[j]

[5]: stringa=input('inserire una stringa: ') stringa=list(stringa) diz={i:stringa.count(i) for i in stringa} print(diz)

inserire una stringa: pomodoro {'p': 1, 'o': 4, 'm': 1, 'd': 1, 'r': 1}

while l<3: l1=input('inserire una lista di numeri: ').split(',') l1=list(map(int,l1)) l=len(l1) l2=l1.copy()