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
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.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?” 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:
/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 the floor of-3.5is-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)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 — two rulers laid over the same characters.
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.
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 index1(A), stop before index4. Indices 1, 2, 3 giveA,T,E→ATE.s[-2:]— start two from the end (index-2, the second2), run to the end. Characters at-2and-1→26.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,2→GT22.s[2:100]— from index 2 (T) to the end; the out-of-range100is 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]:startincluded,stopexcluded,stepdefault1;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 raisesIndexError.