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

02 - Intro to Computers and Programming

The document serves as an introduction to computers and programming, specifically focusing on the course COMP 125 - Programming with Python. It covers fundamental concepts such as computer components, data storage, programming languages, and the program development cycle. Additionally, it emphasizes the importance of software, the distinction between high-level and low-level languages, and provides guidelines for using Python as a programming environment.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

02 - Intro to Computers and Programming

The document serves as an introduction to computers and programming, specifically focusing on the course COMP 125 - Programming with Python. It covers fundamental concepts such as computer components, data storage, programming languages, and the program development cycle. Additionally, it emphasizes the importance of software, the distinction between high-level and low-level languages, and provides guidelines for using Python as a programming environment.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

02 – Introduction to Computers and

Programming

COMP 125 – Programming with Python


Recording Disclaimer

The synchronous sessions are recorded (audiovisual recordings). The students are not required to keep their
cameras on during class.

The audiovisual recordings, presentations, readings and any other works offered as the course materials aim
to support remote and online learning. They are only for the personal use of the students. Further use of
course materials other than the personal and educational purposes as defined in this disclaimer, such as
making copies, reproductions, replications, submission and sharing on different platforms including the digital
ones or commercial usages are strictly prohibited and illegal.

The persons violating the above-mentioned prohibitions can be subject to the administrative, civil, and
criminal sanctions under the Law on Higher Education Nr. 2547, the By-Law on Disciplinary Matters of Higher
Education Students, the Law on Intellectual Property Nr. 5846, the Criminal Law Nr. 5237, the Law on
Obligations Nr. 6098, and any other relevant legislation.

The academic expressions, views, and discussions in the course materials including the audio-visual
recordings fall within the scope of the freedom of science and art.

COMP 125 Programming with Python 2


What is a Computer?

▪ “A machine that can be instructed to carry out generalized sets of


operations (called programs) via computer programming”
▪ Hardware components of a computer (physical devices) carry out the
programmed instructions
▪ There are a lot of components and parts, but the major ones are:
▪ Processor a.k.a. Central Processing Unit (CPU)
▪ Main Memory a.k.a. Primary Storage (RAM)
▪ Secondary Storage (Hard Disk (HDD), SSD, CD, Flash Memory, etc.)
▪ Input and Output Devices (keyboard/mouse, monitor, etc.)

COMP 125 Programming with Python 3


What is a Computer?

COMP 125 Programming with Python 4


Processor / Central Processing Unit (CPU)

▪ The part of the computer that runs the programs


▪ “Process”or – it is the part that does the processing of instructions
▪ Without it, we cannot run our programs
▪ CPUs used to be huge
▪ Nowadays, we can comfortably fit them in our desktop computers and laptops

ENIAC computer, 1945

COMP 125 Programming with Python 5


Main Memory (a.k.a. Primary Storage)

▪ This is where the computer stores a program and the data it is using, while
that program is running
▪ Commonly known as Random Access Memory (RAM)
▪ CPU can quickly access the RAM
▪ It is volatile, used for temporary storage while program is running. Contents
are erased when computer is off.
▪ e.g., you lose electricity
▪ That’s why we save to “disk” = secondary storage!
▪ RAM is faster to access than secondary storage,
but it often has smaller size

COMP 125 Programming with Python 6


Secondary Storage

▪ Holds large amounts of data and programs for long periods of time
▪ Programs/data stored here are loaded to main memory when needed
▪ Types of secondary storage:
▪ Hard Disk Drive: Magnetically encodes data onto a spinning circular disk
▪ Solid State Drive: Faster than disk drive, no moving parts, stores data in solid
state memory
▪ Optical devices: data encoded optically
▪ Flash memory: portable physical disk (e.g., USB disks, USB sticks)

COMP 125 Programming with Python 7


Input Devices

▪ Input: data the computer collects from users and other devices
▪ Input device: device that collects the input data
▪ Examples: keyboard, mouse, touchscreen, scanner, camera, etc.

COMP 125 Programming with Python 8


Output Devices

▪ Output: data produced by the computer for users or other devices


▪ Can be numbers, text, image, audio, video, etc.
▪ Output device: device that formats and presents output
▪ Examples: monitor, printer

COMP 125 Programming with Python 9


Software

▪ Everything the computer does is controlled by software


