CS G11 Python
CS G11 Python
3. Data Types:
a. Integer: A non-decimal number.
These are the basic data types in Python, we will dive into more later in the course.
4. Operators:
a. Arithmetic Operators:
i. Addition +
ii. Subtraction –
iii. Multiplication *
iv. Division /
b. Comparison Operators: Operators that compare values and return True or False.
i. Equal ==
ii. or
iii. not
Input Output
A B A AND B A OR B NOT A
TRUE TRUE TRUE TRUE FALSE
TRUE FALSE FALSE TRUE FALSE
FALSE TRUE FALSE TRUE TRUE
FALSE FALSE FALSE FALSE TRUE
5. Output:
Outputs are data sent from a system.
Note: Notice that the message is put between quotations marks (””).
A Syntax Error is an error that occurs when we don’t follow the Python language syntax.
6. Input:
Inputs are data received by the system.
In this example, the user is asked to write their name. When it’s written, the message is
shown on the console.
7. Casting:
There may be times when you want to specify a type on to a variable. This can be done
with casting.
a. int(): constructs an integer from an integer, a float (by removing all decimals), or
a string (if the string represents a whole number (an integer)):
Example 1:
In this example, we’re transforming from string to integer, because the string n
represents a whole number so we’re specifying that we need an integer.
Example 2:
We notice in this example that the string n doesn’t represent a whole number, it
represents a word (string). That’s why it gives an error as a result.
Example 3:
We notice that the numbers were not added but they were concatenated
instead. This is due to the input function reading all data as a string.
How can we fix it?
We can fix it by adding the int() function to the input() function:
Notice that when we used casting with the int() function the numbers are added
normally.
b. float(): Constructs a float from a float, an integer, or a float (if the string
represents a float or an integer):
Example 1:
Example 2:
Example 3:
In this example, the code returns an error because the variable n doesn’t
represent a float or an integer, it represents a string.
c. str(): Constructs a string from a wide variety of data types, this includes strings,
integers, and floats:
Example 1:
Example 2:
Example 3:
Note 1: If we write anything other than Palestine, the code will return nothing.
Note 2: Notice that when writing the if statement in Python we need to put a
colon (:) after the condition. We also need to put an indentation before writing
instructions to let the machine know that the instructions are part of the if
statement. If we ignore these rules, the code will return an error. For example:
A/
B/
b. Two Way Selection: Used when there are two sets of statements:
Example: Write a program that will check if a number is even or odd.
Note 1: When we have 2 sets of statements, we write the first one after if +
condition, and the second one after else.
Note 2: The colon and indentation are also valid for this as they are part of the
Python syntax.
c. Multi Way Selection: Used when we have more than 2 sets of statements:
Example: Write a program that will show the price of concert tickets based on
the age input by the user. If their age is between 1 and 16, the price is 100£, if
their age is between 17 and 30 the price is 200£, and if their age is greater than
30, the price is 300£.
Note 1: When we have more than two sets of statements, we write the first one
after if + condition, the next ones (no matter how many there are) after elif +
condition, and the last one after else. We don’t have to write a condition after
else.
Note 2: The colon and indentation are always valis as they are part of the Python
syntax.
9. Control Structure: Iterations: For Loops:
Used to repeat an instruction or a sequence a known number of times.
Note 1: Lists are used to store multiple items in a single variable. Lists are one of the
built-in data types in Python. In this example, the variable students is the list.
Note 2: the variable s will iterate through each item of the list and print its value.
10. Control Structure: Iterations: While Loops:
Used to repeat an instruction or a sequence if a condition is true, the loop stops
once the condition becomes false.
Example:
Note: In this example, the condition for the loop to continue is that n must be less than
6. The loop starts from n=1. the value of n is increased by 1 after each iteration. Once
n=6, the condition becomes false, and the program stops as a result.