Wednesday, April 14

# Python Programming Tutorial for Beginners – Part 4 – Basic Operators

Operators are used to perform operations on or to check on variables and values. Think of them as doctors in programming that can either bring you into existence, give you a checkup or modify your identity. There are many types of operators but for this basic tutorial, we will focus on Assignment operators, Arithmetic operators and Comparison operators.

### Assignment operator

You have already used an assignment operator in the form of the equal sign so when we type x = 3 the operator is giving the variable named x the value of the integer number 3.

### Arithmetic operators

Next up we have arithmetic operators which are used to perform mathematical operations on variables and values. So think of addition, subtraction, multiplication and division just to start out.

so let’s try them out:

a = 5
b = 4
c = 5 + 4
c = a + b
d = c – 3
e = c * 4
f = e / 2

then add some print statements between each value to see the results on screen as we learned earlier

a = 5
print(a)
b = 4
print(b)
c = 5 + 4
print(c)
c = a + b
print(c)
d = c – 3
print(d)
e = c * 4
print(e)
f = e / 2
print(f)

Click run and take your time to go through the code line by line.

### Comparison operators

Next, we have comparison operators which are used to, you guessed it… compare two values. we have the equal to (==), don’t mix it up with the assignment operator, not equal to (!=) which is an exclamation mark right next to an equal sign. Greater than (>), less than (<). Let’s play with these a little and let me explain why comparison operators can be so powerful

g = a==b

h = c > d

In the code above, we want to check if a is equal to b. From our variable assignments above we know that a is 5 and b is 4 so they are not equal which means its False. Which means a boolean value for false will be stored in the variable g. What about the variable h? Follow the code and think about what will be stored in the variable and what data type it will then let us print the result on the screen and use the type() function to see what data type g and h are

g = a==b
print(g)
print(type(g))
h = c > d
print(h)
print(type(h))

As you can see after running the program, booleans are stored. Comparison operators are powerful because they are simple and can be used to do many cool things when programming such as powering if statements, loops and much much more.

recap

We just dipped our feet a little into operators in Python – The assignment operator assigns a value to things. We looked at some of the simpler arithmetic operators allowing us to add, subtract, divide and multiple and finally we looked at comparison operators allowing us to check or compare two values or variables. The following two tabs change content below. #### Kelroy

Kelroy is an internet junkie, technology advocate and avid gamer who envisioned Geezam.com as a Caribbean based technology blog offering a more global view of technology topics. Reach out if you have a guest post or would like to write for us. 