datarekha

Python for GATE: Types & Slicing

GATE DA programming is in Python, and the signature question is predict the output. Master types, division, and slicing first.

7 min read Beginner GATE DA Lesson 47 of 122

What you'll learn

  • Python's core types: int, float, str, bool, and how truthiness works
  • Integer vs float division: // floors to an int-style result, / always gives a float
  • Indexing and slicing s[start:stop:step] with stop EXCLUSIVE and negative indices
  • Reading a snippet and predicting its printed output — the GATE DA signature skill

Before you start

GATE DA does not test C. The programming questions are in Python, and the signature format is predict the output: you are shown a short snippet and must say exactly what it prints. No coding from scratch — just careful reading. This lesson builds the two skills that win those marks: knowing the types and reading slices. Both pay off well past the exam — every pandas and NumPy data pipeline you will ever write rests on this exact slicing and int-vs-float intuition.

Types, truthiness, and the two divisions

Every value has a type. The four you meet constantly:

  • int — whole numbers: 42, -7.
  • float — decimals: 3.14, 2.0 (note the dot — 2.0 is a float, 2 is an int).
  • str — text in quotes: "GATE", 'da'.
  • boolTrue or False.

Truthiness: when a value is used as a condition, Python asks “is this truthy?” Falsy values are 0, 0.0, "" (empty string), [] (empty list), and None. Everything else is truthy — including -1 and "False" (a non-empty string).

The exam-favourite operators:

7 / 2true division3.5always a float7 // 2floor division3floors toward −∞2 ** 10power1024double star, not caret
Two divisions and a power: the operators GATE most loves to slip into output questions.
  • / is true division and always returns a float: 7 / 2 = 3.5, and even 4 / 2 = 2.0.
  • // is floor division: it divides then floors toward negative infinity. 7 // 2 = 3. (Watch negatives: -7 // 2 = -4, because floor of -3.5 is -4.)
  • ** is exponentiation: 2 ** 10 = 1024. The caret ^ is not power in Python — it is bitwise XOR, a classic trap for ex-C programmers.
  • len(x) gives the number of items; range(n) yields 0, 1, ..., n-1.

Indexing and slicing

A string or list is a sequence you can index. Positions count from 0 on the left and from -1 on the right.

GATE202601234567-8-7-6-5-4-3-2-1s[start : stop : step]stop is EXCLUSIVE; step defaults to 1; a negative step walks backward
Top row: characters. Middle: positive indices. Bottom: negative indices.

A slice s[start:stop:step] returns a new sub-sequence:

  • It includes start but stops before stop (stop is exclusive).
  • step defaults to 1; omit a part and the default fills in (s[:3] is from the start, s[3:] is to the end, s[:] is a full copy).
  • A negative step walks backward, so s[::-1] reverses the whole sequence.
  • s[::2] takes every second item starting at index 0.

Play with it — change the slice and watch the output:

How GATE asks this

The pattern is predict the output, posed as an MCQ (four candidate outputs) or a NAT (give the printed integer, or the length of a slice). Typical wording: “What is the output of the following code?” with a one- or two-line snippet using a slice, //, or **. The test is whether you apply exclusive stop, negative indices, and float vs int division correctly under time pressure. There is no library to recall — just precise evaluation.

Worked example

Trace this line one slice at a time, against the index diagram above:

s = "GATE2026"
print(s[1:4], s[-2:], s[::-1], s[::2])
  • s[1:4] — start at index 1 (A), stop before index 4. Indices 1, 2, 3 give A, T, EATE.
  • s[-2:] — start two from the end (index -2, the second 2), go to the end. Characters at -2 and -126.
  • s[::-1] — no start/stop, step -1, so walk the whole string backward → 6202ETAG.
  • s[::2] — every second character from index 0: indices 0, 2, 4, 6 → G, T, 2, 2GT22.

So print (which joins its arguments with spaces) shows:

ATE 26 6202ETAG GT22

The fourth slice is the one that trips people up: index 6 of "GATE2026" is the second 2, not the 6 — so it is GT22, not GT26.

Quick check

Quick check

0/6
Q1What is the value of 7 // 2 in Python? (integer)numerical answer — type a number
Q2For s = "GATE2026", how many characters does the slice s[1:4] contain? (integer)numerical answer — type a number
Q3What does print(s[::-1]) output for s = "GATE2026"?
Q4Which of these expressions evaluate to a float? (select all that apply)select all that apply
Q5Which statements about Python slicing and types are TRUE? (select all that apply)select all that apply
Q6What is the output of print(s[2:100]) for s = "GATE2026"?

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Explore further

Related lessons

Skip to content