Lecture02-syntax-3pp

# 0 this distinction affects how the numbers are stored

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

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 ﬂoats or ints. Subtraction x-y x and y may be ﬂoats or ints. Multiplication x*y x and y may be ﬂoats or ints. Division x/y x and y may be ﬂoats or ints. The result is always a ﬂoat. Floor Division x // y x and y may be ﬂoats or ints. The result is the ﬁrst 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 ﬂoats or ints. This is the result of raising x to the y th power. Float Conversion ﬂoat(x) Converts the numeric value of x to a ﬂoat. 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 ﬂoat, 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 ﬂoat 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 ﬂoat if either operand is a ﬂoat 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 ﬁrst learned in elementary school. 83//2 is 41 with a remainder of 1. The result of ﬂoor division isn’t always an int. 83//2.0 yields 41.0 so be careful. While ﬂoor division returns an integer, it doesn’t necessarily return an int. We can insure a number is a ﬂoat or an integer by writing ﬂoat or int in front of the number. So, ﬂoat(83)//2 also yields 41.0. Likewise, int(83.0)//2 yields 41. There are inﬁnitely many real numbers but only a ﬁnite number of ﬂoats 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 ﬂoor division Example of mod operator Example of 2 to the 3rd power Example of ﬁnding...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online