▪ Broadly two categories:
▪ System software: programs that control and manage basic operations of a
computer
▪ Operating System (OS): controls operations of hardware components
▪ Windows, macOS, IOS, Android, Linux etc.
▪ Utility program: performs specific task to enhance computer operation or safeguard data
▪ Examples: anti-virus programs, network utilities (for Wi-Fi and ethernet connectivity), disk
compression, cleaning or backup software
▪ Application software: programs that make your computer useful for you in
everyday tasks
▪ Examples: web browsers, word processing, email, games, social media, etc.

COMP 125 Programming with Python 10


How Computers Store Data
How are Data and Instructions Stored?
▪ Computers use a binary system
▪ Everything is stored in 0s and 1s. Each 0 or 1 is called a bit
▪ A byte is made of 8 bits. 1 byte can store 28=256 different “things”
▪ How to store larger things?
▪ 1 kilobyte (KB) = 210 bytes = 1,024 bytes
▪ 1 megabyte (MB) = 1,024 KB
= 220 bytes = 1,048,576 bytes
▪ 1 gigabyte (GB) = 1,024 MB
= 230 bytes = 1,073,741,824 bytes
▪ terabyte (TB), petabyte (PB), exabyte (EB) …

▪ Note: In different contexts and based on a different standard 1 kilobyte (KB) can
refer to 103 bytes, 1 megabyte (MB) to 106 bytes and so on…

COMP 125 Programming with Python 12


How are Data and Instructions Stored?

▪ Storing a number (e.g., integer between 0 and 255)


▪ We use its binary representation

1 0 0 1 1 1 0 1 = 128 + 0 + 0 + 16 + 8 + 4 + 0 + 1 = 157
20 (1)
21 (2)
22 (4)
23 (8)
24 (16)
25 (32)
26 (64)
27 (128)
Place values

▪ Storing a character (e.g., one letter)


▪ We use one or more bytes by mapping each character to a number
▪ This is called an encoding (ASCII value)
COMP 125 Programming with Python 13
ASCII Encoding

COMP 125 Programming with Python 14


How are Data and Instructions Stored?

▪ Other, somewhat simplified, examples:


▪ A “string” (text) is made up of multiple characters
▪ An image is made up of multiple numbers (usually these numbers are bytes)
▪ A video is made up of multiple images + audio (again multiple numbers)
▪ CPU Instructions
▪ All CPU instructions have numerical values (opcode). One or more bytes…

COMP 125 Programming with Python 15


How Programs Work
How Does a Program Work?

▪ Let’s say that you or someone else wrote some program...


▪ The program consists of several “instructions”, i.e., things to do
▪ The CPU carries out the instructions defined by the program
▪ These instructions are made of primitive CPU operations, called the
machine language or instruction set
▪ This is extremely low level.
▪ You can code using the machine
Language, but …
▪ High level programming languages
let you abstract away the details

COMP 125 Programming with Python 17


Programming Languages

▪ Programming languages
▪ are formal, human-made languages that have been designed to express
computations
▪ have strict syntax rules that govern the structure of statements
▪ are literal – mean exactly what is written, no metaphors

▪ Structure is important
▪ learn to parse the program and identify the code structure

▪ Every detail matters


▪ even the smallest errors are not acceptable

COMP 125 Programming with Python 18


Low-Level Languages
machine assembly
language language
▪ Assembly language: Uses short words
(mnemonics) for instructions instead of a
numerical machine language
representation
▪ Still low level but sometimes needed,
easier to work with than machine
language!
▪ Assembler: Translates assembly
language to machine language for
execution by CPU
▪ Both machine language and assembly
language are low-level languages

COMP 125 Programming with Python 19


High-Level Languages

▪ High-level languages have strong abstraction from


the details of the underlying computer
▪ Much more human-readable and programmer-
friendly
▪ High-level languages
▪ Allow “simple” creation of powerful and complex
programs
▪ More intuitive to understand than low-level
languages
▪ Allow developing programs with a low number of
instructions
▪ Are supported by rich sets of tools

▪ Python is a high-level language


▪ Many other popularly used programming languages
nowadays are also high-level
COMP 125 Programming with Python 20
Compilers

▪ Programs written in high-level languages must be translated into machine


language to be executed
▪ Compiler translates high-level language program into a machine language
program
▪ Machine language program can be executed at any time

55 31 D2 89 E5 8B 45 08
56 8B 75 0C 53 8D 58 FF
Compiler 0F B6 0C 16 88 4C 13 01
83 C2 01 84 C9 75 F1 5B
5E 5D C3

COMP 125 Programming with Python 21


Interpreters

▪ Interpreter translates and executes instructions in a high-level language program


▪ Interprets one instruction at a time (instead of compiling the entire set of instructions)
▪ Python is an interpreted language (no need for a compiler)

COMP 125 Programming with Python 22


Terminology
▪ Source code: set of instructions (program code) written by the programmer
▪ Syntax: set of rules to be followed when writing a program
▪ If we say (or your computer says) you made a syntax error, that means you must have
somehow failed to follow the rules when writing your program
▪ You need to go and find your mistake
▪ Semantics: meaning of the program / instructions
▪ If we say you made a semantic (logic) error, that means the meaning/rationale is wrong
▪ e.g., you were supposed to add 3 and 5 to find 8, but you subtracted them
▪ Nothing wrong with syntax (subtraction is a valid operation), but code outcome is wrong –>
doesn’t work as it should
▪ You still need to go and find your mistake ☺
▪ Usually harder to find a semantic error than finding a syntax error

COMP 125 Programming with Python 23


Program Development
Designing a Program

▪ Programs must be designed before they are written


▪ Design is the most important part of the program development cycle
▪ Understand the task that the program is to perform
▪ Ask questions about program details and how it should behave
▪ Then determine the steps that must be taken by the program
▪ Break down the complex task into smaller parts / instructions
▪ Remember the omelette?
▪ Create an algorithm: A step-by-step procedure for performing some task

COMP 125 Programming with Python 25


Pseudocode and Flowchart
Example: Calculating Pay
▪ Pseudocode: plain language
Flowchart:
description of the steps in an Pseudocode:

algorithm 1. Input the hours worked


2. Input the hourly pay rate
▪ Flowchart: a diagram that represents
3. Calculate gross pay as hours
a set of instructions worked multiplied by pay rate

▪ Oval: terminal symbols 4. Display the gross pay

▪ Parallelogram: input and output


▪ Rectangle: processing or operation
▪ Diamond: decision

COMP 125 Programming with Python 26


Example: Greatest of Two Numbers
▪ Given two numbers a and b, which one is the maximum?
▪ You can assume a≠b for this exercise

Pseudocode:
Step 1. Input (get) a and b
Step 2. Check if a > b
If so, go to Step 3
Otherwise, go to Step 4
Step 3. Print “a is greatest”
Step 4. Print “b is greatest”

COMP 125 Programming with Python 27


Another Example: Greatest of Three Numbers
▪ Given three numbers a, b and c, which one is the maximum?
▪ You can assume a≠b≠c for this exercise

Pseudocode:
Good exercise for you! ☺

COMP 125 Programming with Python 28


Another Example: Numbers up to N

▪ Given N, print all numbers starting from 1 up to N.


▪ Start from 1, increment one by one, go until you hit N
▪ We need to keep track of which number we are currently at

Pseudocode:
Step 1. Input (get) N
Step 2. Initialize i = 1
Step 3. Check if i <= N
If so, go to Step 4
If not, stop
Step 4. Print i
Step 5. Increment i by 1
Step 6. Go to Step 3

COMP 125 Programming with Python 29


Program Development Cycle

Implement
Design the Correct syntax Test the Correct logic
the program
program errors program errors
(write the code)

The most important Write the necessary Check errors Consider different While testing your
part of the program codes using the produced by the use cases and be program, correct logic
development cycle selected programming compiler/interpreter sure that your errors that cause your
language program works program give
Independent from a These errors are for all these cases unexpected/incorrect
programming easy to find results
language

COMP 125 Programming with Python 30


Using Python
Programming Environment
▪ Your first task:
▪ Download and install Spyder
▪ https://www.spyder-ide.org/
▪ The latest Spyder comes with
Python 3.12.
▪ If you have an existing
Python environment in your
computer make sure you
have Python version 3.12 or
above.

COMP 125 Programming with Python 32


Programming Environment

Editor Console
(open, edit, run files) (Interactive Use)

COMP 125 Programming with Python 34


Our First Python Program

▪ We use the print function to display output in a Python program

Our first program :


We called the print function

Argument (data that you


want to display on the screen)
Run the program :

Output of the program :

COMP 125 Programming with Python 35


Strings and String Literals

▪ Programs almost always work with data of some type


▪ integers, floating point numbers, strings, booleans, etc.

We used ‘Hello world!’ as our data.

This data consists of a sequence of characters string


▪ String literals must be enclosed in quote marks to mark where the string data
begins and ends
▪ You can either use single-quote marks (') or a set of double-quote marks (")

COMP 125 Programming with Python 36


Interactive Mode
▪ The Python interpreter can run Python programs that are saved in files, or
interactively execute Python statements that are typed at the keyboard.
▪ Demo…

COMP 125 Programming with Python 37

You might also like