Category: Uncategorized

18. Checking numbers

Checking if a number is equal is useful, but we need to be able to do more! Relational operators allow us to perform a range of checks on variables Remember when we are comparing, use two equals rather than one! Symbol Example Operation == if x == 10: equal to < if x < 10: […]

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 […]