Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Common Tuple Methods in Python | Other Data Types in Python
Introduction to Python (dev copy)

bookCommon Tuple Methods in Python

Let's dive into the primary methods associated with tuples.

  • len(t) - gives the number of elements in the tuple t;
  • t * n - produces n repetitions of the tuple t;
  • t.count(x) - counts how often x appears in the tuple t.

In the previous chapter, we highlighted that tuples are immutable. So, you might wonder: "How can I update a tuple with new values?"

Unlike lists, you can't use methods like .append() or .extend() with tuples. However, remember a few chapters back when we discussed concatenation? Here's the method that works for tuples too:

tuple1 + tuple2 - this joins two tuples together (both elements must be tuples).

For instance, let's add some new data to the tuple we were working with in the last example.

Just a heads up: make sure the data you're adding is also in tuple format to successfully concatenate.

1234567
# 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 7

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Spørg mig spørgsmål om dette emne

Opsummér dette kapitel

Vis virkelige eksempler

Awesome!

Completion rate improved to 1.64

bookCommon Tuple Methods in Python

Stryg for at vise menuen

Let's dive into the primary methods associated with tuples.

  • len(t) - gives the number of elements in the tuple t;
  • t * n - produces n repetitions of the tuple t;
  • t.count(x) - counts how often x appears in the tuple t.

In the previous chapter, we highlighted that tuples are immutable. So, you might wonder: "How can I update a tuple with new values?"

Unlike lists, you can't use methods like .append() or .extend() with tuples. However, remember a few chapters back when we discussed concatenation? Here's the method that works for tuples too:

tuple1 + tuple2 - this joins two tuples together (both elements must be tuples).

For instance, let's add some new data to the tuple we were working with in the last example.

Just a heads up: make sure the data you're adding is also in tuple format to successfully concatenate.

1234567
# 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 7
some-alt