0% found this document useful (0 votes)
7 views

CS G11 Python

The document provides an overview of Python programming, covering topics such as programming languages, variable declaration, data types, operators, input/output, casting, and control structures like selection and iterations. It explains basic concepts and syntax in Python, including examples for clarity. The content is aimed at Grade 11 students in a Computer Science course focused on algorithms and programming.

Uploaded by

xpcwwz4rhm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CS G11 Python

The document provides an overview of Python programming, covering topics such as programming languages, variable declaration, data types, operators, input/output, casting, and control structures like selection and iterations. It explains basic concepts and syntax in Python, including examples for clarity. The content is aimed at Grade 11 students in a Computer Science course focused on algorithms and programming.

Uploaded by

xpcwwz4rhm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Grade 11

Computer Science: Algorithms and Programming II


Python Programming Language
1. Programming Language:
a. A programming language is a computer language that is used by programmers
(developers) to communicate with computers. It is a set of instructions written in
any specific language (C, C++, Java, Python) to perform a specific task.
b. Python: Python is a high-level, general-purpose programming language. Its
design philosophy emphasizes code readability with the use of significant
indentation.
2. Declaring Variables:
In Python, a variable is created the moment you assign a value to it.
Example:

3. Data Types:
a. Integer: A non-decimal number.

b. Float: A decimal number.

c. String: collection of letters, digits, and symbols.

d. Boolean: Represents data that is either True or False.

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. Not equal !=

iii. Greater than >

iv. Greater than or equal to >=


v. Less than <

vi. Less than or equal to <=

c. Logical Operators: Operators that combine multiple Boolean expressions or


values and provide a single Boolean output.
i. and

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 (””).

If we want to print a variable, we don’t have to put it between quotation marks:

If we want to print a variable alongside a message we separate between them using a


comma (,) remember that the message has to be between quotation marks (”” ):

An error will occur if we don’t separate them using a comma:

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:

8. Control Structures: Selection (if statements):


The if statement lets you execute a sequence of instructions based on conditions. There
are 3 types of selection:
a. One Way Selection: Used when there is one set of statements:
Example: Write a program that will print a message only if the name of the
country is equal to Palestine.

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.

Example 1: Write a program that counts from 1 to 10.


Note: the variable i will iterate through each value of the range from 1 to 10 and print
each value.
Example 2:

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.

You might also like