Slides badge

Constructs

  • Describe, exemplify and implement high-level constructs in Python

Success Criteria

  • Be able to use and describe several different computational constructs in Python

Learning Intentions

Programming Languages

  • All programming languages are a series of instructions that follow a order.

  • Programming languages are compiled from a high-level language (such as Python) to machine code (made up of binary 1s and 0s).

  • Some computer languages, such as PHP and Python, have a conversion step in between called op-code or byte-code.

Programming Languages

  • Programming languages such as Python are made of constructs which are made up of expressions (you'll see this as we go on)

Assignment

  • Assignment is one of the simplest constructs. In Python, variables do not have to be declared before they are assigned:

variable1 = "Hello class"
variable2 = 231

Image copyright jamiebalfour.scot

Mathematical Expressions

  • Computers are very good at mathematical calculations.
  • Programming languages take advantage of this.
  • To do this, we simply write the mathematical expression using the correct operators:
Symbol Name Purpose Example
+ Plus Addition 5 + 6
- Minus Subtraction x - 10
* Times Multiplication 5 * var
/ Divide Division var / 4
var1 = int(input())
print(var1 * 5)

mATHEMATICAL EXPRESSIONS

  • Computers are very good at mathematical calculations.
  • Programming languages take advantage of this.
  • To do this, we simply write the mathematical expression using the correct operators:
Symbol Name Purpose Example
+ Plus Addition 5 + 6
- Minus Subtraction x - 10
* Times Multiplication 5 * var
/ Divide Division var / 4
var1 = int(input())
print(var1 * 5)

Relational operators

  • Computers can also very quickly perform comparisons.
  • By comparison, we are talking about checking if something is equal to, greater than or not equal to.
Actual symbol Python symbol Name Example
= == Equal to 5 == 5
> > Greater than x > 10
< < Less than y < 6
!= Not equal to var ≠ 4
>= Greater than or equal to 6 >= 6
6 >= 3
<= Less than or equal to 6 <= 6
4 <= 6

String concatenation

  • As mentioned before strings can be concatenated (joined) together in Python:

firstName = "Joe"
surName = "Balfour"
output = firstName + surName
print(output)

Flow Control

Flow Control

  • Flow control is used to control the flow of the program.
  • At National 5 you need to know about the most important flow control structures:
    • Sequence
    • Selection
    • Repetition

Sequence

  • Sequence is the order in which instructions are executed. For example, in programming terms, if you were to try and access the value in a variable before declaring the variable, the program would crash.
number1 = int(input())
answer = number1 + number2
number2 = int(input())

Selection

  • Selection is another flow control construct that is very common.
  • Selection is all about making decisions based some condition. 
  • A condition is either True or False.
  • The most common type of selection construct is the if statement.
  • If is a type of conditional statement.
  • If a condition evaluates to False, an Else statement will run (if specified).

Conditions

  • A condition is a expression that evaluates to True or False. 
    • For example, 5 > 10, 6 == x
  • A complex condition is a condition that contains And/Or:
    • For example, 5 > 10 And 6 > 3, gives us True 

Truth Tables

  • Truth tables make it easy to figure out the result of a condition and what path a selection statement will take.
  • For this you have two variables, A and B, which each have their own values. 
A B A And B A Or B
True True True True
True False False True
False True False True
False False False False

Truth Tables

  • Not is another keyword and logical operator available in most programming languages.
  • If not is applied to a Boolean value, it flips it, so True becomes False and False becomes True. 
A Not (A)
True False
False True

Truth Tables

  • When using the not keyword in Python, you can use it as a function by writing not(x):
print(not(5 > 10 And 10 < 4))
  • And you can also check if something is not equal to using != sign:
print(5 != 10)

For each of the following complex conditions, state whether it is true or false:

 

  1. True And False
  2. False Or True
  3. False And False
  4. 5 > 10 Or 3 < 10
  5. 5 == 10 And 50 > 40
  6. not(5 > 10 Or 3 != 10)
  7. 5 != 9 And 5 >= 5

tRUTH TABLES

Selection

number1 = int(input())

if number1 > 10:
  print("Greater than 10!")
else:
  print("Not greater than 10")
  1. What would be displayed if the number 5 is entered?
  2. What would be displayed if the number 12 is entered?
  3. What would be displayed if the number 10 is entered?

Repetition

  • Repetition or iteration is perhaps the most useful construct available to programmers.
  • Computers are very quick at repeating something several times over and much faster than humans.
  • Repetition or iteration is also known a looping.
  • A loop can be either a fixed loop or a conditional loop.

Fixed loops

  • A fixed loop is a loop which repeats a specified number of times. In Scratch, this is called the repeat block.
  • In Python, this is known as a for loop:

Image copyright jamiebalfour.scot

Fixed loops

max = 10
for i in range(0, max):
  print(i)
  • A fixed loop is a loop which repeats a specified number of times. In Scratch, this is called the repeat block.
  • In Python this is written as shown below:

Open Python and create the following programs:

 

  1. A program to show all numbers up to and including 1000
  2. A program to show all multiples of two up to and including 2000
  3. A program to ask the user for number and then loop until it reaches that number

On your computers

Conditional loops

  • Conditional loops work similarly to a selection statement mixed with a fixed loop.
  • They are used to repeat whilst a condition is True (or in some cases, False).
  • In Python this is known as a while loop:

Image copyright jamiebalfour.scot

Over the next few periods, we will work with the Python booklet, particularly pages 19 to 32.

 

This is your task, working with Python work your way through the tasks, creating a folder for each topic (e.g. Conditional selection) naming each task within the topic with task number. 

Task

Presentation Overview
Close
JB
Constructs
© 2020 - 2024 J Balfour
12:55 | 29-04-2024
Join Live Session
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Widget Screen
Canvas Controls
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      ×
      All slideshow files