Tamil Nadu HSE First Year Computer Science study materials
Tamil Nadu HSE First Year Computer Science study resources

Tamil Nadu HSE First Year Computer Science: Control Structures and Functions

Tamil Nadu HSE First Year Computer Science · Work through your exam, one topic at a time.

All guide sections

Control Structures and Functions

Control Structures and Functions tests selection, iteration and user-defined functions: if/elif/else, while, for with range(), loop control, parameter passing, return values and scope.

Core rules

  • Python uses indentation, not braces, to define blocks. Inconsistent indentation is a syntax or logic error.
  • if/elif/else tests conditions in order and runs the first true branch only.
  • for iterates over a sequence; range(start, stop, step) excludes stop. With one argument, it starts at 0.
  • while repeats while its condition remains true; ensure something changes inside the loop.
  • break exits the nearest loop, continue skips to the next iteration, and pass does nothing.
  • A loop's else block runs only if the loop finished without break.
  • def creates a function. Parameters can be positional, keyword or default. A function without return returns None.
  • Variables assigned inside a function are local unless declared global.

Worked example

def greet(name, msg='Hello'):
    return msg + ', ' + name

print(greet('Ravi'))
print(greet('Ravi', 'Hi'))

The first call uses the default msg, so it prints Hello, Ravi. The second passes 'Hi' positionally, overriding the default, so it prints Hi, Ravi. The function returns a string; it does not print inside itself, so the print() calls are required.

Traps that cost marks

  • Forgetting that range(1, 5) produces 1, 2, 3, 4, not 5.
  • Using = instead of == in a condition.
  • Placing return inside a loop when the final result is needed after the loop.
  • Assuming a local variable changes the global variable of the same name.
  • Ignoring the colon after if, for, while and def.