Tuesday, 21 September 2021

Python: Print every item in the list

Approach 1: Using for loop

print_every_item_1.py

def print_every_item(my_list):
    for item in my_list:
        print(item)

print_every_item([2, 3, 5, 7])

 

Approach 2: Using index notation

print_every_item_2.py

def print_every_item(my_list):
    no_of_items = len(my_list)
    count = 0

    while count < no_of_items:
        print(my_list[count])
        count += 1
    
print_every_item([2, 3, 5, 7])

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment