MCS-201 · Unit 10

Data Structures and Control Statements in Python

Section 1 of 12

Block 3 · Unit 10Data Structures and Control Statements in Python9 min · Section 1 of 12

10.1Identifiers, Keywords, Statements and Expressions

Covers IGNOU MCS-201 §10.3, 10.4

Every program is made of names, reserved words, statements and expressions. This section sets out the rules for each, so that the rest of the unit can use them freely.

Identifiers

Rules for a valid identifier:

RuleValidInvalid
Made of letters, digits and underscores _mca_course, other_1my-name (hyphen)
Must not start with a digitplus11plus
No spaces or symbols such as !, @, #, $, %total_markstotal marks, cost$
Must not be a keywordclass_nameclass
Case-sensitiveIGNOU and Ignou are two different names
Any lengthnumber_of_students_enrolled

Python 3 also allows letters from other alphabets (for example नाम = "Nav" is valid), but English letters are the convention.

By convention (PEP 8, Python’s style guide), variables and functions use lower_case_with_underscores, classes use CapitalisedWords, and constants use UPPER_CASE.

Keywords

A keyword is a reserved word with a fixed meaning to the interpreter; it cannot be used as an identifier. Python 3 has 35 keywords. All are lowercase except True, False and None:

FalseNoneTrueandas
assertasyncawaitbreakclass
continuedefdelelifelse
exceptfinallyforfromglobal
ifimportinislambda
nonlocalnotorpassraise
returntrywhilewithyield

The list can be printed with import keyword; print(keyword.kwlist). Python also has soft keywords (match, case, type, _) that are reserved only in particular statements.

Statements and expressions

ExpressionStatement
What it isA combination of values, names and operators that produces a valueA complete instruction that Python executes
Examples25, x, z + 25, len("abc")x = 2, if x > 0: ..., import math, print(x)
In interactive modeIts value is displayedDisplays nothing unless it prints
In a scriptIts value is discarded unless printedRuns normally

The parts of an expression: in x = a + 5 - b, the names a and b and the value 5 are operands, + and - are operators, and a + 5 - b is the expression; the whole line is an assignment statement.

Key takeaways

  • Identifiers: letters, digits, _; no leading digit; not a keyword; case-sensitive; any length.
  • 35 keywords; all lowercase except True, False, None.
  • An expression evaluates to a value; a statement is an instruction.
  • In scripts, use print() to see values.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which of these is a valid identifier?

Show answer

Answer: C. _count2

It uses only letters, digits and underscores and does not start with a digit. 2nd_year starts with a digit, mca-2026 contains a hyphen, and for is a keyword.

Question 2 · multiple choice

Which keyword is NOT written in lowercase?

Show answer

Answer: A. None

True, False and None are the only capitalised keywords.

Question 3 · multiple choice

What does this script print?

Python · quiz_script.py
a = 4
a * 3
print(a)
Show answer

Answer: C. 4

a * 3 is computed and discarded; it does not change a, and a script does not display bare expressions.

Question 4 · multiple choice

In total = price * qty + 50, what is price * qty + 50?

Show answer

Answer: B. An expression

It combines operands and operators to produce a value; the whole line, including total =, is an assignment statement.

Question 5 · multiple choice

What happens when a script contains If x > 3:?

Show answer

Answer: D. A SyntaxError is reported

If is not a keyword, so Python cannot parse the line as an if statement.

Question 6 · short answer

State four rules for naming identifiers in Python, with one invalid example for each.

Show worked answer

(1) Only letters, digits and underscores may be used: roll-no is invalid because of the hyphen. (2) The name cannot begin with a digit: 3d_points is invalid. (3) A keyword cannot be used: while is invalid. (4) Spaces and special symbols are not allowed: total marks and rate% are invalid. Names are also case-sensitive, and there is no length limit.

↑ ↓ move↵ openTab switch type