If you want to jump straight into the code, simply execute the following command in the terminal to import the relevant python files.
git checkout Homework task5.py task6.py
Homework Notes
In this homework, we will learn about another variable type: Booleans. They can have only one of two values - True & False. Secondly, we are going to introduce the concept of the import statement.. We're also going to be doing some more stuff with lists and also introduce a new built-in function called len which allows us to count the elements in a list.
Booleans
Let's start with Booleans. There are a couple of ways to define a Boolean: directly, or with logic.
Booleans - direct definition
We directly define a Boolean in this example:
boolean_first = True
Note that the True or False statement must be without inverted commas (that would be a string) and with a capital first letter. For reasons we wont go into, strings (regardless of their value) are almost always True. If this is confusing and doesn't make sense, don't worry, we'll go into more later.
In Python, there are a couple more which are important:
== Equal to
!= Not equal to
Note that Equal to requires a double equal sign. This is to differentiate it from the definition operator which involves just a single equals sign (=).
Python import statement
In Python (and most other programming languages) we don't have to have all the code we are going to use in a single file. We can import functionality from another Python file. This Python file might not be written by us. In fact, we might never actually read this file or communicate with its author, we just know that we need it and how to use it.
Think of cars travelling around on roads throughout the world. Millions of cars are moving right now, each having some kind of engine inside producing movement. How many of the drivers of these cars know how the engine inside works? Or any of the other components for that matter. Most just know that there's an accelerator pedal, brake and steering wheel which when used have a predictable effect on the behaviour of the car. For all intents-and-purposes, the engine and other mechanical components are a magical black box with controls on the outside that are used to get some useful output. In a sense, when we use a car we are importing the functionality of an engine into our vehicle and then using it - they allow us to keep standardised functionality under the hood and away from every day work.
In this homework assignment, you are defining a variable in one file (in this case a list of Booleans in task5.py) and importing it into a second file (task6.py). We are going to need some Booleans for task 6: we could define some new ones, but we already have a list of them from task5, so why not use those?
The syntax of an import statement is as follows:
from module import variable/function
The module is just another file or a built-in module that we just need the name of (we'll cover this, don't worry. In further assignments, we will look into how to import a wider variety of variables, functions and other things that we can use.
Quick Start
If you want to jump straight into the code, simply execute the following command in the terminal to import the relevant python files.
git checkout Homework task5.py task6.py
Homework Notes
In this homework, we will learn about another variable type: Booleans. They can have only one of two values - True & False. Secondly, we are going to introduce the concept of the import statement.. We're also going to be doing some more stuff with lists and also introduce a new built-in function called len which allows us to count the elements in a list.
Booleans
Let's start with Booleans. There are a couple of ways to define a Boolean: directly, or with logic.
Booleans - direct definition
We directly define a Boolean in this example:
boolean_first = True
Note that the True or False statement must be without inverted commas (that would be a string) and with a capital first letter. For reasons we wont go into, strings (regardless of their value) are almost always True. If this is confusing and doesn't make sense, don't worry, we'll go into more later.
Booleans - with logic
You may be familiar with the following comparison operators:
In Python, there are a couple more which are important:
Note that Equal to requires a double equal sign. This is to differentiate it from the definition operator which involves just a single equals sign (=).
Python import statement
In Python (and most other programming languages) we don't have to have all the code we are going to use in a single file. We can import functionality from another Python file. This Python file might not be written by us. In fact, we might never actually read this file or communicate with its author, we just know that we need it and how to use it.
Think of cars travelling around on roads throughout the world. Millions of cars are moving right now, each having some kind of engine inside producing movement. How many of the drivers of these cars know how the engine inside works? Or any of the other components for that matter. Most just know that there's an accelerator pedal, brake and steering wheel which when used have a predictable effect on the behaviour of the car. For all intents-and-purposes, the engine and other mechanical components are a magical black box with controls on the outside that are used to get some useful output. In a sense, when we use a car we are importing the functionality of an engine into our vehicle and then using it - they allow us to keep standardised functionality under the hood and away from every day work.
In this homework assignment, you are defining a variable in one file (in this case a list of Booleans in task5.py) and importing it into a second file (task6.py). We are going to need some Booleans for task 6: we could define some new ones, but we already have a list of them from task5, so why not use those?
The syntax of an import statement is as follows:
from module import variable/function
The module is just another file or a built-in module that we just need the name of (we'll cover this, don't worry. In further assignments, we will look into how to import a wider variety of variables, functions and other things that we can use.