2.2 Programming Fundamentals
Variable
A named container, that stores a value in memory, that can change its value whilst the programming is running.
myVar = 0
Constant
A named container, that stores a value in memory, that cannot change its value whilst the program is running.
const myConst = 0 (note that python does not have a constant keyword)
Operators
+, -, , /, MOD,DIV, ^ (or *)
==, !=, <, <=, >, >=
Assignment
favNum = 5
Input
favNum = input(“Type in your number”)
Output
print(“your favourite number is”, favNum)
Integer
A whole number
1, 5, -259
Real / Float
Numbers which can, but do not necessarily, have a fractional part
1.5, -125.1
Boolean
TRUE or FALSE
(1 or 0)
Sequence
A set of instructions executed in order (line by line).
myVar = 0
print(myVar)
Selection
Instructions that do not have to be executed in sequence (non-sequential). The next instruction is based on the results of an event / condition.
if myVar < 0:
print (“negative!!”)
else:
print (“positive!!”)
Iteration Repetition of a block of statements within a program. |
Count-controlled iteration (fixed iterations) for i in range(5, 10, 3) print(“hello”, i)
|
Nested count-controlled iteration for i in range (5) for j in range(5) print(“hello”, i*j)
|
Condition-controlled iteration (indefinite) i = 5 while(i < 10) print(“hello”, i) i += 3 |
Character A single alphanumeric symbol ‘a’, ‘c’, ‘1’ |
String One or more alphanumeric characters “abcd”, “123”, “12/01”, “£3.50” |
Casting Changing how a variable’s data type is interpreted / a temporary conversion of data type.
datatype( yourVariable ) i.e. favNumString = str(favNum) print(“your favourite number is” + favNumString) |
Subroutines A block of indented code that is executed whenever the name/identifier is called in the program. It may or may not have arguments input to it when called. Subroutines: allow code to be reused multiple times; make it easier to debug code (more maintainable); split the code up to allow programmers to work on different parts at once.
def mySubroutine(): print(“You have called mySubroutine!!”)
for i in range(5) mySubroutine()
| |
Functions A subroutine that returns a value
def mySubroutine(x, y) return (x + y) * x
| Procedures A subroutine that does not return a value
def mySubroutine(x, y) print((x + y) * x) |
Arrays (& array functions)
favFoods = [“Chicken”, “Fish and Chips”]
favFoods.append(“Pizza”)
favFoods.pop(1)
print(favFoods[1])
favDrinks = [“Water”, “Orange Juice”]
favThings = favFoods + favDrinks
Variable
A named container, that stores a value in memory, that can change its value whilst the programming is running.
myVar = 0
Constant
A named container, that stores a value in memory, that cannot change its value whilst the program is running.
const myConst = 0 (note that python does not have a constant keyword)
Operators
+, -, , /, MOD,DIV, ^ (or *)
==, !=, <, <=, >, >=
Assignment
favNum = 5
Input
favNum = input(“Type in your number”)
Output
print(“your favourite number is”, favNum)
Integer
A whole number
1, 5, -259
Real / Float
Numbers which can, but do not necessarily, have a fractional part
1.5, -125.1
Boolean
TRUE or FALSE
(1 or 0)
Sequence
A set of instructions executed in order (line by line).
myVar = 0
print(myVar)
Selection
Instructions that do not have to be executed in sequence (non-sequential). The next instruction is based on the results of an event / condition.
if myVar < 0:
print (“negative!!”)
else:
print (“positive!!”)
Iteration Repetition of a block of statements within a program. |
Count-controlled iteration (fixed iterations) for i in range(5, 10, 3) print(“hello”, i)
|
Nested count-controlled iteration for i in range (5) for j in range(5) print(“hello”, i*j)
|
Condition-controlled iteration (indefinite) i = 5 while(i < 10) print(“hello”, i) i += 3 |
Character A single alphanumeric symbol ‘a’, ‘c’, ‘1’ |
String One or more alphanumeric characters “abcd”, “123”, “12/01”, “£3.50” |
Casting Changing how a variable’s data type is interpreted / a temporary conversion of data type.
datatype( yourVariable ) i.e. favNumString = str(favNum) print(“your favourite number is” + favNumString) |
Subroutines A block of indented code that is executed whenever the name/identifier is called in the program. It may or may not have arguments input to it when called. Subroutines: allow code to be reused multiple times; make it easier to debug code (more maintainable); split the code up to allow programmers to work on different parts at once.
def mySubroutine(): print(“You have called mySubroutine!!”)
for i in range(5) mySubroutine()
| |
Functions A subroutine that returns a value
def mySubroutine(x, y) return (x + y) * x
| Procedures A subroutine that does not return a value
def mySubroutine(x, y) print((x + y) * x) |
Arrays (& array functions)
favFoods = [“Chicken”, “Fish and Chips”]
favFoods.append(“Pizza”)
favFoods.pop(1)
print(favFoods[1])
favDrinks = [“Water”, “Orange Juice”]
favThings = favFoods + favDrinks