Lecture 2 - Binary Numbers, Python Basics PDF
Lecture 2 - Binary Numbers, Python Basics PDF
COMP 208!
Lecture 2: Binary numbers, Python basics
• Binary numbers
• Python basics
• Variables
2
Question poll test
• Session ID:
• http://mcgill.ca/polling
A. The CPU
C. The printer
3
Binary numbers
Decimal number system
5
Decimal number system
Each digit in the sequence is a different power of 10.
E.g., for the number 2019:
Digit 2 0 1 9
6
Binary number system
• So it's base 2.
183 1 0 1 1 0 1 1 1
7
Binary number system
Each digit in the sequence is a different power of 2.
E.g., for the binary number 11010:
Powers of 2
24 23 22 21 20
(Place Value) (16) (8) (4) (2) (1)
Digit 1 1 0 1 0
110102
8
Converting from binary to decimal
Powers of 2
24 23 22 21 20
(Place Value) (16) (8) (4) (2) (1)
Digit 1 1 0 1 0
= 1 × 24 + 1 × 23 + 0 × 22 + 1 × 21 + 0 × 20
110102 = 2610
9
Converting from decimal to binary
10
Decimal to binary - example (1)
1. Repeatedly divide
the number by 2
until result is 0.
11
Decimal to binary - example (2)
109 =
11011012
12
Binary in computers
Bits
• A bit is a binary digit (i.e., either 0 or 1).
14
Bytes
15
Bits in Python
16
Integers in binary
• We already saw how to represent positive integers
109 =
11011012
• For negative numbers, we use an extra bit for the sign:
-109 = 109 =
111011012 011011012
17
Finite precision
3.14159265358979323846264338327950
28841…
3.141592653589793
18
Real numbers in binary
0100000000001001
0010000111111011
0101010001000100 3.141592653589793
0010110100011000
Floating-point
format
(64 bits)
19
Text in binary
01101000 h
01100101 e
01101100 l
Character encoding
01101100 (e.g. ASCII or Unicode) l
01101111 o
21
For further info
22
Python
Our first Python program
print("Hello World")
1. Launch Thonny.
25
Our first Python program
print("Hello World")
print("This is our first Python program!")
26
Errors
We have to make sure to respect the syntax of Python.
That means, for example, closing parentheses '(' that we have
opened.
print("Hello World")
print("This is our first Python program!")
print("I think I'm forgetting something...."
SyntaxError
27
Interactive mode
28
Values & Types
Values
30
Types
31
print()
>>> print(3000)
3000
>>> print(3.1415)
3.1415
32
Printing multiple values
33
type()
>>> print(type(3000))
<class 'int'>
>>> print(type(3.1415))
<class 'float'>
34
Comments
35
Comments
36
The int type
>>> print(12345)
12345
>>> print(12345a)
SyntaxError: invalid syntax
37
The float type
>>> print(3.14)
3.14
>>> print(type(123.))
<class 'float'>
38
Basic operations on numbers
Addition 7 + 12 19
Division 33 / 8 3.3
39
Basic operations on numbers (2)
Modulus 27 % 10 7
Exponentiation 2 ** 3 8
40
Division vs. floor division
• The division operator ("/") gives us
a floating point number as result for any division,
even if both numbers are integers.
42
Arithmetic operators and result type (1)
Addition (+)
3 // 2 → 1
Subtraction (-)
Multiplication (*)
3 - 1.5 → 1.5
Exponentiation (**)
2.5 * 3.5 → 8.75
3.2 // 2 → 1.0
43
Arithmetic operators and result type (2)
3 / 5 → 0.6
In summary,
PEDMAS:
1. Parentheses
2. Exponentiation
45
Order of operations: examples
46
Order of operations: examples (2)
47
Order of operations: examples (3)
>>> print(12 + 23 - 10) # left to right
25
>>> print(10 / 5 * 2) # left to right
4.0
>>> print(10 / (5 * 2)) # using () to change priority
1.0
48
Order of operations: examples (4)
49
The string type
50
Strings (2)
51
Strings (3)
52
Basic operations on strings
>>> print(len('Hello'))
5
53
Basic operations on strings (2)
54
Basic operations on strings (3)
55
Variables
Variables
>>> print("Sum is:", 10 + 5)
Sum is: 15
# to print the sum again,
# we have to redo the addition
57
Making a variable
>>> result = 10 + 5
58
Variables and assignment
59
Variables and assignment (2)
60
Resources
• Binary/decimal conversion
https://www.purplemath.com/modules/numbbase.htm
61
Reminders
62