Py02: Numbers
Computers process numbers and manipulate their value. Numbers are represented using binary values. Many programming languages differentiate numbers as integers and floating numbers.
Introduction
Numbers can be represented as integers, floats, and complex numbers. Numbers can be applied with mathematical operators like addition, subtraction, multiplication, division, and modulus.
Operation on Number
Arithmetic operations are performed on numbers. The operations are addition (+), subtraction (-), multiplication (*), division(/), and remainder(%).
left = 23
right = 34
print(left + right) # 57
print(left - right) # -11
print(left * right) # 782
print(left / right) # 0.6764705882352942
print(left % right) # 23
Type of Number
Integers are numbers that don't have decimal point values, such as:
- -23
- 0
- 30
- 1000
We can check the type of a value by using the type() function.
print(type(12))
Output
<class 'int'> >
Underscore can be used in large numbers to improve readability. The compiler simply ignores the underscores when it stores the number in the variable.
x = 1_000_230
y = 1000230
Variable x and y store the same value. The first line is easier to read compared to the second line
Float numbers with decimal points values, such as:
- -23.4
- 0.0
- 1.42123
- 1200.52
the following statement checks the type for the number 2.34. (the hash show preview of the output)
print(type(2.34))
Output
<class ‘float’> >
Casting
Using int() and float() to specify a type of variable is also known as casting. The int() constructor, creates an integer number from an integer literal, float literal where numbers after decimal will be removed, or string literal that only contains a valid number format. Another function bool() is used to cast an input to a boolean True or False.
print(int(12))
print(int(53.43))
print(int("-232"))
Output
12 53 -232 >
The float() constructor, creates a floating point number from a float literal, integer literal where the floating point will be added, or string literal that only contains a valid number format.
print(float(120))
print(float(-0.4))
print(float("32.12"))
Output
120.0 -0.4 32.12 >
The int() and float() can be used like variable declaration similar to other programming language.
int a = 4; // like in c, c++ or java
a = int(4) // in python
Input from User
As we already know the input()
function is used to get input from the user and it returns a string.
height = input("Enter height : ")
width = input("Enter width : ")
area = height * width
print(area)
This will cause the following error because we are trying to apply calculation on a string, even though we entered the correct inputs.
Output
Enter height : 3 Enter width : 5Traceback (most recent call last): File "<string>", line 3, in <module> TypeError: can't multiply sequence by non-int of type 'str'>
Convert String to Number
Any inputs from user that are used in calculations, need to convert from string to a number. To do this we can either use int() or float().
height = input("Enter height : ")
width = input("Enter width : ")
area = int(height) * int(width)
print(area)
Output
Enter height : 2 Enter width : 3 6 >
This example will convert string to integer value. If a float input is entered user another error will occur. This is because the int() function checks for the correct format of number before converting to integer.
Output
Enter height : 1.1 Enter width : 1.2Traceback (most recent call last): File "<string>", line 3, in <module> ValueError: invalid literal for int() with base 10: '1.1'>
As a solution, use the float() as it also can accept integer values.
height = input("Enter height : ")
width = input("Enter width : ")
area = float(height) * float(width)
print(area)
Output
Enter height : 1 Enter width : 2 2.0 >
A post by Cuber
Comments
Post a Comment