Comp 151. More on Arithmetic and intro to Objects

Similar documents
Comp 151. More on Arithmetic and number types

Python Programming: An Introduction to Computer Science

CITS 1401 Problem Solving & Programming

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors

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

Python Programming: An Introduction to Computer Science

Introduction to Computer Programming for Non-Majors

Last Time. integer/float/string. import math math.sqrt() math.pi. Python Programming, 2/e 2

Writing Python Programs, Numerical Data Types

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

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting

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

Mark Important Points in Margin. Significant Figures. Determine which digits in a number are significant.

Computing with Numbers

MAT 003 Brian Killough s Instructor Notes Saint Leo University

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

Python Programming Exercises 1

Repeat or Not? That Is the Question!

Helping Students Understand Pre-Algebra

Objective- Students will be able to use the Order of Operations to evaluate algebraic expressions. Evaluating Algebraic Expressions

CS1 Lecture 3 Jan. 18, 2019

Rational Number is a number that can be written as a quotient of two integers. DECIMALS are special fractions whose denominators are powers of 10.

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Outline. Data and Operations. Data Types. Integral Types

Algorithms and Programming I. Lecture#12 Spring 2015

>>> * *(25**0.16) *10*(25**0.16)

Vocabulary: Looking For Pythagoras

CSCE 120: Learning To Code

Integers and Rational Numbers

Computing with Numbers Zelle - Chapter 3

Built-in Types of Data

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

Jython. secondary. memory

CGF Lecture 2 Numbers

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Choose the file menu, and select Open. Input to be typed at the Maple prompt. Output from Maple. An important tip.

Arithmetic expressions can be typed into Maple using the regular operators:

CHAPTER 1: INTEGERS. Image from CHAPTER 1 CONTENTS

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CHAPTER 3: CORE PROGRAMMING ELEMENTS

Lecture 3. Input, Output and Data Types

Intro. Scheme Basics. scm> 5 5. scm>

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

T H E I N T E R A C T I V E S H E L L

CS1 Lecture 3 Jan. 22, 2018

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Lecture 2: Variables & Assignments

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Numerical Precision. Or, why my numbers aren t numbering right. 1 of 15

MITOCW watch?v=kz7jjltq9r4

Lecture 2: Python Arithmetic

Python Intro GIS Week 1. Jake K. Carr

MAT 090 Brian Killough s Instructor Notes Strayer University

Introduction to Engineering gii

Floating Point January 24, 2008

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Graphics calculator instructions

Table of Contents. Introduction to the Math Practice Series...iv Common Mathematics Symbols and Terms...1

Math Day 2 Programming: How to make computers do math for you

(Refer Slide Time: 02:59)

Numerical computing. How computers store real numbers and the problems that result

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CS112 Lecture: Primitive Types, Operators, Strings

CPS122 Lecture: From Python to Java

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Getting Started Values, Expressions, and Statements CS GMU

Programming for Engineers Introduction to C

Chapter 1 Operations With Numbers

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

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

Representing numbers on the computer. Computer memory/processors consist of items that exist in one of two possible states (binary states).

Unit 2: Accentuate the Negative Name:

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Pre-Algebra Notes Unit One: Variables, Expressions, and Integers

Julia Calculator ( Introduction)

may be sent to:

CITS2401 Computer Analysis & Visualisation

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Our Strategy for Learning Fortran 90

Operators and Expressions:

Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

These are notes for the third lecture; if statements and loops.

Inequalities and you 3

Basics of Computation. PHY 604:Computational Methods in Physics and Astrophysics II

