One of the things that makes a computer useful is the ability to repeat an operation many times over at high speeds. This is called iteration or looping The FOR loop allows us to carry out a set of instructions a specific number of times The code within the loop must be indented The range […]
Author: Daniel Norfolk
All the different ways of checking strings and characters on the last page return either TRUE (1) or FALSE (0). This means that they can be used with IF statements If you played around with the input string, you should have realised that islower() and isupper() only return true when the entire string is a […]
There are other useful functions for checking and manipulating characters and strings Reversing a string Sometimes you might need to reverse a string. This may be to make a working with binary strings easier, or to help create random passwords. Tasks Type in the code and make sure that it works Add code to reverse […]
Characters can be checked and compared in much the same way as numbers, using the same symbols Less than (<) and greater than (>) work if you think about the alphabet as going from low (a) to high (z) Remember that lowercase and uppercase letters are recognised as different characters by the computer Symbol Example […]
We can check strings using the same methods as with numbers A character is simply a string with a length of 1 The in operator is a quick way of checking if one string or character is contained within another – useful to check user input against an allowed list of characters or symbols Some […]
Main Task Create a basic calculator program that will take two numbers, and perform one of the following functions depending on what the user chooses: add subtract multiply divide You should assume that all user input will be valid whole numbers. You should ask the user which function they wish to perform. The output should […]
Logical operators allow you to combine checks on variables The two operators are OR and AND OR will be true if either condition is met AND will be true when both conditions are met In Python, they are always written in lowercase Tasks Type in the code and test that it works Modify the program […]
It is very important to be able to check the type of data entered If you try to convert data to a type that won’t ‘fit’, your program will crash The code below will crash if a non-numeric input is supplied The example below uses the Try/Except statement to catch the error The program attempts […]
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: […]
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