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

The calculus chapter ended by pointing here: away from smooth curves and toward a few lines of code that must run exactly. So let us be precise about the rules. GATE DA does not test C — its 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, character for character. There is no coding from scratch, only careful reading.

This lesson builds the two skills that win those marks: knowing the types and reading slices. Both pay off far past the exam — every pandas and NumPy pipeline you will ever write rests on this exact slicing and int-versus-float intuition, so the careful reading you practise now becomes the careful coding you do later.

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?” The falsy values are 0, 0.0, "" (empty string), [] (empty list), and None. Everything else is truthy — including -1 and "False" (a non-empty string, so it counts as true).

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 the floor of -3.5 is -4, not -3.)
  • ** 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 — two rulers laid over the same characters.

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.

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-versus-int division correctly under time pressure. There is no library to recall — only precise evaluation, exactly the discipline the last chapter asked you to carry over.

Worked example

Trace this snippet one slice at a time, reading each against the index diagram above:

s = "GATE2026"
print(s[1:4])    # start at 1 (A), stop before 4 → indices 1,2,3
print(s[-2:])    # two from the end, to the end → indices -2,-1
print(s[::-1])   # step -1, the whole string backward
print(s[::2])    # every second char from index 0 → indices 0,2,4,6
print(s[2:100])  # stop 100 is past the end — clamped, no error

Taking them in turn:

  • 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), run to the end. Characters at -2 and -126.
  • s[::-1] — no start or 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.
  • s[2:100] — from index 2 (T) to the end; the out-of-range 100 is silently clamped → TE2026 (no error).

So the five lines print, exactly:

ATE
26
6202ETAG
GT22
TE2026

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

A question to carry forward

A string is the gentlest sequence there is: read-only, one character per slot, and you can only look at it, never change it. But the real workhorses of Python are its collections — lists you can change, dictionaries that map keys to values, sets that quietly drop duplicates. Each brings its own behaviour, and one of them hides a trap sharp enough that GATE returns to it year after year. Here is the thread onward: when you write b = a and a is a list, do you get a second list — or just a second name pointing at the very same one, so that changing b silently changes a too?

In one breath

  • GATE DA programming is Python, and the format is predict the output — careful reading, not coding.
  • Two divisions: / is true division, always a float (4/2 = 2.0); // floors toward −∞ (7//2 = 3, −7//2 = −4). ** is power; ^ is XOR, not power.
  • Falsy: 0, 0.0, "", [], None — everything else (incl. -1, "False") is truthy.
  • Slicing s[start:stop:step]: start included, stop excluded, step default 1; s[::-1] reverses, s[::2] takes every other.
  • Slicing forgives an out-of-range bound (clamps, e.g. s[2:100]); indexing out of range raises IndexError.

Practice

Quick check

0/6
Q1Recall: which statements about Python slicing and types are TRUE? (select all that apply)select all that apply
Q2Recall: what is the value of 7 // 2 in Python? (integer)numerical answer — type a number
Q3Trace: for s = "GATE2026", how many characters does the slice s[1:4] contain? (integer)numerical answer — type a number
Q4Trace: what does print(s[::-1]) output for s = "GATE2026"?
Q5Apply: which of these expressions evaluate to a float? (select all that apply)select all that apply
Q6Create: what is the output of print(s[2:100]) for s = "GATE2026", and why no error?

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.

Related lessons

Explore further

Skip to content