Author: Daniel Norfolk

16. IF…ELSE

The ELSE statement is added at the bottom of the IF statement to do something different if the condition is not met The example will execute one of the print statements, not both Tasks Modify your IF Selection program to use the ELSE statement Save and label your code Extension Task How could you use […]

15. Selection with IF

IF is the first conditional statement you will use It allows you to execute different code depending on the circumstances Look how the code is indented to identify it is part of the IF statement Tasks Type in the code above and make sure it works Modify the program to accept a character typed in […]

14. Random numbers

Sometimes you need to create random numbers to make ‘lucky dip’ choices from lists To create a random integer (whole number) you can use the code below: Tasks Create a program to simulate a lottery draw. It should generate 6 numbers between 1-49 and print them appropriately Run the program multiple times. Why will this […]

13. Decimal places

Sometimes, you need to print out numbers with a set number of decimal places This method is useful when working with currency values or when you are dividing numbers that may create answers with many decimal places Tasks Enter the code above and test it works Modify the code to print to 2 decimal places […]

12. Comments and blank lines

It is good practice to layout your code so it can be easily understood by others This includes using comments and blank lines to space oout your code and label it to explain what it does Comment lines are ignored by the interpreter. They are used to write information useful to someone reading the code […]

10. Working with numbers

Arithmetic operations are easy in Python Common operators to use with numbers are as follows: Symbol Example Operation () (a+b)/c Parenthesis * a*b Multiplication / a/b Division % a%b Modulus + a+b Addition – a-b Subtraction = a=b Assignment Tasks Write a program that asks for the width and height of a rectangle, and outputs […]

9. Numbers

Variables detect what data is in them if you assign them values. Note strings are declared within speech marks “” If you input from the keyboard, the computer will always treat input as a string. You can save some time by casting (converting) the data type of variable when you ask for the input Tasks […]

8. Variable conversion Pt. 2

Numbers can be integer (no decimal point) or float (decimal point) The example below declares two variables, one of each type and prints them as integers, floating point numbers and as strings if no type is specified, the variable will print as it was input Tasks Type in the code and test that it works […]

7. Variable conversion Pt. 1

Variables contain either string, integer or float data All user input is stored as a string by default, meaning an extra step is required to convert the input to the correct data type For example, the program below will not output the expected result (doubling the age entered) This is because you shouldn’t add a […]