1 cs 177 week 2 recitation slides introduction. 2 announcements
TRANSCRIPT
![Page 1: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/1.jpg)
1
CS 177 Week 2 Recitation Slides
Introduction
![Page 2: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/2.jpg)
2
Announcements
![Page 3: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/3.jpg)
What Computers Understand?
Computers can only understand Encoding of numbers.
So what is the meaning of Encoding?
![Page 4: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/4.jpg)
Computers are electronic devices that react to wires. Computer hardware produces (patterns of) voltages
(variations of electric current). If a wire has a voltage on it, we say that it encodes 1. If it has no voltage, we say 0.
How can we represent voltages?
![Page 5: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/5.jpg)
Encoding is the activity of converting data or information into some symbol.
Computer understand just two symbols, 0 and 1.
0 and 1 are also numbers (in the binary number system). A binary number system gets its name from having just two digits, O and 1.
Encoding
![Page 6: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/6.jpg)
Binary Number System
Represents numeric values using two symbols, 0 and 1 Also called Base-2 number system
To convert decimal numbers into binary, divide the decimal number repeatedly by 2 Remainder at each step becomes a digit of the binary number Repeat until result of further division becomes zero
6
![Page 7: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/7.jpg)
Decimal to Binary Conversion
To convert the decimal number 26 into binary,
In a computer, 26 is represented as 11010
7
![Page 8: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/8.jpg)
Binary to Decimal Conversion
Multiply successive digits of the binary number with increasing powers of 2
Add the products to get the
decimal number
8
![Page 9: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/9.jpg)
23 = 8 * 1 = 8 +22 = 4 * 1 = 4 +21 = 2 * 0 = 0 +20 = 1 * 1 = 1Interpreted as 13
Examples
![Page 10: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/10.jpg)
Can you do this?
![Page 11: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/11.jpg)
Example 1: Emoticons
What if you want to tell a friend your emotion by texting them on your mobile phone (without texting a long message)?
You can use emoticons… Emoticons are an encoding of emotions: :) I’m happy, I like it :( I’m unhappy, I dislike it :’( I’m crying and sad
You are using a combination of symbols (parenthesis, punctuation symbols), that you normally you use in writing, to represent your emotions.
![Page 12: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/12.jpg)
Example 2: Character Codes
Now suppose that you want to encode the previous 3 symbols : ( ) using numbers…
You may use the ASCII code …
![Page 13: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/13.jpg)
ASCII Table
![Page 14: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/14.jpg)
Definitions
Bit: is the smallest unit of data in a computer that has a single binary value either 0 or 1.
Byte: is a unit of data that is eight binary digits long. How many bits are there in 5 bytes?
![Page 15: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/15.jpg)
Questions
How many combinations can be represented in one bit? Two bits?
What about three bits and four bits?
![Page 16: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/16.jpg)
So, Decoding is the activity of converting code into plain text.
Decoding
![Page 17: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/17.jpg)
Statements
On starting Python, there is a Python prompt indicating that Python is ready to accept a command. These commands are called statements.
Different kinds of statements
>>> print("Hello, world“) Hello, world>>> print(2+3)5>>> print("2+3=", 2+3)2+3= 5>>>
17
![Page 18: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/18.jpg)
Simple Statements Assigning values
>>> a = 10
>>> b = 2
>>> c = 4
Arithmetic Operations
>>> x = a + b
>>> y = a – b
>>> p = a/b
>>> q = a/c
18
![Page 19: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/19.jpg)
Operator Precedence
Order of mathematical operations in an expression Python uses the same precedence as in Mathematics
Terms inside parentheses or brackets Exponents and Roots Multiplication and Division (Left to Right) Addition and Subtraction (Left to Right)
19
![Page 20: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/20.jpg)
Print Statements From previous example,
>>> print(x)
12
>>> print("y =", y)
y = 8
>>> print("Sum of a and b :", a + b)
Sum of a and b : 12 Other examples,
>>> print("Hello, World!")
Hello, World!
>>> print("Hello", ",", "World!")
Hello , World! We will revisit the print() statement later
20
![Page 21: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/21.jpg)
Programming Tip
Use print() statements for debugging
>>> a = 2 + 3 * 6
>>> b = 10 - 2 * 3
>>> print(a/b)
5.0
Did we get the expected result?
21
![Page 22: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/22.jpg)
Print statements for debugging
Print statements allow us to observe the operator precedence
>>> a = 2 + 3 * 6
>>> print("a = ", a)
a = 20
>>> b = 10 - 2 * 3
>>> print("b = ", b)
b = 4
>>> print("Result = ", a/b)
Result = 5.0
Print statements allow us to observe various variables and the values they have during the course of the program
22
![Page 23: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/23.jpg)
Python Math Library
Provides access to mathematical functions Documentation – http://docs.python.org/library/math.html
math.pow(x, y) - Return x raised to the power y. math.sqrt(x) - Return the square root of x. math.factorial(x) - Return x factorial. math.ceil(x) - Return the ceiling of x. math.floor(x) - Return the floor of x.
23
![Page 24: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/24.jpg)
>>> import math
>>> a = math.factorial(6)
>>> print(a)
720
>>> b = math.sqrt(123)
>>> print(b)
11.0905365064
>>> c = math.floor(5.9)
>>> print(c)
5
24
Examples
![Page 25: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697bf7c1a28abf838c83ef9/html5/thumbnails/25.jpg)
>>> x = math.factorial(4) * math.pow(2,3)
>>> print(x)
192.0
>>> y = 5.5
>>> z = math.floor(y) * math.ceil(y)
>>> print(z)
30
25
Examples