Chapter 4 Numbers CSC1310 Fall 2008. Python Program Structure import math r=int(raw_input()) print 4.0/3*math.pi*r**3 volume.py: Program Program Module.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Types and Arithmetic Operators
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
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
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Java Chapter 2 Creating Classes. Class Defines structure of an object or set of objects; Includes variables (data) and methods (actions) which determine.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
Introduction to Python
Chapter 2 part #4 Operator
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects.
D ATA T YPE. NUMBERS A number is an immutable type – means that changing or updating its value result in a newly allocated object. There are several types.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Input, Output, and Processing
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Variables, Expressions and Statements
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012
INC 161 , CPE 100 Computer Programming
Arithmetic Operator Operation Example + addition x + y
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Core Objects, Variables, Input, and Output
Data Types and Expressions
CHAPTER 3: String And Numeric Data In Python
Terminal-Based Programs
Data Types and Expressions
Data Types and Expressions
Introduction to Python
Presentation transcript:

Chapter 4 Numbers CSC1310 Fall 2008

Python Program Structure import math r=int(raw_input()) print 4.0/3*math.pi*r**3 volume.py: Program Program Module Module Statement Statement Expression Expression

Python Built-in types Numbers , 1234, 999L, 3+4j Strings “Hello World”, ‘spam’ Lists [1,2,3,4], [1,2,[3,4]] Dictionaries {‘test’: ‘yum’} Tuples (1, 2, 3, 4) Files input=open(‘file.txt’, ‘r’).read()

Numbers Integer 222, -24, 0 (32 bit) [-2 31, ] Long integer L,33657l Floating-point 1.23, 123e-2, 0.123e+2 Octal literals 0177, 01234, 0456 (0-8) Hex literals 0x9ff, 0XFf (0-9, A,B,C,D,E,F) Complex number literals 3+4j, 3j, 4J+3, complex(3,4)(real,imag) realpart+imaginarypart j realpart+imaginarypart J

Assignment Variables Variables – a name that is used to keep some information To create variable, assign a value to it. Variable in expression is replaced by its value. Variable should be assigned before it is used in expression. >>> a=3 #Name Created >>> b=4 >>>s=d=0 What will happen if I ask to print variable c at this point?

Mathematical Operators + Addition (a + b) - Subtraction (a - b) * Multiplication (a * b) / // Division (a / b, a // b) (integer, floating-point; floor division) (from 3.0: true and floor division) % Remainder/format (a % b) ** Binary power (a ** b) - Unary negation (- b) + Identity (+ b)

Variable _ _ In interactive mode, the last printed expression is assigned to the variable _. >>> tax = 12.5 / 100 >>> price = >>> price * tax >>> price + _ >>>_*tax

Comparison and Value Equality Operators Value equality == <> != (a == b, a <> b, a != b) Comparison operators = (a b, a = b)

Mixed Operators, Types Operator Precedence  unary negation, identity, binary power  multiplication, remainder/ format, division  addition, subtraction  comparison and value equality operators Parentheses to group sub-expressions Different numeric types in expression:  Python converts operand up to the type of the most complicated operand and then calculates result. Integer -> Long integer-> Floating-point numbers-> Complex numbers. Integer -> Long integer-> Floating-point numbers-> Complex numbers.

Numeric Representation Interactive prompt shows more digits than print >>> b / (2.0 + a) >>> print b/(2.0+a) String formatting >>> num= >>> “%e” % num >>> “%2.2f” % num >>> str(num) >>> repr(num)

Long Integers Arbitrarily big Convert integer to long integer if overflow occurs (after 2.2) More precision vs lower performance

Hexadecimal and Octal Notations Octal notation (base 8) Octal notation (base 8)  leading zero (0) (!!!!!)  octal digits 0-7 (each represents 3 bits) >>> oct(64) Hexadecimal notation (base 16) Hexadecimal notation (base 16)  leading 0X or 0x  hex digits 0-9 (each represents 4 bits)  upper- or lowercase A-F (each represents 4 bits) >>> hex(64) String to integer and vise-verse String to integer and vise-verse >>> int(’64’), int (‘0xf’,16), int (‘034’,8) >>> eval(’64’), eval(‘0xf’), eval(‘034’) #slower >>> “%o %x %X” % (64,64,255)

Built-in functions and modules >>>import math >>>math.pi, math.e >>>math.cos(2*math.pi/3) #sin, tan, acos, asin, atan >>>math.exp(4) #math.e**4 >>>math.pow(math.e,4), pow(math.e,4) >>>math.log (math.e**4) >>>math.sqrt(4) >>>math.ceil(2.0/3), math.floor(2.0/3) >>>int(2.567), round(2.567), round(2.4) #float,long >>>print round(2.567,2) >>>abs(-2), abs(2.0) #available without import >>>divmod(3,4)

Dynamic Typing runtime Types are determined at runtime (no need for declaration as in C++, JAVA) Creation Creation: variable is created when it is first assigned a value. Types: Types: there is no type information associated with variable. Variable refers to some object of a particular type. Use Use: when a variable is in expression, it is immediately replaced with a referenced object. Unassigned variable results in error.

>>>a = 3 3. Link the variable a to the new object 3 1.Create an object to represent the value of 3 2. Create the variable a, if it does not yet exist Variables always link to objects Larger objects may be link to other objects.

Shared Reference >>>a=3 >>>b=a>>>a=“change” “change” Objects Objects are allocated memory Variables Variables are entries in search table with space for a link to an object (pointers, not labels)

Garbage Collection >>>a=3 >>>a=5 5