3.Numbers and Arithmetic in Python

We can perform various operations in Python like addition, subtraction, Multiplication, Division, etc.

[python]
>>>>5+8
13
>>>6-4
2
>>>>5*5
25
2
>>>9%4
1
[/python]

what if we want to divide numbers which are not properly divisible, consider

[python]
>>>>18/7
2
[/python]

By default Python considers numbers as integer type. For evaluation of a number in float, we can do like this

[python]
>>>18.0/7
2.5714285714285716
>>>18/7.0
2.5714285714285716
>>>18.0/7.0
2.5714285714285716
[/python]

Now last expression which I’ll be considering is the exponent. When we want to evaluate 2 power 3 (2 cube), then

[python]
>>>2**3
8
[/python]

Thus, using ** we can evaluate exponent expression.

Now moving onto Variables..

→ Variables

→ It is a pretty much a place holder or a temporary storage. To initialize a variable,

syntax: Variable_name=Value

example: x=8

this will assign value 8 to x.

[python]
>>>x+8
16
>>>x*3
24
[/python]

Thus, we can directly use the variables in the command which has the value in it.

We can assign multiple variables also.

[python]
>>>y=6
>>>x+y
14
[/python]

What if one needs to enter a value given by the user, i.e. Dynamic initializing. One can do that by using input() function.

Example:

[python]
>>>x=input(“Enter the number:")
Enter the number:12
>>>x
12
[/python]

Thus, we have completed some of the basics of variables and operators.

Advertisement