Long (or LONGMATH ) floating-point (or integer) variables (length up to 1 million, limited by machine memory, range: approx. ±10 1,000,000.

Significant Figure Rules

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

BEGINNING PROBLEM-SOLVING CONCEPTS FOR THE COMPUTER. Chapter 2

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Machine Arithmetic 8/31/2007

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Transcription:

Comp 151 More on Arithmetic and intro to Objects

Admin Any questions 2

The Project Lets talk about the project. What do you need A 'accumulator' variable. Start outside of the loop Lets look at your book's interest example 3

Strings Remember Strings from last time Strings represent text. Discussion of data representation in a computer 1000001 = 65 (A) 110000 = 48 (0) Literal strings (actually written out and not in a variable or anything) Represented inside of quotes in python. 4

Documentation Strings Triple quoted strings that are put frst in a function becomes the documentation for that function The help text on that function we ll look at that more closely next time 5

Literal strings Three types of literal strings in python Double quoted strings: Eg: Its called a 'string' in programming Single quoted strings Eg: 'she said how do you do yesterday' Triple quoted strings (either type of triple quotes) Eg this is an example of a triple quoted string What do you notice about each type of string? 6

Getting help Python has good integrated help Very easy from pycharm Highlight the function/command you want help for Press F1(problems on windows) Choose menu<view><quick Documentation Lookup> Or turn on easy showing in settings/preferences Go to settings (or preferences on mac) <editor><general> and select the Show quick documentation on mouse move checkbox. If there is help text, it will be shown Works diferently from command line 7

Data Types Review from last time Data represented very diferently in computer depending on its type Integer, foating point, character, objects Python hides some of this from you, but the distinction still exists. Time cost of this hiding. Can fnd the type of anything in python by using the type functions demo 8

arithmetic + - and * all work as expected results are 'closed' on the integers English translation: integer + integer always yields an integer. division is not closed on the integers There is a major diference between python 2 and python 3 here. Discuss. but in programming it is print( 8/2) shows? print(7/2) shows? 9

Integer division in python3 In python3 can force integer division Using // 7//2 is what? Why would I want this? Grid drawing example Searching a list example Modulus/remainder : % Explain Rational, integer division & random examples 1

Rest of arithmetic ** exponentiation abs() 4**3 = 4*4*4 Absolute value function 1

Libraries Thus far all of our functions are from the builtin module Now we want to use math functions from the math module In order to use stuf from another module need to import it. Lets use math import math Put at top of your fle Module/library terms in code. 1

Math library functions See 3-2 page 68 in the text for some functions Or https://docs.python.org/3.6/library/math.h tml For the whole thing 1

Using the Math Library To access the sqrt library routine, we need to access it as math.sqrt(x). Using this dot notation tells Python to use the sqrt function found in the math library module. To calculate a root, you can do discroot = math.sqrt(b*b 4*a*c) How would the above be written in mathematical notation? If time have students work through factorial example for me Python Programming, 2/e 14

The Limits of Int What is 100!? >>> main() Please enter a whole number: 100 The factorial of 100 is 93326215443944152681699238856266700490715968264381621468 59296389521759999322991560894146397615651828625369792082 7223758251185210916864000000000000000000000000 Wow! That s a pretty big number! Python Programming, 2/e 15

The Limits of Int Newer versions of Python can handle it, but Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> import fact >>> fact.main() Please enter a whole number: 13 13 12 11 10 9 8 7 6 5 4 Traceback (innermost last): File "<pyshell#1>", line 1, in? fact.main() File "C:\PROGRA~1\PYTHON~1.2\fact.py", line 5, in main fact=fact*factor OverflowError: integer multiplication Python Programming, 2/e 16

The Limits of Int What s going on? While there are an infinite number of integers, there is a finite range of ints that can be represented. This range depends on the number of bits a particular CPU uses to represent an integer value. Old PCs/raspberry pi s use 32 bits. (newer ones use 64 bits) Python Programming, 2/e 17

The Limits of Int Lets use 32 bits for a Raspberry Pi That means there are 232 possible values, centered at 0. This range then is 231 to 2 31-1. We need to subtract one from the top end to account for 0. But our 100! is much larger than this. How does it work? Python Programming, 2/e 18

Handling Large Numbers Does switching to float data types get us around the limitations of ints? If we initialize the accumulator to 1.0, we get >>> main() Please enter a whole number: 15 The factorial of 15 is 1.307674368e+012 We no longer get an exact answer! Python Programming, 2/e 19

Handling Large Numbers: Long Int Very large and very small numbers are expressed in scientific or exponential notation. 1.307674368e+012 means 1.307674368 * 1012 Here the decimal needs to be moved right 12 decimal places to get the original number, but there are only 9 digits, so 3 digits of precision have been lost. Python Programming, 2/e 20

Handling Large Numbers Floats are approximations Floats allow us to represent a larger range of values, but with lower precision. Python has a solution, expanding ints! Python Ints are not a fixed size and expand to handle whatever value it holds. Python Programming, 2/e 21

Type Conversions We know that combining an int with an int produces an int, and combining a float with a float produces a float. What happens when you mix an int and float in an expression? x = 5.0 + 2 What do you think should happen? Python Programming, 2/e 22

Type Conversions For Python to evaluate this expression, it must either convert 5.0 to 5 and do an integer addition, or convert 2 to 2.0 and do a floating point addition. Converting a float to an int will lose information Ints can be converted to floats by adding.0 Python Programming, 2/e 23

Type Conversion In mixed-typed expressions Python will convert ints to floats. Sometimes we want to control the type conversion. This is called explicit typing. Python Programming, 2/e 24

Type Conversions >>> float(22//5) 4.0 >>> int(4.5) 4 >>> int(3.9) 3 >>> round(3.9) 4 >>> round(3) 3 Now we can get rid of eval for inputs of numbers!!!! How might we do that? Python Programming, 2/e 25

Assignment Read chapter 3 2