In this blog, we will learn about numbers in Python and how to use them.
Types of Numbers:
- Python has various "types" of numbers (numeric literals). We'll mainly focus on integers and floating point numbers.
- Integers are just whole numbers, positive or negative. For example: 2 and -2 are examples of integers.
- Floating point numbers in Python are notable because they have a decimal point in them, or use an exponential (e) to define the number. For example 2.0 and -2.1 are examples of floating point numbers. 4E2 (4 times 10 to the power of 2) is also an example of a floating point number in Python.
- we can do basic arithmetic function in python. See the below examples.
2+1
o/p - 3
# Subtraction
o/p - 3
# Subtraction
2-1
o/p - 1
# Multiplication
o/p - 1
# Multiplication
2*2
o/p - 4
# Division
o/p - 4
# Division
3/2
o/p - 1.5
Note: Python 2 treats numbers that you type without any digits after the decimal point as integers, which can lead to some unexpected results during division. For example, if you type the expression 3 / 2 in Python 2 code, the result of the evaluation will be 1, not 1.5 as you might expect. This is because Python 2 assumes that you want the result of your division to be an integer, so it rounds the calculation down to the nearest whole number. In order to get the result 1.5, you would have to write 3.0 / 2.0 to tell Python that you want it to return a float, that is, to include digits after the decimal point in the result. Python 3 evaluates 3 / 2 as 1.5 by default, which is more intuitive for new programmers.
# Floor Division
o/p - 1.5
Note: Python 2 treats numbers that you type without any digits after the decimal point as integers, which can lead to some unexpected results during division. For example, if you type the expression 3 / 2 in Python 2 code, the result of the evaluation will be 1, not 1.5 as you might expect. This is because Python 2 assumes that you want the result of your division to be an integer, so it rounds the calculation down to the nearest whole number. In order to get the result 1.5, you would have to write 3.0 / 2.0 to tell Python that you want it to return a float, that is, to include digits after the decimal point in the result. Python 3 evaluates 3 / 2 as 1.5 by default, which is more intuitive for new programmers.
# Floor Division
7//4
o/p - 1
Note: Floor division returns quotient of the result, in which the digits after the decimal point are removed
# Modulo
o/p - 1
Note: Floor division returns quotient of the result, in which the digits after the decimal point are removed
# Modulo
7%4
o/p - 3
# Powers
o/p - 3
# Powers
2**3
o/p - 8
Python allows you to write over assigned variable names. We can also use the variables themselves when doing the reassignment. Here is an example of what I mean:
# Let's create an object called "a" and assign it the number 5
o/p - 8
Python allows you to write over assigned variable names. We can also use the variables themselves when doing the reassignment. Here is an example of what I mean:
# Let's create an object called "a" and assign it the number 5
a = 5
# Adding the objects
# Adding the objects
a+a
o/p - 10
# Reassignment
o/p - 10
# Reassignment
a = 10
# Check
# Check
a
o/p - 10
o/p - 10
Comments
Post a Comment