LearnWave
Learn it. Master it.

Getting Started with Python

Identifiers, variables, comments and objects

Choose valid, meaningful identifier names.

An identifier is a name given to a variable, function or other item in a program. The rules:

  • It must begin with a letter (a-z, A-Z) or an underscore (_).
  • After that, it can have letters, digits and underscores.
  • It cannot start with a digit.
  • It cannot contain special symbols such as !, @, #, $ or %, or spaces.
  • It cannot be a keyword: you cannot call a variable True or while.

So country, _total and marks2 are valid; 2marks and roll no are not. Choose meaningful names: marks1 and average tell a reader far more than a, b and c.

A variable is a name that refers to an object (a value) stored in memory. A variable is created when you first assign a value to it with an assignment statement:

  • country = "India"
  • rollNo = 25

A variable must be given a value before it is used in an expression; otherwise Python reports an error.

A comment starts with # and runs to the end of the line. The interpreter ignores comments; they are notes for people, explaining what the code does.

In Python, everything is an object, and each object has a unique identity, a little like its address in memory. The function id() returns it. If two variables refer to the same object, their ids are equal: after a = 15 and b = 20 - 5, both a and b refer to the object 15, so id(a) and id(b) are the same.

A short example program:

  • side = 6 # length of one side
  • perimeter = 4 * side
  • print(perimeter) # shows 24
Letters, digits, underscore; never a digit first.

Practise this lesson

Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.

No account, no email address. Opens the real product.