Just thought it’s funny to share when I revealed the solution
The exercise is to print elements of a list at odd indexes
The solution:
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# stat from index 1 with step 2( means 1, 3, 5, an so on)
for i in my_list[1::2]:
print(i, end=" ")
My version:
myList = [] num = input("Enter a string of numbers: ") userList = num.split() numList = (int(x) for x in userList) for i in numList: myList.append(i) print(f"Your list: {myList}") print(f"Odd indexes: ", end="") for i in range(0, len(myList)): if i % 2 != 0: print(myList[i], end=" ")
submitted by /u/henconst796
[link] [comments]
r/learnpython Just thought it’s funny to share when I revealed the solution The exercise is to print elements of a list at odd indexes The solution: my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] # stat from index 1 with step 2( means 1, 3, 5, an so on) for i in my_list[1::2]: print(i, end=” “) My version: myList = [] num = input(“Enter a string of numbers: “) userList = num.split() numList = (int(x) for x in userList) for i in numList: myList.append(i) print(f”Your list: {myList}”) print(f”Odd indexes: “, end=””) for i in range(0, len(myList)): if i % 2 != 0: print(myList[i], end=” “) submitted by /u/henconst796 [link] [comments]
Just thought it’s funny to share when I revealed the solution
The exercise is to print elements of a list at odd indexes
The solution:
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# stat from index 1 with step 2( means 1, 3, 5, an so on)
for i in my_list[1::2]:
print(i, end=" ")
My version:
myList = [] num = input("Enter a string of numbers: ") userList = num.split() numList = (int(x) for x in userList) for i in numList: myList.append(i) print(f"Your list: {myList}") print(f"Odd indexes: ", end="") for i in range(0, len(myList)): if i % 2 != 0: print(myList[i], end=" ")
submitted by /u/henconst796
[link] [comments]