Lessons on Python Numbers Walter Didimo [ 30 minutes ]
Types of numbers There are only three kinds of number in Python: integer any integer number floating-point any real number complex any number having an imaginary part Examples: 10 is an integer number 10.0 is a floating-point number 10 + 4j is a complex number (4 is the imaginary part)
Function type() The function type() returns the type of a number >>> type(10) <class 'int'> >>> type(10.0) <class 'float'> >>> type(10j) <class 'complex'> More in general, function type() can be used to know the type (class) of any data >>> type('hello') <class 'str'>
How to format numbers You can use the %d, %f, %e, and %E parameters to concatenate integers and floats to a string >>> print("int and float --> %d and %f" % (10, 2.5)) int and float --> 10 and 2.500000 >>> print("int and float --> %d and %e" % (10, 2.5)) int and float --> 10 and 2.500000e+00 >>> print("int and float --> %d and %E" % (10, 2.5)) int and float --> 10 and 2.500000E+00 >>> print("int --> %d" % 10.6) What do you expect? Try yourself
How to format numbers You can also control the number of decimal digits that must be displayed >>> print("rounded float --> %.02f" % 12.567) rounded float --> 12.57 >>> print("rounded float --> %.03E" % 234.455) rounded float --> 2.345E+02
Complex numbers How can you format a complex number? use the.real and.imag attributes to extract its real and imaginary part, respectively format each of the two parts (e.g., with %f) >>> (10+4j).real 10.0 >>> (10+4j).imag 4.0 >>> n = (10+4j) >>> print("(%0.2f,%0.2f)" % (n.real, n.imag)) (10.00,4.00)
Basic math You can do basic math with numbers, by applying the classical operators + summation - subtraction * multiplication ** power / division // integer division Parenthesis can be used to force precedence rules (like in a simple calculator)
Basic math Notice that, in the new versions of Python, the division operator / always returns the exact result (even if both the operators are integer) >>> (10+24)*5 170 >>> 10/4 2.5 >>> 10//4 2 >>> 2E340*10 inf this stands for infinity (too large)
More examples >>> 2**3 / (10-4) 1.3333333333333333 >>> print("%f" % (2**3 / (10-4))) 1.333333 few digits shown by default >>> print("%.30f" % (2**3 / (10-4))) 1.333333333333333259318465024990
More about integer division Notice that, the result of an integer division is always rounded down (towards inf) >>> -10//4-3 (obtained by rounding down -2.5) This is different from other languages (e.g. Java), where the decimal part is just truncated
Modulus Like other languages, the operator % returns the remainder (modulus) of an integer division >>> 10%4 2 with negative numbers, the behavior is different from other languages, such as C or Java >>> -10%4 2 (in Java or C it would be -2!!) The rules in Python are: (a//b)*b +(a%b) == a (a%b) has the same sign as b
Function round() You can also round a float to an integer, by using the round() function >>> round(10.3) 10 >>> round(10.7) 11 >>> round(-10.7) -11
Join of strings and numbers Unlike other languages, you cannot use the + operator to join a string and a number >>> print("my " + 4 + " friends") Traceback (most recent call last): File "<pyshell#21>", line 1, in <module> print("my " + 4 + " friends") TypeError: Can't convert 'int' object to str implicitly But you can use the str() "function" to convert a number into a string >>> print("my " + str(4) + " friends") My 4 friends
The str class name More in general, str is the class name for strings; str(x) creates a string representing x The parameter x can also be a string >>> str1 = str("hello") >>> str2 = str("world") >>> print(str1 + " " + str2) Hello world
Join of strings and numbers To insert numbers within a string you can also use the format() method, which we already introduced >>> print('my {} friends'.format(4)) My 4 friends
Octal and hexadecimal formats Use the %o, %x, %X formatting symbols to format a number as octal or hexadecimal >>> print("%o %x %X" % (30,30,30)) 36 1e 1E
Calculator in interactive mode In the interactive mode, the value of the last expression is assigned to the _ variable Such a variable should be used as a "read-only" variable >>> 6+10 16 >>> 10 - _ -6