0 this distinction affects how the numbers are stored

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: r x+y 1 Introduction Comments x and y may be floats or ints. Subtraction x-y x and y may be floats or ints. Multiplication x*y x and y may be floats or ints. Division x/y x and y may be floats or ints. The result is always a float. Floor Division x // y x and y may be floats or ints. The result is the first integer less than or equal to the quotient. Remainder or Modulo x%y x and y must be ints. This is the remainder of dividing x by y . Exponentiation x ** y x and y may be floats or ints. This is the result of raising x to the y th power. Float Conversion float(x) Converts the numeric value of x to a float. Integer Conversion int(x) Converts the numeric value of x to an int. The decimal portion is truncated, not rounded. Absolute Value abs(x) Gives the absolute value of x. Round round(x) Rounds the float, x , to the nearest whole number. The result type is always an int. Fig. 1.18 Numeric operations digits, like 83 for instance. Real numbers, called float in Python, are written with a decimal point as in 83.0. This distinction affects how the numbers are stored in memory and what type of value you will get as a result of some operations. In Fig. 1.18 the type of the result is a float if either operand is a float unless noted otherwise in the table. Dividing the integer 83 by 2 yields 41.5 if it is written 81/2. However, if it is written 83//2 then the result is 41. This goes back to long division as we first learned in elementary school. 83//2 is 41 with a remainder of 1. The result of floor division isn’t always an int. 83//2.0 yields 41.0 so be careful. While floor division returns an integer, it doesn’t necessarily return an int. We can insure a number is a float or an integer by writing float or int in front of the number. So, float(83)//2 also yields 41.0. Likewise, int(83.0)//2 yields 41. There are infinitely many real numbers but only a finite number of floats that can be represented by a computer. For instance, the number PI is approximately 3.14159. However, 26 Examples of how to use the arithmeAc operators Example of floor division Example of mod operator Example of 2 to the 3rd power Example of finding...
View Full Document

Ask a homework question - tutors are online