Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Python Programming: An Introduction to Computer Science
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Chapter 3 Computing with Numbers
Data Representation Number Systems.
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.
Section 1.1 Numbers and Their Properties.
Objectives You should be able to describe: Data Types
CSC 110 Numeric data types [Reading: chapter 3] CSC 110 D 1.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IMS 3253: Math 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Five Fundamental Math Operations Precedence of Math.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
CSC1015F – Chapter 3, Computing with Numbers Michelle Kuttel
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
Input, Output, and Processing
CPS120: Introduction to Computer Science Operations Lecture 9.
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
© 2010 Pearson Prentice Hall. All rights reserved. CHAPTER 5 Number Theory and the Real Number System.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Significant Figure Rules RulesExamples The following are always significant Non zero digits Zeros between non zero digits Zero to the right of a non zero.
Copyright © 2015, 2008, 2011 Pearson Education, Inc. Section 4.1, Slide 1 Chapter 4 Exponential Functions.
Mathematical Calculations in Java Mrs. C. Furman.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
The Math Class Methods Utilizing the Important Math Operations of Java!
Copyright © Cengage Learning. All rights reserved. Fundamental Concepts of Algebra 1.1 Real Numbers.
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Module 2.2 Errors 03/08/2011. Sources of errors Data errors Modeling Implementation errors Absolute and relative errors Round off errors Overflow and.
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.
Python Programming, 3/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Topics Designing a Program Input, Processing, and Output
Expressions.
Expressions.
Data Types and Conversions, Input from the Keyboard
BIL 104E Introduction to Scientific and Engineering Computing
Variables, Expressions, and IO
Thinking about programming
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Arithmetic Expressions & Data Conversions
Number Theory and the Real Number System
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Arithmetic Expressions & Data Conversions
Presentation transcript:

Chapter 3

 Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may be several numeric data types, perhaps six.  These different numeric data types developed because different amounts of memory were needed to store different kinds of numbers.  Fortunately, Python has just two data types: int (for integers) and float (for decimal numbers)

 Integers are just whole numbers, and can be positive or negative  Floats are numbers including fractions, expressed with a floating point (decimal).  When we type a “literal” (when we hard code a value into our program), Python recognizes whether that number is an integer or a float number, and assigns the correct data type.

 # change.py  # A program to calculate the value of some change in dollars  def main():  print "Change Counter"  print  print "Please enter the count of each coin type."  quarters = input("Quarters: ")  dimes = input("Dimes: ")  nickels = input("Nickels: ")  pennies = input("Pennies: ")  total = quarters * dimes * nickels *.05 + pennies *.01  print  print "The total value of your change is", total  main()

 Alter the change.py program so that you also count half-dollars and dollars (Kennedy and Susan B. Anthony coins)

 Python has a built-in function, type(), which tells us which data type a variable is. Just put the variable name between the parentheses.  Example: if you type newVar = 58 into the Python interpreter, then type(newVar), the result displayed will be

 In the Python interpreter, use the type() function to test the following variables:  num_units = 10  num_students=25  subtotal =  score1 = 87  average = 81.5  users_online = 112  total =

 Python performs the basic math operations on both numeric data types Operatoroperation +Addition -Subtraction *Multiplication /Division **Exponentiation %Remainder abs()Absolute value

 Exponentiation refers to the power, or exponent, of a number. For example, 2 4 refers to two to the power of four, or 2*2*2*2.  In Python, you can use the exponentiation operator, **, to find the value of exponentiation.  2 4 = 2 ** 4 = 16  Try it in the Python interpreter

 You all know what the remainder is… the value that is left after division, if there is any value (other than zero) left at all.  E.g., the remainder of 10/7 is 3.  In Python, we would express this as 10 % 7  Workshop: Use the interpreter to find the remainders: 200/23, 17/4, 25/3, 28/9, 100/12, 10/6

 The absolute value of a number is its numerical value regardless of its sign, that is, whether it’s negative or positive.  The built-in Python function abs() will always return a number’s absolute value.  The absolute value of 107 is 107; the absolute value of -33 is 33.  Workshop: use the interpreter to verify these absolute values

 There is a suite of built-in functions in Python that handle mathematical functions. See Table 3.2 in our textbook, p. 57  To have access to all these built-in functions in Python, we need first to invoke the library: import math, the very first line of the program

 We’ll limit our use of the Math library to one “property” (built-in data) and three functions.  Pi is an approximate value for pi, or ….. In Python, this gets expressed as math.pi  exp(x) will give the exponential of x  ceil(x) gives the smallest whole number greater than or equal to x  and floor(x) gives the largest whole number less than or equal to x

 In teams of two, write a program which asks the user for the radius of a circle and then calculates the area of that circle.

 In teams of two, write a program that asks the user for a float number and then rounds that number up and down.

 Sometimes integer calculations produce an integer so huge it exceeds a language and computer’s ability to display it. In other words, the number exceeds memory allocations.  When a number exceeds memory, we get an Overflow error  Very long/short floats in Python will be represented in scientific, or exponential, notation. e+n will be added to the end of the number, indicating that 10 n should be added to the number, where n is a power of 10.

 There is a third data type, long ints, which handle huge numbers. The memory allocated for a long int, or L, is flexible, not fixed like floats or integers.  When declaring a literal number, you can create a long int by just adding L to the end L  New versions of Python automatically convert humongous integers to long ints.  Except when huge numbers are expected, you should always use the int type, because it’s much faster to compute.

 Often it is necessary to convert one data type to another, since arithmetic operations on a computer are different for integers and floats.  Automatic type conversion: When there is a mixed-type expression, Python will automatically convert an integer to a float to perform the operation: average_wgt = 507.5/7  Seven will automatically be converted to a float, and average_wgt will be a float.

 Sometimes we have to force a type conversion. For example, if we’re trying to find an average, and both factors are integers, the result may well be a float. But unless we tell Python to produce a float, it will whack off the fraction and return an integer.  num_students = 12  sum = 1005  avg = sum/num_students  This will produce an integer; we need a float

 The solution is to force the result into the float type: avg = float(sum/num_students)  Python provides similar type conversion functions for integers and long integers:  >>>myInt = int(3.3) >>>print myInt 3  >>>myLInt = long(6.7) >>>print myLInt 6L

 Python also has a built-in function, round(), which rounds a float to the nearest whole float value  >>>round(7.1490) 7.0  We can then convert the rounded number into an integer, if we wanted: >>>int(round(7.1490)) 7

 Write a program which gets a number between 1 and 10 from the user and which also gets a random number from the computer. Print both so the user can compare the values.  The first line of your program should be import random  To generate a random number, type random.random()