+ All Categories
Home > Documents > 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.

1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.

Date post: 17-Jan-2016
Category:
Upload: johnathan-lawrence
View: 217 times
Download: 0 times
Share this document with a friend
25
1 CS 177 Week 2 Recitation Slides Introduction
Transcript
Page 1: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.

1

CS 177 Week 2 Recitation Slides

Introduction

Page 2: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.

2

Announcements

Page 3: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.

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.

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.

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.

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.

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.

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.

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.

Can you do this?

Page 11: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.

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.

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.

ASCII Table

Page 14: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.

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.

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.

So, Decoding is the activity of converting code into plain text.

Decoding

Page 17: 1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.

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.

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.

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.

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.

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.

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.

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.

>>> 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.

>>> 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


Recommended