Updated: Aug 18, 2020
Numbers in Python
Data types that store numeric values are called Numbers.
If you change the value of a number data type, this results in a newly allocated object. you can call numbers immutable.
We can simply create a number object by assigning some value to a variable. we can manipulate them as shown in the below example.
Note that variable names should be small letters
Should not be a Python keyword.
Cannot start with a number
No spaces allowed in the name. Use _ instead
Python is a dynamically typed language i.e. we can re-assign values to different data types.
Whereas in the case of other languages, a variable can only have a value of its declared type. What I mean here is if let's say a variable is of type string, then you can assign only str type values to it and it will give an error when a number is assigned.
But Python is flexible, you don't need to worry about this in Python.
Below is the example
x = 5 y = 6 print("The product of two number is " , x * y) print("The sum of two number is " , x * y) print("The division of two number is " , x / y) print("The minus of two number is " , x - y) print("The modulus of two number is " , x % y)
The product of two number is 30 The sum of two number is 30 The division of two number is 0.8333333333333334 The minus of two number is -1 The modulus of two number is 5
1. What's the difference between floating point and an integer? An integer has no decimals in it, a floating point number can display digits past the decimal point. 2. Why doesn't 0.1+0.2-0.3 equal 0.0 ? This has to do with floating point accuracy and computer's abilities to represent numbers in memory. For a full breakdown, check out: https://docs.python.org/2/tutorial/floatingpoint.html
Know the data type of a variable
As Python is dynamically typed, it may be helpful to know the variable datatype holding a value. This can be done by using the "type" keyword.
# To know the variable type value a = 1 b = 2.20 c = True d = "Hello There" print(type(a)) print(type(b)) print(type(c)) print(type(d))
<class 'int'> <class 'float'> <class 'bool'> <class 'str'>
We will learn about the string data type in the next article.