Python || Simple Math Using Int & Float This page will display the use of int and float data types. Since python is a flexible programming language, variable data types do not need to be explicitly declared like in C/C++/Java, but they still exist within the grand scheme of things.

==== ADDING TWO NUMBERS TOGETHER ====

To add two numbers together, you will have to first declare your variables by doing something like this.

Notice in lines 6-8, I declared my variables, giving them a name. You can name your variables anything you want, with a rule of thumb as naming them something meaningful to your code (i.e avoid giving your variables arbitrary names like “x” or “y”). In line 16 the actual math process is taking place, storing the sum of “num1” and “num2” in a variable called “total.” I also initialized my variables to zero. You should always initialize your variables before you use them.

the resulting code should give you the following output:

Please enter the first number: 25
Please enter the second number: 1
The sum of 25 and 1 is: 26

==== SUBTRACTING TWO NUMBERS ====

Subtracting two ints works the same way as the above code, and we would only need to edit the above code in one place to achieve that. In line 16, replace the addition symbol with a subtraction sign, and you should have something like this

The above code should give you the following output

Please enter the first number: 25
Please enter the second number: 1
The difference between 25 and 1 is: 24

==== MULTIPLYING TWO NUMBERS ====

This can be achieved the same way as the 2 previous methods, simply by editing line 16, and replacing the designated math operator with the star symbol “*.”

The above code should give you the following output

Please enter the first number: 8
Please enter the second number: 24
The product of 8 and 24 is: 192

==== DIVIDING TWO NUMBERS TOGETHER ====

This one is a little different from the other three. Before we would use int variables to store our data. In division, when you divide numbers together, sometimes they end in decimals. Int data types can not store decimal data (try it yourself and see), so here is where we use the float data type.

The resulting code will basically be the same as the other previous three, only instead of our variables being of type int within the print statement, they will be of type float.

The above code should give the following output

Please enter the first number: 1
Please enter the second number: 25
The quotient of 1 and 25 is: 0.040000

==== MODULUS ====

If you wanted to capture the remainder of the quotient you calculated from the above code, you would use the modulus operator (%).

From the above code, you would only need to edit line 16, from division, to modulus.

The above code should give the following output

Please enter the first number: 1
Please enter the second number: 25
1 mod 25 is: 1