Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python - List Exercises



Python List Exercise 1

Python program to find unique numbers in a given list.

L1 = [1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2]L2 = []for x in L1:   if x not in L2:      L2.append(x)print (L2)

It will produce the followingoutput

[1, 9, 6, 3, 4, 5, 2, 7, 8]

Python List Exercise 2

Python program to find sum of all numbers in a list.

L1 = [1, 9, 1, 6, 3, 4]ttl = 0for x in L1:   ttl+=xprint ("Sum of all numbers Using loop:", ttl)ttl = sum(L1)print ("Sum of all numbers sum() function:", ttl)

It will produce the followingoutput

Sum of all numbers Using loop: 24Sum of all numbers sum() function: 24

Python List Exercise 3

Python program to create a list of 5 random integers.

import randomL1 = []for i in range(5):   x = random.randint(0, 100)   L1.append(x)print (L1)

It will produce the followingoutput

[77, 3, 20, 91, 85]
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp