3.1 - Fundamentals of programming

Table of Contents

1 Programming

Arithmetic operations in a programming language

Arithmetic operators included in Python:

  • + addition
  • - subtraction
  • * multplication
  • / division
  • % modular
  • // floor division

Relational operations in a programming language

A relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. In python:

  • == equal in value
  • >= greater than or equal
  • =< less than or equal
  • != not equal
  • > greater than
  • < less than

Boolean operations in a programming language

Logical operators are mainly used to control program flow based on the outcome of the result of the logical operations. In Python:

  • AND
  • or
  • not

Constants and variables in a programming language

  • Constants or names constants are variables whose values will not be changed by the program.
  • The advantage of having constants or named constants is:
    • Improves readability of code
    • Easier to update the programming code if the value changes
    • Reduce the likelihood of inconsistency causing errors if multiple places use the same value instead of using the same named constant.

String-handling operations in a programming language

  • be familiar with the following common Python string operations:
    • concatenation
    • upper()
    • lower()
    • split()
    • list()

Random number generation in a programming language

  • random module is used to generate random numbers.
  • it can be used to pick a random item from a collection.

Exception handling- defensive programming

  • Exception handling is the process of what code does when exception happens during execution, so that the code will not simply crash. This allows the change of program flow.
  • For example: when open a file, user may enter the file name wrong. This exception could be handled by asking the user re-input the file.
  • Exception handling makes program more robust.
  • Defensive programming is about ensuring the continuing function of a piece of software under unforeseen circumstances. For example, checking arguments to a function or method for validity before executing the body of the function, after executing the function, checking the return value or state is expected.

Subroutines (procedures/functions)

  • Subroutines are segements of code that perform a specific purpose in a program.
  • Subroutines allow programmers
    • to organise their code in easy to follow manner
    • to debug their code easily by focusing on one subroutine at a time
    • to reduce code redundancy. The same subroutines can be used again and again in the same program or by other programs.
    • to increase code efficiency.
  • procedures are subroutines that do not return any values after execution.
  • functions are subroutines that do return values after execution.
  • subroutines can accept parameters from the caller. For example, the following function accept two parameters and return one value:
def areas(width, height):
    area = width * height
    return area
  • in Python, you can also do the following:
def areas(width, height):

    return areawidth * height

Returning a value/values from a subroutine

  • return a single value from a subroutine in Python:
def getCircleArea(r):
    return 3.14*r**2
  • return two values from a subroutine in Python:
board=[["w","d","c"],
       ["a", "b", "z"]]
def getCoordinate(letter):
    for y,rows in enumerate(board):
	for x, C in enumerate(rows):         
	    if letter == board[y][x]:
		 return y,x

Local variables in subroutines

  • for the above example, the board is a global variable as it was defined outside subroutines.
  • local variables are defined inside a subroutine and can only be accessed inside the subroutine. Once the subroutine finishes, all local variables are removed from memory.