Python Language Tuple

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

A tuple is a immutable list of values. Tuples are one of Python's simplest and most common collection types, and can be created with the comma operator (value = 1, 2, 3).

Syntax

  • (1, a, "hello") # a must be a variable

  • () # an empty tuple

  • (1,) # a 1-element tuple. (1) is not a tuple.

  • 1, 2, 3 # the 3-element tuple (1, 2, 3)

Remarks

Parentheses are only needed for empty tuples or when used in a function call.

A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in that respect tuples are a lot like lists. The important difference is that tuples are immutable and are hashable, so they can be used in sets and maps



Got any Python Language Question?