LearnFrenzy
Sign in Take a free test

Python Programming · Topic 8 of 25

Tuples Questions and Concepts

  • 4 exercises
  • 50 questions
  • ~60 min
  • Answers explained

Coding Techniques

Tuples · Study material

zip() builds tuples; zip(*pairs) takes them apart

zip pairs items from several sequences as tuples. Spreading the pairs back into zip with * transposes them – the classic "unzip".

names = ("Asha", "Ravi", "Meera")
marks = (91, 78, 85)

pairs = list(zip(names, marks))          # zip builds tuples
print(pairs)

n, m = zip(*pairs)                       # "unzip": * spreads the pairs as arguments
print(n, m)

lookup = dict(pairs)                     # pairs → dict
print(lookup["Meera"])

Output

[('Asha', 91), ('Ravi', 78), ('Meera', 85)]
('Asha', 'Ravi', 'Meera') (91, 78, 85)
85

Tuple or list?

Use a tuple when…Use a list when…
the items form one record: (name, age, city)the items are many things of the same kind
the size is fixeditems are added or removed
it must be a dict key or set itemyou need sort(), append(), remove()
you return several values from a functionyou build results in a loop

Safe unpacking patterns

line = "Asha,21,CSE,Pune"

name, age, *rest = line.split(",")     # tolerates extra fields
first, _, third, _ = line.split(",")   # _ for values you ignore

row = ("Ravi", 78)
if len(row) == 2:                      # check the shape when input is untrusted
    name, marks = row

Tuples checklist

  • Always add the comma for one-item tuples.
  • Unpack instead of indexing: name, age = row.
  • Use a starred name when the length can vary.
  • Use namedtuple (or a dataclass) for readable records.
  • Keep only immutable items in tuples used as keys.
  • Convert with tuple(sorted(t)) when you need a sorted tuple.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Python Programming

Why it matters

  • Python is the most popular first language and the default for data science, AI, automation and scripting
  • Campus coding rounds and technical interviews increasingly allow – or prefer – Python
  • "Predict the output" questions on lists, strings, dictionaries and mutable defaults are a favourite in written tests
  • Clean Python with comprehensions, generators and OOP is what product companies expect from freshers

Who it is for

  • School students (Class 11–12 computer science with Python) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, coding tests and Python interviews
  • Anyone moving into data science, AI or automation who needs strong Python fundamentals

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs: syntax errors, runtime exceptions and logical bugs
  • Coding problems with solutions and real output, every one run on Python 3 – open any example in the online Python compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture tests
  • Python interviews for freshers
  • Data science interviews
  • CBSE / university practicals
  • GATE CS

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, errors, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links