Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

😊

Python Guide
The print command allows the users message within the parentheses to be displayed on the screen.

We can also print multiple messages on the screen in this format:

However, this is not as efficient as it is easier to simply output our command in this format:

The use of the \n within the printed statement


prints the text after the \n in a new line.

Comments
Comments allow us for annotation of our work but are however ignored by the program upon
execution. Comments are made using the # symbol. We can also make multiple line comments using 3
apostrophes at the start and end of the comment.

As clearly seen upon execution, the program


ignored the comment in the code, and it is
not printed.

Variables
A variable is a container for data values. These values can change as the program is running as well.
Variables must start with a letter and do not need speech marks when printing their names.

Variables can also store string data values. Strings are created using
”(input text)”

We can also print variables within sentences using the + symbol. This is called concatenation.

Using the + symbol will add both the variable and the
sentence together without adding a
space in-between
Using a comma, however, will automatically add a space in between the sentences.

Tasks 1.

Inputting Text
We use the input command to allow the user to input data, which is automatically stored in a string.

In this example, the user’s input is saved to the variable ‘object’ and hence, the variable can be printed
alone or within a sentence for other purposes.
This can also be done with multiple variables to form longer sentences and outputs and can be used for
specific purposes.

Formatting Inputs
To enter whole numbers, you need the int command. (int = integer numbers, that is, whole numbers
without a decimal point)
Decimal numbers can be input to the program via the float command.

Tasks 2

Data Types
We have several types of data types such as:
String(str) – A data structure that represents a sequence of characters.
Integer – A whole number
Float- A decimal number
Boolean- A logical operation (True or False)
Casting
Converting variables from one data type to another is called casting.
Example: str(variable_name)
Int(variable_name)
float (variable name)
When doing arithmetic operations, the result is automatically given as a decimal number.
Casting a float into an integer will remove the decimal.
For example:

OR

Selection
Selection allows the user to make an input into the program. Statements such as the if and else
create conditions for these.

If statements
These statements perform an action based on a conditional value.

The use of the colon ‘:’ at the end of the if statements serve as a ‘THEN’ condition. Hence, in this
program, it would be:
IF path is equal ‘right’ THEN
Print ('You take the right path.... you suddenly come across a large wall.. Dead end!')
ELSE:
IF path ==’left’ THEN
Print (‘The path is clear, you've chosen the right path.. WELL DONE!’)

Nested IF statements
These are complex IF statements within IF statements (nesting).

The use of nesting can result in multiple results when multiple conditions are made.

Selection with numbers and comparison operators


Comparison operators such as these:

Can be used with IF statements as well. These create conditions that are based on the operator
(Symbol) and the users input.
Logical operators such as ‘AND’ & ‘OR’ can also be used to make the program more efficient.
Loops
Loops(iteration) is used to repeat a particular action or command.
- For Loops: example:

These loops

The use of ‘and’, ‘or’ creates two conditions as well which are used to repeat a
command(print(“Solace”)) for both affected by the number of times specified which input of the user.
Hence, ‘or’ can also be used in place of ‘and’ in this example is 5 times. ‘i’ measuresto change the
conditions.

Arithmetic operations
- Integer Division
Integer division removes any decimal numbers and points and results in a integer alone. This is
performed by using the symbol //

- Modulo Division
This division outputs the remainder of the operation alone and not the whole number. It is
performed by using the % symbol.

- Exponents (Powers)
This is basically raising to the power of another number of loops occurring. To see this, we can
record iterations such as in this. For example:, 2**2 is 2 raised to the power of 2 (2x2) which is 4.
The symbol for exponent is **
In this example the program displays the numbers between 1 and 11, but however, 1 is a
starting point and 11 is omitted at the end of execution. This is because 11 is what the program
runs up to but not including.

Like the above example, this program displays all the even numbers between 10 and 21 as
10 as the startup number.

Variables can also be used

Section 4 tasks

You might also like