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.
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.0is a float,2is an int).str— text in quotes:"GATE",'da'.bool—TrueorFalse.
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:
/is true division and always returns a float:7 / 2 = 3.5, and even4 / 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.5is-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)yields0, 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.
A slice s[start:stop:step] returns a new sub-sequence:
- It includes
startbut stops beforestop(stop is exclusive). stepdefaults to1; 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 index0.
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 giveA,T,E→ATE.s[-2:]— start two from the end (index-2, the second2), go to the end. Characters at-2and-1→26.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,2→GT22.
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.