📝

Input List with Loops

Oct 6, 2025

Overview

This lecture explains the limitations of Python's input function when accepting lists from users and demonstrates how to use loops to correctly input multiple items and store them as a list.

Problem with the Input Method

  • The input() function always returns user input as a string, not as separate data elements.
  • When entering multiple numbers (e.g., "67 48 90"), input() stores them as a single string, not as a list of integers.
  • If stored directly, the string "67 48 90" is treated as individual characters in a list, not as numbers.

Input a List Using Loops

  • To store multiple numbers as a list, first ask the user how many elements to enter using: n = int(input("Enter the number of elements")).
  • Initialize an empty list with: numbers = [].
  • Use a for loop to repeat the input process: for i in range(n):.
  • For each iteration, receive an integer input with: x = int(input()).
  • Append each integer to the list using: numbers.append(x).
  • The final list contains separate integer items as intended.

Key Terms & Definitions

  • input() — Function to receive input from the user, always returns a string.
  • Typecasting — Converting data from one type to another (e.g., string to integer with int()).
  • List — An ordered collection of items in Python.
  • append() — List method to add an item to the end of a list.
  • for Loop — A control flow statement used to repeat a block of code multiple times.

Action Items / Next Steps

  • Practice writing a Python program that inputs a list of user numbers using a loop.
  • Review previous presentation notes on receiving and typecasting user input.