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: […]
Category: Uncategorized
You can check a variable for many different conditions using ELIF Tasks Modify your IF ELSE program to use the ELIF statement to check for the letters a-f Save and label your code
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 […]
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 […]
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 […]
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 […]
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 […]
Main Task The formula for the volume of a cylinder is πr2h The formula for the surface area is (2πr2) + (2πrh) Create a program that will: Ask for the radius and height of a cylinder Calculate the volume and surface area Output the values separately Save and label your code Extension Task Create a […]
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 […]
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 […]