Download Week 2 Recitation Slides - Purdue CS Wiki/Application server

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS 177 Week 2 Recitation Slides
Introduction
1
Announcements
2
What Computers Understand?


Computers can only understand Encoding of
numbers.
So what is the meaning of
Encoding?
How can we represent voltages?

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.


Encoding
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.
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
Decimal to Binary Conversion

To convert the decimal number 26 into binary,

In a computer, 26 is represented as 11010
7
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
Examples
23 = 8 * 1 = 8 +
22 = 4 * 1 = 4 +
21 = 2 * 0 = 0 +
20 = 1 * 1 = 1
Interpreted as 13
Can you do this?
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.
Example 2: Character Codes


Now suppose that you want to encode the previous 3
symbols : ( ) using numbers…
You may use the ASCII code …
ASCII Table
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

How many bits are there in 5 bytes?
eight binary digits long.
Questions


How many combinations can be represented in one
bit? Two bits?
What about three bits and four bits?
Decoding

So, Decoding is the activity of converting code
into plain text.
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
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
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
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
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
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
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
Examples
>>> 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
>>> 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