Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Common Tuple Methods in Python | Other Data Types in Python
Introduction to Python
course content

Kurssisisältö

Introduction to Python

Introduction to Python

1. First Acquaintance with Python
2. Variables and Types in Python
3. Conditional Statements in Python
4. Other Data Types in Python
5. Loops in Python
6. Functions in Python

book
Common Tuple Methods in Python

Tuples in Python come with a few built-in operations that allow you to retrieve information and manipulate them indirectly. Here are some key methods:

  • len(t): returns the number of elements in tuple t;
  • t * n: creates n repetitions of tuple t;
  • t.count(x): counts how often x appears in t.

Since tuples are immutable, you might wonder: How do I add or modify elements in a tuple? Unlike lists, tuples do not support methods like .append() or .extend() for adding elements directly. However, there is a workaround called tuple concatenation.

  • tuple1 + tuple2: merges two tuples (both must be tuples).
123456789
# Initial tuple US_Info = ("USA", 9629091, 331002651) # New data US_Info_new = ("Washington D.C.", 50) # Concatenate two tuples and save the result US_Info_upd = US_Info + US_Info_new print(US_Info_upd)
copy
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 7
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt