Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

Similar documents
CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Chapter 4: Basic C Operators

CS & IT Conversions. Magnitude 10,000 1,

MACHINE LEVEL REPRESENTATION OF DATA

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

Python Programming Exercises 1

Objectives. Connecting with Computer Science 2

CHW 261: Logic Design

Introduction to Computers and Programming. Numeric Values

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

CHAPTER 2 Number Systems

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

2 Number Systems 2.1. Foundations of Computer Science Cengage Learning

C++ Programming: From Problem Analysis to Program Design, Third Edition

Values, Variables, Types & Arithmetic Expressions. Agenda

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

COMP2121: Microprocessors and Interfacing. Number Systems

Chapter 2 Exercises and Answers

Chapter 1. Numeric Artifacts. 1.1 Introduction

Level ISA3: Information Representation

CMSC 104 -Lecture 2 John Y. Park, adapted by C Grasso

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

ANSI C Programming Simple Programs

1.1 Review of Place Value

Python Programming: An Introduction to Computer Science

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

COMP Overview of Tutorial #2

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

Real Numbers finite subset real numbers floating point numbers Scientific Notation fixed point numbers

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Chapter 2. Data Representation in Computer Systems

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

CMPS 10 Introduction to Computer Science Lecture Notes

Chapter 1 Emad Felemban

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems

Number Systems MA1S1. Tristan McLoughlin. November 27, 2013

Module 2: Computer Arithmetic

Computer Organisation CS303

Octal and Hexadecimal Integers

The. Binary. Number System

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems

Divide: Paper & Pencil

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

hp calculators HP 33S Working with Fractions Simple Examples Using Fractions Entering and Using Fractions Fractions in Programs and Equations

These are reserved words of the C language. For example int, float, if, else, for, while etc.

TOPICS. Other Number Systems. Other Number Systems 9/9/2017. Octal Hexadecimal Number conversion

What Is It? Instruction Register Address Register Data Register

Introduction to Numbering Systems

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Floating-point representation

Number Systems CHAPTER Positional Number Systems

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Lecture (01) Digital Systems and Binary Numbers By: Dr. Ahmed ElShafee

Lecture 2: Python Arithmetic

Variable and Data Type I

Outline. Data and Operations. Data Types. Integral Types

Basic Arithmetic Operations

Conversion Between Number Bases

Fixed-Point Math and Other Optimizations

Number Systems. Both numbers are positive

Numeral Systems. -Numeral System -Positional systems -Decimal -Binary -Octal. Subjects:

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Common Core State Standards Mathematics (Subset K-5 Counting and Cardinality, Operations and Algebraic Thinking, Number and Operations in Base 10)

Octal & Hexadecimal Number Systems. Digital Electronics

Digital Computers and Machine Representation of Data

Chapter 7. Basic Types

Bits, Words, and Integers

Switching Circuits and Logic Design Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

THE LOGIC OF COMPOUND STATEMENTS

Final Labs and Tutors

Introduction to Programming

CITS 1401 Problem Solving & Programming

CS321. Introduction to Numerical Methods

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

CHAPTER 2 Data Representation in Computer Systems

BINARY SYSTEM. Binary system is used in digital systems because it is:

Introduction to Computer Science (I1100) With 1 coin 2 possibilities: Head / Tail or 0/1

CHAPTER 2 Data Representation in Computer Systems

x= suppose we want to calculate these large values 1) x= ) x= ) x=3 100 * ) x= ) 7) x=100!

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Math Glossary Numbers and Arithmetic

T02 Tutorial Slides for Week 2

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

CHAPTER 2 (b) : AND CODES

Chapter 4 Section 2 Operations on Decimals

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

Binary Representations and Arithmetic

Properties and Definitions

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Introduction to Programming, Aug-Dec 2008

Transcription:

Python Numbers CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17 1 (adapted from Meeden, Evans & Mayberry) 2 Learning Outcomes To become familiar with the basic numeric data types in Python To understand how numbers are represented and stored in memory To be aware of the Python math library To understand the Accumulator pattern To be able to read and write programs that process numerical data 3 1

Number Basics Computers were originally designed, as the name suggests, to crunch numbers Why are numbers important? Code can be thought of as a mathematical formula Number manipulation lies at the heart of most programs Computers store all information in binary, base 2. Each bit of memory is either on, 1, or off, 0. What does the binary number system look like? 4 The Decimal Number System The decimal number system is a positional number system. Example: 5 6 2 1 1 X 10 0 = 1 10 3 10 2 10 1 10 0 2 X 10 1 = 20 6 X 10 2 = 600 5 X 10 3 = 5000 5 The Decimal Number System The decimal number system is also known as base 10. The values of the positions are calculated by taking 10 to some power. Why is the base 10 for decimal numbers? Because we use 10 digits, the digits 0 through 9. The decimal number system, and other number systems, are symbolic representations of concrete quantities 6 2

The Binary Number System The binary number system is also known as base 2. The values of the positions are calculated by taking 2 to some power. Why is the base 2 for binary numbers? Because we use 2 digits, the digits 0 and 1. 7 The Binary Number System The binary number system is also a positional numbering system. Instead of using ten digits, 0-9, the binary system uses only two digits, 0 and 1. Example of a binary number and the values of the positions: 1 0 0 1 1 0 1 2 6 2 5 2 4 2 3 2 2 2 1 2 0 8 Converting from Binary to Decimal 1 0 0 1 1 0 1 1 X 2 0 = 1 2 6 2 5 2 4 2 3 2 2 2 1 2 0 0 X 2 1 = 0 1 X 2 2 = 4 2 0 = 1 2 4 = 16 1 X 2 3 = 8 2 1 = 2 2 5 = 32 0 X 2 4 = 0 2 2 = 4 2 6 = 64 0 X 2 5 = 0 2 3 = 8 2 7 = 128 1 X 2 6 = 64 77 10 9 3

Converting from Binary to Decimal Practice conversions: Binary Decimal 11101 1010101 100111 10 Geek Joke #1 Seen on a random T-shirt: There are 10 kinds of people in the world: Those who understand binary and those who don t 11 Converting from Decimal to Binary Make a list of the binary place values up to the number being converted. (In the example below, 2 5 is the largest possible leftmost position) Perform successive divisions by 2, placing the remainder of 0 or 1 in each of the positions from right to left. Continue until the quotient is zero. Example: 42 10 64 32 16 8 4 2 1 _ 1 0 1 0 1 0 2 6 2 5 2 4 2 3 2 2 2 1 2 0 12 4

Converting from Decimal to Binary Practice conversions: Decimal 59 82 175 Binary 13 Working with Large Numbers 0 1 0 1 0 0 0 0 1 0 1 0 0 1 1 1 =? Humans can t work well with binary numbers; there are too many digits to deal with. Memory addresses and other data can be quite large. Therefore, we sometimes use the hexadecimal and octal number systems. 14 The Hexadecimal Number System The hexadecimal number system is also known as base 16. The values of the positions are calculated by taking 16 to some power. Why is it base 16 for hexadecimal numbers? Because we use 16 symbols, the digits 0 through 9 and the letters A through F. 15 5

The Hexadecimal Number System Binary Decimal Hexadecimal Binary Decimal Hexadecimal 0 0 0 1010 10 A 1 1 1 1011 11 B 10 2 2 1100 12 C 11 3 3 1101 13 D 100 4 4 1110 14 E 101 5 5 1111 15 F 110 6 6 111 7 7 1000 8 8 1001 9 9 16 The Hexadecimal Number System Example of a hexadecimal number and the values of the positions: 3 C 8 B 0 5 1 16 6 16 5 16 4 16 3 16 2 16 1 16 0 17 The Octal Number System Example of an octal number and the values of the positions: 1 3 0 0 2 4 8 5 8 4 8 3 8 2 8 1 8 0 Binary equivalent: 1 011 000 000 010 100 = 1011000000010100 18 6

Example of Equivalent Numbers Binary: 1 1 0 1 0 0 0 0 1 0 1 0 0 1 1 1 2 Octal: 150247 8 Decimal: 53415 10 Hexadecimal: D0A7 16 19 Notice how the number of digits gets smaller as the base increases. But Why Use Hex or Octal? Simple: can divide binary numbers into equalsized sets of bits, then convert directly This is not true of decimal-to- {binary,hex,octal} 20 Python Operators Operation Python Operator Addition + Subtraction - Multiplication * Division / Exponentiation ** Modulus % 21 7

In-Place Operators All of the arithmetic operators have an inplace version for convenience: x += 1 # Adds 1 to x, like x = x + 1 x -= 2 # equiv. to x = x - 2 x *= 6 x /= 5 x **= 3 x %= 2 22 Integers vs. Floats Integers Truncates floating point results Always has exact values Used for things that are countable Faster arithmetic operations Stored in 32 bits (4 bytes) one bit holds the sign and 31 bits store the number Largest possible int is 2147483647 Floats Allows floating point results Sometimes has approximations of values Used for continuous data Slower arithmetic operations Stored in 64 bits (8 bytes) one bit holds the sign, 11 bits hold the exponent and 52 bits hold the mantissa Much larger, since numbers are stored in exponential form 23 Integers vs. Floats Many languages are difficult to use when a program requires working with integers that are larger than 2147483647. Since a different data type has to be used to hold the values, the code has to be rewritten. Python handles this problem for you by seamlessly continuing to run the program by switching the data type to long ints automatically. 24 8

Long Integers The third numeric type in Python is the long int. A long int doesn't have a fixed size. It expands to the length the number needs, limited only by the amount of memory your computer has. 25 Working with Integers >>> 7 / 3 2 Both the dividend and the divisor are integers, causing the integer arithmetic unit to do the calculation. The integer arithmetic unit can take only integers as input and can produce only integer results. This is called integer division and it can be quite useful, as in the following example. 26 Working with Integers >>> length = input("enter a length in inches : ") Enter a length in inches : 27 >>> feet = length / 12 >>> inches = length % 12 >>> print length, "inches is ", feet, "feet and ", inches, "inches 27 inches is 2 feet and 3 inches 27 9

Working with Floating-Point Numbers >>> 7.0 / 3.0 2.3333333333333335 Here the floating-point arithmetic unit had to be used for the calculation. Floats are not always exact answers. We all know the last digit here should be a 3, not a 5. If the number is a repeating decimal, or if it has more decimal places than there is space, the value may be slightly off or truncated. 28 Types How can you tell what type of number you are using? >>> type(1) <type 'int'> >>> type(1.0) <type 'float'> >>> type(1.0 + 2) <type 'float'> >>> mynum = 4 >>> type(mynum) <type 'int'> >>> mynum = mynum + 2.0 >>> type(mynum) <type 'float'> 29 Types Notice that numbers are automatically converted to floats if an operation contains both integers and floats. Preserves accuracy of calculation Explicit conversion can be done with float() >>> type(float(1)) <type 'float'> >>> float(7)/3 2.3333333333333335 30 10

Variable Types Unlike many other languages, Python does not require you to preselect a type for your variables No variable declarations needed Variables are created the first time they are written into Their type can change to fit the current need when rewritten 31 Math Library The math library contains many useful math functions. You must have 'import math' in your program to use the functions in the math library. More information can be found on the official Python documentation page. 32 Math Library Functions Function cos(x),sin(x),tan(x) log(x,base) floor(x) ceil(x) sqrt(x) pi e Purpose Trigonometric functions Logarithm of x with given base Floor function (closest integer less than or equal to x) Ceiling function (closest integer greater than or equal to x) Square root of x Constant that represents pi Constant that represents e 33 11