w3resource

Extending sequences with ellipsis in Python

Python ellipsis (...) Data Type: Exercise-8 with Solution

Write a Python program that uses Ellipsis to extend a sequence with another sequence.

Sample Solution:

Code:

# Define three sequences
sequence1 = [1, 2, 3]
sequence2 = [4, 5, 6]
sequence3 = [7, 8, 9]

# Extend sequence1, sequence2 and sequence3 using ellipsis
extended_sequence = sequence1 + [...] + sequence2 + [...] + sequence3

# Print the extended sequence
print(extended_sequence)

Output:

[1, 2, 3, Ellipsis, 4, 5, 6, Ellipsis, 7, 8, 9]

In the exercise above, we have three sequences, sequence1, sequence2 and sequence3. We use 'ellipsis (...)' between them to represent an extension operation. The result, extended_sequence, will contain the elements from sequence1, sequence2 and sequence3 concatenated together.

Flowchart:

Flowchart: Extending sequences with ellipsis in Python.

Previous: Generating sequences with ellipsis in Python.
Next: Creating Python classes with dynamic attributes.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Summing a sequence of numbers (calculating the sum of zero to ten with skips):

>>> l = range(0,10,2)
>>> sum(l)
20

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook