CMPT120 J.
Ye
Introduction to
Computing Science and Programming
• What this course is about
• How Computers represent information
• Algorithms and Programming
• Python IDLE
Different levels of viewing a computer
As a complex system, computers can be viewed from a
number of perspectives, or levels:
High
User Level: Application Programs
High Level Languages
Assembly Language / Machine Code
Functional Units: Memory, ALU, ...
Logic gates
Low Transistors and Wires
2
What this course is NOT about?
• This course is not about how to use computers or any
application software
• This course is not (only) about Python
(we just use Python to introduce programming concepts)
3
What this course IS about?
• Basic ideas of computing science
– algorithms; programs; data representation; running time; ...
• Basic ideas of computer programming
– Basic concepts: variables; references; functions; data types;
variable scope; …
– basic data structure: lists (and strings)
– basic control structures: conditional statements; loops;
–…
• Create algorithms to solve simple problems
• Implement computer programs in Python
• Design programs that are easy to understand and maintain
4
How Computers represent information?
Bits and Bit Patterns
• Bit: binary digit (0 or 1)
• Byte: 1 byte = 8 bits
• Bit Patterns (e.g. 1000001) are used to represent
information.
– numbers (we only discuss unsigned and signed integers)
– Text characters
– Images
– Sound
– Instructions
– … 5
Representing Unsigned Integers
• Binary notation: Uses bit patterns to represent
a number in binary system
• Binary system:
The traditional decimal system is based on powers of
ten.
The Binary system is based on powers of two.
6
The base ten and base two systems
7
Decoding the binary representation 100101
What is the decimal equivalent of the binary number 100101?
8
MSB and LSB of a binary number
Left-most Right-most
bit bit
9
An algorithm for converting a (decimal)
unsigned integer to its binary representation
10
Applying the algorithm to obtain the
binary representation of 13
13 / 2 = 6 r 1 LSB
6 / 2 = 3 r 0
3 / 2 = 1 r 1
1 / 2 = r 1 MSB
(13)10 = (1101)2
11
The binary addition facts
12
Adding two binary (unsigned) integers
• What is the sum of the following binary integers: 1101 and 1011,
suppose more than 4 bits are available if needed?
1101
+ 1011
11000
• If no more than 4 bits is available, then the above addition (in
computers) causes an overflow
• An overflow occurs in computers when there are not enough
bits to hold an arithmetic result
• For addition of two unsigned integers, an overflow occurs when
there is a carry out of the most significant bit.
• How to add up these two unsigned integers: 101 + 111101?
13
Representing Signed Integers
• Two’s complement notation - the most popular
means of representing signed integer values in
computers
– The leftmost bit is the sign bit: 0 for positive, 1
for negative
– A negative integer is the complement of the
corresponding positive integer
14
How to “complement” an integer in 2’s
complement system ?
• From positive to negative:
start with the positive version: 0101 (+5)
flip all the bits: 1010
add one: 1011 (−5)
• From negative to positive:
start with the negative version: 1011 (−5)
flip all the bits: 0100
add one: 0101 (+5)
For positive integers, the unsigned and 2’s complement
representations are identical (provided that the unsigned representation
starts with a zero)
15
Representing integer −10 in 2’s
complement system using 6 bits
1. Obtain the 2’s complement representation (also the
unsigned binary representation) of +10 in 6 bits
(using the algorithm on slide 10):
001010
The leading 0’s do not
contribute values
2. Complement it:
-- flip all the bits: 110101
-- add one: 110110
(-10)10 = (110110)2 16
Two’s complement notation(in 3 and 4 bits)
17
Addition problems converted to two’s
complement notation
Note: If there is a carry out of the sign bit, just discard it 18
Hexadecimal Notation
• We can also use hexadecimal notation to represent bit
patterns with much shorter length.
E.g.
10001111 binary becomes 8F hex
• Hexadecimal notation is also called base-16 notation. It
has 16 basic symbols(digits) to represent numbers:
{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
• Every hexadecimal digit corresponds to four binary bits
as shown in the following table (on the next slide):
19
Hexadecimal Notation (cont)
Hex Corresponding Hex Corresponding
digit binary pattern digit binary pattern
0 0000 8 1000
1 0001 9 1001
2 0010 A 1010
3 0011 B 1011
4 0100 C 1100
5 0101 D 1101
6 0110 E 1110
7 0111 F 1111
20
Hexadecimal Notation (cont)
• Using the table, work out the following equivalents:
10100111 binary = ( ? ) hex
100001 binary = ( ? ) hex
( ? ) binary = E33 hex
( ? ) binary = 5678 hex
21
Representing Text
• Each character (letter, punctuation, etc.) is assigned a
unique bit pattern.
– ASCII: Uses patterns of 8-bits (actually 7-bits) to
represent most symbols in written English text
– Unicode: Uses patterns of 16-bits to represent the
major symbols used in languages world wide
– ISO standard: Uses patterns of 32-bits to represent
most symbols used in languages world wide
22
The message “Hello.” in ASCII
23
ASCII Table
24
In summary:
• All information that is stored and manipulated with a
computer is represented with 0’s and 1’s
• Sequences of 0’s and 1’s can represent integers (unsigned
or signed), float numbers, characters/strings, instructions
… But their representation schemes are different. E.g.
1001110 may represent:
character ‘N’, or
unsigned integer 78, or
signed integer −50, or
… 25
In summary (cont)
• 25 and 25.0 are represented and stored in computer
completely differently (although we didn’t talk about how
to represent floating point numbers). They are different
types of values
• It is the programming language’s responsibility to keep
track of what type of information a sequence of 0’s and 1’s
stand for.
• Given limited number of bits, only limited number of
“things” can be represented. E.g., using one byte, we can
represent 28 different “things” (numbers, characters, … )
26
What is an algorithm?
• A simple version: An algorithm is a set of
instructions for solving a problem
• A formal version: “An algorithm is a sequence of
unambiguous instructions for solving a problem,
i.e., for obtaining a required output for any valid
input in a finite amount of time.”
27
Algorithms
Algorithms can be expressed using:
• Plain English – usually in a step-by-step fashion
• Pseudecode – almost code
• Flowchart – a type of diagram that can be used to
show how “things” work (will be discussed later in
the course)
28
Algorithms
Algorithm Example #1
An algorithm that (a computer) “guesses” a secret integer between 1 and 100
1. Tell the user to pick a secret integer between 1 and 100.
2. The smallest possible integer is 1; the largest possible is 100
3. The computer makes a guess that is halfway between the smallest and largest
4. Ask the user if the guess is too large, too small or correct.
5. If the user says it is correct, the game is over.
6. If the user says the guess is too small, the smallest possible integer is now the
guess plus one.
7. If the user says the guess is too large, the largest possible integer is now the
guess minus one.
8. Unless the guess is correct, go back to step 3.
29
Algorithms
Algorithm Example #2
-- The Euclidean algorithm
30
What is Computer Science?
• “Computer Science is no more about computers
than astronomy is about telescopes, biology is
about microscopes or chemistry is about test
tubes. Science is not about tools, it is about how
we use them and what we find out when we use
them.”
-- Anany Levitin, Computing Research News
• Computer science is often defined as “the science
of algorithms”
31
The central role of algorithms in computing science
32
What is Programming?
• Programming: the process of developing a program
• Program: a representation of an algorithm that can be
executed on a computer. Every program is written in a
particular programming language.
• A Programming language essentially represents a set of
reserved words, syntax rules and tools (libraries) that are
used to define your program. In this course, the language
we use is Python
• Each programming language has its strengths and
weaknesses, but they share many of the same concepts
33
Program (Software) Development Process
• Analyzing the problem: Identify what should be done
• Devising an algorithm for solving the problem
• Implementing the algorithm (Programming, coding)
• Testing the code
• Maintaining the software
34
Python IDLE Basics
• Start Python IDLE Shell in lab: (search IDLE in Window Start)
( We use Python 3. Any version of Python 3 will do. It can be
downloaded from https://www.python.org/downloads )
• After launching Python IDLE, you will see:
A window called “IDLE Shell” that contains something like:
Python 3.11.3 (… … ...) The version number might be different
Type “help", "copyright", "credits" or … for more information…
>>>
• This is the Python interactive interpreter window
35
Python IDLE Basics (cont)
• >>> is the Prompt
• Whatever you type after the prompt should be an
instruction (or expression) that Python can understand,
then press Enter, Python interpreter will immediately
execute your instruction
• For example:
>>> print(“What is your name?”)
36
Python IDLE Basics (cont)
• “What is your name?” is a string
• So we are printing some string to the screen. print is the
command (or, a built-in function in Python) that can put
text on the screen
• There are many other general commands/operators that
can perform calculation, dealing with input/output,
sorting, …
37
Python Editor and .py files
• As we know, the window with the >>> prompt is the
interpreter window (Python Shell)
• Writing instructions directly in the shell is best for testing
small sections of code, not for writing entire program
• We usually write a program in a .py file using Python
editor
38
Python Editor and .py files (cont)
• To write a complete program, create a text file from the
shell (File New File), now you can write your Python
code in this editor window
• Editor window doesn’t have a prompt or anything else.
After you finished your program code, you need to save it
as a .py file
• To run your program, simply press F5 when the editor
window (containing your code) is active
39