One item at a time
friends = ["Asha", "Rahul", "Priya"]
for friend in friends:
print("Hi,", friend)Output
Hi, Asha Hi, Rahul Hi, Priya
Getting the position too
enumerate() gives you the position and the item. start=1 counts from 1:
friends = ["Asha", "Rahul", "Priya"]
for number, friend in enumerate(friends, start=1):
print(f"{number}. {friend}")Output
1. Asha 2. Rahul 3. Priya
Building a new list
Start with an empty list and append inside a loop:
prices = [100, 250, 80]
with_delivery = []
for price in prices:
with_delivery.append(price + 40)
print(with_delivery)Output
[140, 290, 120]
Reading a list from input
Many challenges give you several numbers on one line. split() breaks the line up, then convert each part:
line = input("Numbers: ")
numbers = []
for part in line.split():
numbers.append(int(part))
print(numbers)Output (when you type 4 8 15 16)
Numbers: 4 8 15 16 [4, 8, 15, 16]