* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download type
Law of large numbers wikipedia , lookup
Large numbers wikipedia , lookup
Location arithmetic wikipedia , lookup
Principia Mathematica wikipedia , lookup
Approximations of π wikipedia , lookup
Proofs of Fermat's little theorem wikipedia , lookup
Positional notation wikipedia , lookup
Types and Arithmetic Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1 Data Types • Each variable has specific type – numeric types (int and float) – strings – boolean (True or False) • Define capabilities of variable – Numeric types manipulated with arithmetic operators – Strings manipulated with string functions – Booleans used in branching statements Dynamic Data Typing • Based on current value stored in variable x = 3 x = “Fred” x is an integer x is now a string – Other languages (C, Java, etc.) require type to be set when variable declared and not later changed – Good idea to not change type in middle of program (confusing!) • Can use type function to see current type of variable Numeric Data Types • Two different numeric types – integer (whole numbers) – floating point (numbers with decimals) • Based on type of literal assigned to variable – No decimal integer – Decimal float – Even if 0 after decimal 2.0 is float Numeric Data Types and Memory • Numeric values must be stored in binary memory – No limit to size of integers in Python • Other languages may limit to fixed storage size(64 bits, etc.) • Problem: Some numbers may require infinite number of digits to store – Example: 1/3 = 0.3333333333333333333333… – Cannot store with complete accuracy on computer! Float Type and Memory • Python allocates limited number of digits for floats – Usually about 16 digits • 1/3 = 0.3333333333333333 in Python • Affects accuracy of computation – 5 * 2/3 ≠ 2/3 * 5 • Difference between integer and float numbers – integer arithmetic guaranteed to be accurate – float arithmetic not guaranteed Floats and Exponential Notation • Exponential notation: digits e exponent – Equivalent to digits × 10exponent • Used to store arbitrarily large/small floats with limited number of digits – 0.0000000000000000000000000000123 1.23e-30 – Why called “floating point” numbers Arithmetic Operators • Basic mathematical operators: + addition - subtraction Also have unary minus * multiplication -number / division ** exponentiation // quotient (like division but truncates result to integer) % remainder (what is left over after quotient) Example: x = 23 // 5 y = 23 % 5 4 3 Arithmetic Operators and Types • Result type based on operand type – integer op integer integer – float op float float – float op integer or integer op float float • Exception: division – integer / integer float • Even if the operands are divisible! • If need to get a whole number, use // instead Explicit Type Conversion • Can use type(value) to evaluate value as type x = float(3) y = int(3.52) 3.0 3 • Used to manually truncate to integer – Not same as rounding! • Can convert strings to/from equivalent numbers s = str(3.52) ‘3.52’ x = float(“3.52”) 3.52 y = int(“3”) 3 Parsing Input to Strings • input function always returns a string – Even if user inputs a number • Must convert to number before manipulating with arithmetic operators – Otherwise get runtime error • Usual form: 1. Prompt for value (stored in string) 2. Use int or float to get correspond numeric value 3. Manipulate numeric value Example: Celsius to Fahrenheit Order of Evaluation • x = 7 + 3 * 2 – Is this 20? Is this 13? – Depends on order of evaluation of operators • Rules of operator precedence: – – – – – Exponentiation done first Unary minus done next Multiplicative operators (*, /, %, //) done next Additive operators (+, -) done last Operators at same level done left to right Parentheses and Precedence • Can use parentheses to change order – Expression in parentheses evaluated before use in rest of expression x = (7 + 3) * 2 20 Incremental Operators • Many statements of form variable = variable op value – Variable changed in terms of current value • Shortcut syntax: variable op= value • Examples: x += 1 same as x = x + 1 x *= 2 same as x = x * 2 …