Slides badge

Starting Software development

Computer hardware revisited

Complete the following diagram for the letters IPO.

Input

Process

Output

Learning Intentions

  • Understand why we learn to program

  • Understand how to develop and run a small program in Python

  • Understand and implement basic input and output methods in Python

Success Criteria

  • I am able to open Python IDLE and open and save code in it

  • I am able to read and implement some Python code

Computer hardware revisited

  1. Think of three computers in your house that are not a smartphone, tablet, laptop or desktop computer.
  2. Think about the purpose of this computer.
  3. For example, a fridge might have a small computer that checks the temperature.

Why are we learning to code?

  • Learning to program/code computers is a great skill to have.
  • You can make money from learning to code in any job you go into, for instance if you work in finance you can write small programs called macros (which are code) to automate your work on occasion.
  • It's also a fundamental part of the National 4 and National 5 courses.
  • Learning to program computers will also improve your problem solving skills

Why learn to code?

What is a computer program?

A computer program is a collection of instructions that performs a specific task when executed by a computer. A computer requires programs to function. A computer program is usually written by a computer programmer in a programming language.

What is a computer program?

  • A computer program is a series of instructions that is designed to be carried out in a very specific order that tells the computer what to do.
  • A computer program is made up of statements that fit together with instructions. These are made up of constructs and expressions.

What is a computer program?

  • Learning to program is now becoming more and more important in every field and can even be useful in admin jobs where writing a small program can automate a job.
  • Learning to program can also help you automate your life, say for instance you want to resize every picture on your computer, so write a small program to do this.
  • Learning to program can help you find a job in the computing industry where you can get paid very well
  • Learning to program can help you create something amazing!

What is code?

$files = directory_list_files("/folder/images/")
for each ($files as $file)
 $img = new Image
 $img->open($file)
 $img->transform_resize(600, 400)
 $img->save($file)
end for

What is code?

Instruction

$files = directory_list_files("/folder/images/")
for each ($files as $file)
 $img = new Image
 $img->open($file)
 $img->transform_resize(600, 400)
 $img->save($file)
end for

What is code?

Statement

$files = directory_list_files("/folder/images/")
for each ($files as $file)
 $img = new Image
 $img->open($file)
 $img->transform_resize(600, 400)
 $img->save($file)
end for

Introducing Python

Writing our first program

print("Hello World")

That’s all there is to this program!

😮

print is a function in Python. print is used to send something to the display.

Let's get organised

  • Before you start this next topic you'll need to get yourself organised.
  • Create the following folder structure in your space on the server (Z:\)
    • Computing
      • Software Development
        • IPO
        • Variables
  • Each lesson you will need to create a folder based on the topic you are learning and ensure that your Python files are put there.
  • Naming your Python files well will also be very helpful for later.

Python IDLE

  • When you've written your code in this new window, click on Run at the top and select Run Module. Alternatively, press F5.
  • Once IDLE opens, click on File and then click New.
  • Write your own ‘hello world’ program
  • Run your program
  • Change your program so that it says hello to the person who is sitting next to you.

Task

Display a person's name

  • Our next program will take in a persons name and display it. For this example we will use the input() function.

print("Please insert your name")
print("Hello there")
print(input())

Display a person's name - nicer

  • We can also combine pieces of text when we display them using commas in the print function which makes the output much nicer:

print("Please insert your name")
print("Hello there", input())

This is called concatenation.

Other print statements

  • Python has a bunch of interesting things you can do in the print function:

    For example, this will stop Python taking a new line after outputting to the screen:
print('Hello world!', end=' ')

The \n character is the standardised method of adding a new line within a piece of text.

print('Hello\nworld")

Delays

  • You may have come across the wait block in Scratch

  • Most programming languages offer identical functionality. 

  • In Python we use the time.sleep function after importing the time library (more later):

import time
time.sleep(1)
print('Hello everyone!')

Debugging code

  • Being able to fix problems in code is just as important as writing it.
  • The process of looking for problems in code is called debugging.
  • The term bug came about after Grace Hopper discovered a moth inside one of the computers running a program she had developed.

Debugging code

  • Finding bugs today is much easier than it was back then! 
  • There are many tools available to computer programmers to help them identify and fix bugs in code.

Ways to debug your own code

  • You can take a proactive approach to debugging your code and trying to fix things yourself by:
    • Understanding different types of errors (we are going to do a lesson on types of errors later)
    • Stepping through the program yourself before running it (a dryrun) and seeing what result you get
    • Testing different parameters (or inputs until one works)
    • Use debugging tools

Debug this

What is z?

 

a = 15

b = 3

c = 4

 

z = a + b × c ÷ 2

 

What was your answer?

Steps:

c [4] ÷ 2 = 2

b [3] × 2 = 6

12 + a [15] = 21

Debugging code

  • For each of the following code samples, explain what is wrong.
print("Please insert your name)
print["Please insert your name"]

Debugging code

  • For each of the following code samples, explain what is wrong.
print("Please insert your name)
print["Please insert your name"]
print("Please insert your name')

Debugging code

  • For each of the following code samples, explain what is wrong.
print["Please insert your name"]
print("Please insert your name')
print(input)

Debugging code

  • For each of the following code samples, explain what is wrong.
print(input)
print("Please insert your name')

Describing your code

  • Any good programmer should be able to describe what a program does.
  • However, as a program becomes more and more complex, even the most advanced programmers will struggle to describe everything. 
  • To help with this, we can add comments into the code. These are not run as code but help describe what the code is doing.
#Ask the user for their name and display it
print("What is your name?")
print(input())

Describing your code

  • You are also expected to put a bit of information about each program at the top, describing who made it, when they made it and what it does.
# Program to calculate product of two numbers
# J Balfour
# March 2021

Your logbooks

  • Throughout this topic in S3 and into National 5 you should use the logbook to keep a record of things you learn in computer programming. 
  • The left column is a keyword which you should describe and provide an example.
  • This is actually how some of the world's greatest programmers learned, they read the books on programming languages and took notes.
  • Open your Python Logbook and provide descriptions and code of the first four items.
  • Attempt the Python tasks in Worksheet 1. Save each task as a different file in your Python folder!
  • If you finish the above, look at the first example on the Python part of the Code Club website at codeclubprojects.org. You can use this to expand your knowledge above what we learn in school later on!

Task

Grade yourself

I can open Python IDLE and can read some code

I can open Python IDLE and can write code to display something on the screen but need to refer back to examples to do this

I can open Python IDLE and write code to display something on the screen without any help

Presentation Overview
Close
JB
Starting Software Development
© 2020 - 2024 J Balfour
15:53 | 23-05-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