Python's built-in numeric object types include integers, floats and complex numbers.
Numbers in Python support the normal mathematical operations.
In this post you will have some code examples for integers and floats.
Integers
##Integers:
#Let's allocate the integer of 9 to variable x:
>>> x = 9
>>> print(x)
9
#Let's look at the object-type of variable x:
>>> print(type(x))
<class 'int'>
#Let's double check if variable x is an 'integer':
>>> print(isinstance(x, int))
True
#Arithmetic operations on integers:
#add 55 to 20:
>>> print(55 + 20)
75
#subtract 40 from 8:
>>> print(40 - 8)
32
#multiply 3 with 8:
>>> print(3 * 8)
24
#divide 21 to 7:
>>> print(21 / 7)
3.0
#Python uses two multiplication symbols to represent exponents
#get 5th power of 2:
>>> print(2 ** 5)
32
#take 6th power of 10:
>>> print(10 ** 6)
1000000
##use paranthesis to secure order of operations:
>>> print(2 + 5 * 5)
27
>>> print((2 + 3) * 5)
25
Floats
#Floats are the number with a decimal point:
>>> print(0.2 + 0.3)
0.5
>>> print(5 * 0.1)
0.5
>>> print(5 * 0.5)
2.5
#Lets divide 10 by 3 to have a floating number as a result:
>>> print(10 / 3)
3.3333333333333335
There are modules allocated on numeric objects that you can use:
#Import math module to see pi value:
>>> import math
>>> print(math.pi)
3.141592653589793
#take the square root of 64
>>> print(math.sqrt(64))
8.0
#Import random module to create some random numbers:
>>> import random
>>> print(random.random())
0.6964545132840457
#you can use the choice method to pick a number from a list of numbers:
>>> print(random.choice([3, 56, 8, 90]))
8
Below you can download the codes for this Python session:
You can follow along the video lecture here:
I will see you in another post!
One-Time
Monthly
Yearly
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
¤5.00
¤15.00
¤100.00
¤5.00
¤15.00
¤100.00
¤5.00
¤15.00
¤100.00
Or enter a custom amount
¤
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.