Computing with Numbers CSC 161: The Art of Programming Prof. Henry Kautz 9/14/2009.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009.
1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Chapter 2 Data Types, Declarations, and Displays
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Elementary Algebra Exam 4 Material Exponential Expressions & Polynomials.
Chapter 3 Computing with Numbers
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers Python Programming, 2/e1.
MCS 177 Lab Fall 2014 Sept. 2, GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Contact Info Course Instructor: Louis Yu Lab.
Vahé Karamian Python Programming CS-110 CHAPTER 3 Computing with Numbers.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Computer Science 111 Fundamentals of Programming I Number Systems.
An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.
Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Input, Output, and Processing
Variables and Arithmetic. From last class More drawing functions: strokeWeight(n); // higher the n value broader the stroke fill(k) ; // single parameter.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
1 CS1110 Fall 2011 David Gries, Steve Marschner Reading for this lecture and previous lecture: Sections 1.1, 1.2, 1.3. Lab 1 will give you practice with.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Type accurately challenge! This is a starter activity and should take 2 minutes [ slide 1 ] 1.Can you type out the code in Code Box 2.1 with no errors.
Building Java Programs Primitive Data and Definite Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of an assignment is an expression. An expression is anything.
1 CS1100 Fall Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems CS100J:
1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
1 CS100J Spring Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Building Java Programs
Lecture 2: Operations and Data Types
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Primitive Data, Variables, Loops (Maybe)
Building Java Programs Chapter 2
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Building Java Programs
CSI 101 Elements of Computing Spring 2009
Building Java Programs
Building Java Programs Chapter 2
More elements of Python programs
Building Java Programs
Introduction to Programming with Python
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
SSEA Computer Science: Track A
Building Java Programs
Building Java Programs
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Another Example Problem
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
Introduction to Python
Presentation transcript:

Computing with Numbers CSC 161: The Art of Programming Prof. Henry Kautz 9/14/2009

Course Status Readings Completed: Chapters 1 and 2, pre-reading of Chapter 3 To do: Read Chapter 3 Pre-read Sections 11.1 – 11.2 (Data Collections: Lists) Assignments Completed: Assignment 1, Using IDLE (only steps 1 – 5) Turn in hardcopy today, if you have not already To do: Assignment 2, Newton & Kepler Work on with a partner during the lab sections this week Turn in by uploading to Blackboard no later than 10:00am Saturday 19 Sept Workshops In progress Sun-Tue this week: Origami 2 Computing with Numbers

Labs Some students are concerned about amount of time required by labs + workshops + lectures Good things about labs: Can complete assignments much more quickly when help is available – one hour versus an all-nighter Time & place to work with a partner (“eXtreme programming”) But: not all students will need two full lab sessions to complete the week’s assignment How to maximize learning & minimize burden? 3 Computing with Numbers

Lab Policy Attend at least one lab session each week No need to attend sessions for assignments that you have completed and turned in Some sessions will require attendance for organizational and/or informational purposes These will be announced & noted on Calendar page You need to attend lab tomorrow (Tuesday 15 Sept) in order to choose your (first) lab partner Everyone will change partners partway through the semester 4 Computing with Numbers

Changing Sections Need to change your Workshop section? Contact the head workshop leader: Ben Hopkins Need to change your Lab section? Complete a Drop/Add form at the Registrar’s Office What should you do while waiting for a change to be processed? Go ahead and start attending the section to which you intend to change 5 Computing with Numbers

Topics this Class Loops Numeric data types The Math library Converting between numeric data types 6 Computing with Numbers

Indefinite Loops while num != guess: if guess < num: print "Too Low!” else: print "Too High!” General form: while : 7 Computing with Numbers

Definite Loops Suppose we repeat an operation exactly 10 times One approach: n = 0 while n < 10: print n, "squared is", n*n n = n + 1 This pattern is so common that there is a shorthand way of writing it for n in range(10) print n, "squared is", n*n 8 Computing with Numbers

Definite Loops General form: for in : Try typing to the Python shell >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 9 Computing with Numbers

Definite Loops What will this do? for n in [0, 1, 2] print n, "squared is", n*n 0 squared is 0 1 squared is 1 2 squared is 4 10 Computing with Numbers

Definite Loops What will this do? for n in [8, 1, 5] print n, "squared is", n*n 8 squared is 64 1 squared is 1 5 squared is Computing with Numbers

Data Types Type: A category or set of data values. Constrains the operations that can be performed on the data Examples: integer, real number, text string Python is relaxed about types A variable's type does not need to be declared. A variable can change types as a program is running. ValuePython type 42int 3.14float "ni!"str

Numeric Types Python function type tells us the data type of any expression >>> type(3) >>> type(3.14) >>> type(3.0) >>> type( ) Why do you think that "float" is used for the type of numbers that have a decimal point? Computing with Numbers

Arithmetic Operators expression: A value or operation(s) to compute a value. Example: * 3 Arithmetic operators: + - * / add, subtract/negate, multiply, divide ** exponentiate % modulus, a.k.a. remainder precedence: Order in which operations are computed. * / % ** have a higher precedence than * 4 is 13 (1 + 3) * 4 is 16 Computing with Numbers

Integer division What is 218 % 100 ?

Mixed Expressions Mixing integers and floats yields floats >>> type( 6 * 3.14 ) Why does that make the most sense Changing a float back to an integer: round(3.6) == 4 int(3.6) == 3 Computing with Numbers

Logic Logical expressions can be combined using logical operators: OperatorExampleResult and(9 != 6) and (2 < 3)True or(2 == 3) or (-1 < 5)True notnot (7 > 0)False OperatorMeaningExampleResult == equals == 2True != does not equal 3.2 != 2.5True < less than 10 < 5False > greater than 10 > 5True <= less than or equal to 126 <= 100False >= greater than or equal to 5.0 >= 5.0True

True or False? (17 / 4) * 4 == 17 (17 / 4.0) * 4 == 17 (17 / 4) * 4.0 == 17 Computing with Numbers

The Math Library Many more mathematical functions can be accessed by importing the math library Two ways to import a library: >>>import math >>> math.sqrt(81) 9 >>> from math import * >>> sqrt(81) 9 Computing with Numbers

Mini-Exercise Task: computer the distance between two points on the plain (x1, y1) and (x2, y2) What is the formula for distance? Write a Python expression for the distance, using the exponentiation operator ** and the math library function math.sqrt(x) (square root) Computing with Numbers

Why Bother with Both Integers and Floats? Why not only use floats? Integers: represented by a 32 bit binary number Largest value is 2**31 – 1 == 2,147,483,6472,147,483,647 Fast Exact Floats: represented internally using scientific notation == 4.2× == 4.2×10 −9 Much slower Value might be approximate, not exact Computing with Numbers

Computer Math >>> == False >>> == False >>> == True Computing with Numbers

Long Integers Sometimes you may want to represent a very big number exactly Python has a special data type to do this: long integers Long integers can have any number of digits (until your computer runs out memory)! Computing with Numbers

More Computer Math >>> == True >>> == False >>> type( ) Computing with Numbers

Assignment 2 Kepler & Newton Calculating Planetary Orbits Computing with Numbers