Menu
Coddy logo textTech

Sequence Operators

Part of the Fundamentals section of Coddy's Python journey — lesson 66 of 77.

Python provides several operators that can be used with sequences like lists, strings, and tuples.

Concatenation with the + operator joins two sequences together:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2

After executing the above code, combined_list contains:

[1, 2, 3, 4, 5, 6]

Repetition with the * operator repeats a sequence a specified number of times:

numbers = [1, 2, 3]
repeated_numbers = numbers * 3

After executing the above code, repeated_numbers contains:

[1, 2, 3, 1, 2, 3, 1, 2, 3]

These operators work with other sequences too:

# String concatenation
greeting = "Hello" + " " + "World"  # "Hello World"

# String repetition
stars = "*" * 5  # "*****"
challenge icon

Challenge

Easy

Create a function named create_pattern that takes two arguments:

  • A list of numbers (numbers).
  • An integer (repeats).

The function should:

  1. Concatenate the list with itself (list + list).
  2. Repeat the resulting list repeats times using the * operator.
  3. Return the final pattern.

Cheat sheet

List manipulation operators:

  • Combine lists with +:
    combined = list1 + list2
  • Repeat lists with *:
    repeated = list * n

Example:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2
# [1, 2, 3, 4, 5, 6]

numbers = [1, 2] * 3
# [1, 2, 1, 2, 1, 2]

Try it yourself

def create_pattern(numbers, repeats):
    # Write your code here
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals