Skip to main content

Preview Cambridge IGCSE Computer Science Programming Book for Python

Page 27

Chapter 3: Variables and Arithmetic Operators

3.04  Naming Conventions in Python There are a variety of naming conventions in Python. Here are a few of them:

Variable Names Use all lower case, starting with a letter and joining words with underscores. It is considered good practice to use descriptive names as this aids readability and reduces the need for so much commenting. For example: score_total = 56 Total = 56 t = 56

✓ ✗ ✗

FURTHER INFORMATION

There are 31 reserved words that cannot be used as your own variable names: and as assert break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass print raise return try while with yield.

Constants Use all upper case characters to indicate constants.

17

For example: PI = 3.1415 It is considered good practice to give global variables an initial value when declaring variables: this is known as initialising variables. See the next section for more about global and local variables. KEY TERMS

Declaring variables: When a variable is given a name and assigned no value. It is important to declare or initialise global variables.

Initialising variables: When a variable is given a start value.

3.05  Variable Scope When declaring a variable, the placement of the declaration in the code will determine which elements of the program are able to make use of the variable. Global variables are those that can be accessed from any routine within the program. To give a variable global status, it must be declared outside any specific subroutine. It is good practice to make all the global variable declarations at the start of your code directly below any import statements. To access global variables in functions, they can be called as normal; however, if the function is going to change the value stored in the global variable it must be re-declared using the global keyword (see example on page 18).

Variable Scope is outside the syllabus


Turn static files into dynamic content formats.

Create a flipbook