0% found this document useful (0 votes)
4 views21 pages

Lu 03

Uploaded by

Spectre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Lu 03

Uploaded by

Spectre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CHAPTER 3

Numbers
Python’s simplest datatypes

• Booleans – can either be True or False

• Integers – whole numbers eg. 34, 54, 23455, 234

• Floats – numbers which contains/have decimals eg. 6.234, 3.422, 67.1


Booleans
• The special Python function bool() can convert any Python data type to
a boolean.
• Functions have a name, zero or more comma-separated input
arguments surrounded by parentheses, and zero or more return values.
We’ll look into functions more in Chapter 9.
• The bool() function takes any value as its argument and returns the
boolean equivalent.
Integers

• Whole Numbers
Integers
Integers

• Underscores can be used as digit separators


•Integer Operations
Let’s see how we can
use Python for normal
arithmetic.
• Math operators in
Python →
Integer Operations
Integer Operations
Integers & Variables
• All of the preceding examples used literal integers.You can mix
literal integers and variables that have been assigned integer
values:
Integers & Variables
Integers & Variables
Precedence
• In most languages, certain operators have a higher precedence
than others.
• Rules for precedence would more or less be the following:
1. Parentheses ()
2. Multiplication/Division */
3. Addition/Subtraction +-
• In Appendix E in your textbook you will find a detailed list for the
order of precedence in Python
Precedence
Bases

• Integers are assumed to be decimal (base 10)unless you use a


prefix to specify another base.
• With base 10 we count 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• In base 2 (binary), the only digits are 0 and 1
Bases
Type conversions
• To change other Python data types to an integer, use the int()
function.
• The int() function takes one input argument and returns one
value, the “integer-ize”d equivalent of the input argument. This
will keep the whole number and discard any fractional part.
Type conversions
How
• big
In Python issize
2,the an int?
of an int could be limited to32 or 64 bits, depending on
your CPU; 32 bits can store any integer from –2,147,483,648 to
2,147,483,647.
• A long had 64 bits, allowing values from–9,223,372,036,854,775,808 to
9,223,372,036,854,775,807. In Python 3, the long type is long gone, and an
int can be any size—even greater than 64 bits. You can play with big
numbers like a googol(one followed by a hundred zeroes).
• A googolplex is 10**googol(a thousand zeroes, if you want to try it
yourself).This was a suggested name for Google before they decided on
googol, but didn’t check its spelling before registering the domain name
google.com.
• This can cause integer overflow, where the computer does not provide the
adequate space that the number needs. But Python can handle large
numbers with no issue👍.
Floats
• Floats are numbers that have decimal points. Eg. 4.56, 3.2, 7.78
• Floats can include a decimal integer exponent after the letter e:

• Floats are handled similarly to integers: you can use the


operators(+, –, *, /, //, **, and %) and the divmod() function.
Floats

You might also like