Python Programming For Beginners Lesson 4 - Operators

in #programming7 years ago (edited)

Introduction

Hello Everyone and welcome to the fourth lesson of my series about the Python language. I also have a series on the Java programming language which can be found here https://steemit.com/programming/@robertlyon/java-programming-for-beginners-course.

The third lesson of Python Programming for Beginners can be found here https://steemit.com/programming/@robertlyon/python-programming-for-beginners-lesson-user-input

In this lesson, I will be expanding on the knowledge we already have on variables and user input and moving on to how we can manipulate data using operators.

So let's dive straight in.

Operators

In programming, we use operators to deal with the manipulation of numbers, just like in mathematics. Below I have included a list of the commonly used operators in Python. You should be familiar with almost all of these operators from any basic Math class.

List of Operators

  • Parentheses - ()
  • Power of - x ** y
  • Multiplication - x * y
  • Division - x / y
  • Modulo - x % y
  • Addition - x + y
  • Subtraction - x - y

The operators above should be familiar to you and they work in the same way that operators do in math, the one that may seem a little different to you is the modulo % operator, what this operator does is return the remainder of a calculation, for example, 5 % 2 would give the output 1 because 2 goes into 5 twice and leaves a remainder of 1.

The order that I have introduced the operators in are their correct order of operations, the modulo is equal to the multiplication and division operators. If you follow the order of operations you are familiar with in maths you will be fine with these operators.

I am now going to introduce a program below which will allow you to create your own small calculator. In this program I will be using something called IF statements that I have not covered yet, I will explain these thoroughly in a future lesson but I will touch on them here. The point of this lesson is not to understand the logic of the entire program but to focus on how the operators work.

Have a look at the program below, i have also included the code for you to copy and run yourself inside of the python shell.



Figure 1 - Simple Python calculator code example


print("Welcome to your basic calculator program.\n")
print("In this program we will be focusing on how operators work.\n")
print("Please practice with all of the operators learned within the lesson.\n")

num1 = int(input("Please enter a number:\n"))
operator = input("please enter an operator:\n")
num2 = int(input("Please enter a second number:\n"))

if operator == "**":
    print(num1 ** num2)
elif operator == "*":
    print(num1 * num2)
elif operator == "/":
    print(num1 / num2)
elif operator == "%":
    print(num1 % num2)
elif operator == "+":
    print(num1 + num2)
elif operator == "-":
    print(num1 - num2)
else:
    print("Error")

As you can see in the code above there are a few new things going on that we have not discussed. I will start with the first three print statements. These are just a setup for the program and introduce the program, you should be familiar with the print statements. One thing to note though is the "\n" at the end of each sentence. In Python this "/n" allows us to tell the program to take a new line, this stops multiple print statements from appearing on the same line.

The next three lines in the program deal with user input you should be familiar with the basic syntax of the input function from the previous lesson but in this lesson, we are dealing with numbers as well as strings. As the input function only accepts strings as an input by default we have to change the string that is gained from the input to an integer. To do this we need to perform what is called an explicit conversion. This is where we specifically tell the Python interpreter that we want to change the type we have for another type. As you can see in the example above this is achieved by using the int() function in the line of code num1 = int(input("Please enter a number:\n")). This line of code evaluated from right to left. Meaning, get an input from the user, change that input to an integer value, store that integer in the variable num1. The same thing happens for num2 but the operator variable is supposed to contain a string.

The next part of the program contains the IF statements, as we have not covered these I am not going to linger on them too much so don't worry if you don't understand what the IF statement does.

The basics of an if statement are that they let you choose a certain path for your program to follow, for example,

if i am 18 or over
--- i can drink alcohol
else
--- i cannot drink alcohol

This relates to the code in the calculator example

if the value stored in operator is equal to "**"
---print out the value of num1 to the power of num2

The == operator means is equal to

The rest of the program is pretty self-explanatory.

Have a look at the python shell in the image and it will give you an idea of how to input the values into the program. Make sure that you try all of the operators(except the parentheses) that we have covered in the lesson. Try to modify the program so that you can chain more than 1 operator together.

Conclusion

In this lesson, I have covered the most commonly used mathematical operators used in Python programming. These operators will allow you to manipulate numbers effectively in your code. In the next lesson, I will be looking at conditional operators and we will be diving into more IF statements.

As always if there are any improvements you think I can make to this post then please leave a comment and I will consider adding it.

Thank you for reading and I hope that someone will get some use out of these tutorials.

Message to readers

Thanks for taking the time to read my post, if you are interested in Science, Technology or Computer Science then check out my blog, content is a little sparse at the moment but I am making an effort to provide good quality original content to the Steemit community.

Sort:  

Very nice content as always.

Thanks, appreciate it :)

This post received a 99% upvote from @morwhale team thanks to @robertlyon! For more information, click here! , TeamMorocco! .
STEEM Price : 3.188 $

This post has received a 3.03 % upvote from @buildawhale thanks to: @robertlyon. Send at least 1 SBD to @buildawhale with a post link in the memo field for a portion of the next vote.

To support our daily curation initiative, please vote on my owner, @themarkymark, as a Steem Witness

You got a 2.38% upvote from @upme requested by: @robertlyon.
Send at least 1.5 SBD to @upme with a post link in the memo field to receive upvote next round.
To support our activity, please vote for my master @suggeelson, as a STEEM Witness

Congratulations @robertlyon! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.21
TRX 0.25
JST 0.038
BTC 96989.50
ETH 3378.64
USDT 1.00
SBD 3.23