Python Basics Tutorial 3 – Python Math Expression

Notes from the video Python Course Singapore | Learn Python Programming |Python Tutorial 3 – Python Math Expression’:

 

The programming term for calculation is math expression

The common operators in math expression are:

  • addition, which is represented by the plus sign (+),
  • subtraction, which is represented by the minus sign (-),
  • multiplication which is represented by the asterisks sign (*),
  • division which is represented by the forward slash (/) and
  • exponentiation which is represented by two asterisks signs (**)

 

Now, let’s try out a few examples.

Let’s try printing 2 times 5. This displays 10 on the screen.

 

Next, let’s try printing 50 divided by 2. This displays 25 on the screen.

Now, let’s try assigning math expression to variables instead.

randomNumber = 10 – 5
anotherRandomNumber = 5**2
sum = randomNumber + anotherRandomNumber

 

In the statements above, the variable randomNumber is assigned the result of 10 minus 5 which is 5 while the variable anotherRandomNumber is assigned the result of 5 to the power of 2 which is 25.

The variable sum is the sum of the two variables randomNumber and anotherRandomNumber which is 5 plus 25 and it should give us 30.

Let’s see if it’s correct by printing the variable sum.

And yes, we got 30!

Get FREE LESSONS and TIPS on how YOU can excel at programming and business

Awesome! Thank you for subscribing!