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 + list2After 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 * 3After 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
EasyCreate a function named create_pattern that takes two arguments:
- A list of numbers (
numbers). - An integer (
repeats).
The function should:
- Concatenate the list with itself (list + list).
- Repeat the resulting list
repeatstimes using the*operator. - Return the final pattern.
Try it yourself
def create_pattern(numbers, repeats):
# Write your code here
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2Practice on your own: Online Python compiler