Fundamentals 3 min read

Python For Loops: Working with Composite Data Types

The article explains Python's for‑in loop syntax, describes common iterable types such as strings, lists, tuples and sets, details how the range function defines start and end values, and provides concrete code examples that print numbers, list elements and characters from both English and Chinese strings.

Lisa Notes
Lisa Notes
Lisa Notes
Python For Loops: Working with Composite Data Types

Python’s for loop uses the for ... in ... syntax, which differs from the traditional C‑style loop found in many other languages.

The general form is for element in iterable, where the iterable can be a string, list, tuple, set, or any other object that supports iteration.

The built‑in range function generates a sequence of integers. Its first argument is the start value (inclusive) and the second argument is the end value (exclusive).

Examples:

# Output all numbers from 1 to 10
for i in range(1, 11):
    print(i)

# Print each element of a list
for i in [12, 34, 5, 6, 86, 42]:
    print(i)

# Iterate over a string
for i in "hello":
    print(i)

# Iterate over a Chinese string
for i in "欢迎光临":
    print(i)

The resulting output is:

1
2
3
4
5
6
7
8
9
10
12
34
5
6
86
42
h
e
l
l
o
欢
迎
光
临
stringlistrangeiterablefor-loop
Lisa Notes
Written by

Lisa Notes

Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.