Basic arithmetic operators
Before we start with any code, it is a good idea to be familiar with a few operators that we will be using in Python.
The first four will likely be very familiar to you:
The first four will likely be very familiar to you:
Operator |
Description |
+ |
Used to add two values together or join a string (words) |
- |
Used to subtract a value from another |
/ |
Used to divide two numbers |
* |
Used to multiply two numbers or strings (words) |
Activities
Use the Python console to test these operators mathematically. Here are a few examples below to get you started, but also try out some of your own. Can you cause an error? If you do, don't worry, you haven't broken anything!
A few more arithmetic operators
So you have looked at the basic operators in the previous activity, but there are a few others that will come in useful with the tasks to come.
Activities
Find out what the following operators do by experimenting with them using numbers in the Python console. For example:
3 // 7
Once you have worked out what they do, start taking notes on what you have learnt so far with examples that you have tried. This will help consolidate your learning and will be a good reference source when you come to tackling some of the programming tasks later on.
3 // 7
Once you have worked out what they do, start taking notes on what you have learnt so far with examples that you have tried. This will help consolidate your learning and will be a good reference source when you come to tackling some of the programming tasks later on.
Operator |
Description |
// |
? |
** |
? |
% |
? |
If you get really stuck, you can look the answers up here.
Your first two functions and an introduction to variables
Activities
Time to write your first program. From the Python console click File -> New File
Using an input() function, write a question to ask the user to input some information. It could be their name, age or something completely different. Remember to store the information they give you inside a suitably named variable. When the information has been input, output whatever was typed to the screen like the example opposite. If you are really stuck, click on the photo to see the code. When you have finished, update your notes to include the print() and input() function |
|
Data types and casting
Activities
1. Adapt your program from the last activity. This time ask for the user's name and then greet the user when you output
2. Write a similar program, but this time ask for two numbers, add them together, and then output the answer
3. Extend the code from the last question to include a better output for the user.
Update your notes before continuing.
Assessment Task 1
- Create a program that calculates the area of a circle.
- Create a program where the user inputs the value of an item they wish to purchase and the program outputs the cost with VAT added at 17.5%. The price should be output with the pound sign in front of it, or "GBP" after the price.
- Complete quiz 1
- Complete quiz 2
Extra Activities
- Write a program that asks for the width and height of a rectangle and calculates the area
- Write a program that outputs a famous quote. The person who said it and the quote should be stored in separate variables and then joined together in a suitable output
- Write a program to ask for the user's age and then calculate and output the year they were born