X



Home>Tutorial>

Python Tutorial Day 4: Working With Operators.

Isaiah Ozadhe Thursday, September 3, 2020 Python

Hi. Welcome back to Emasson247. This is day 4 of our python series and today, we will be talking about 'working with operators'.
day4

Introduction and recap

In day 3 of the tutorial, i mentioned that we will discuss operators in the subsequent part but before we go on, let me do a quick recap. In the first part: We intalled python, visual studio code, set up visual studio code for python development amongst other things. In the second part: We talked about working with variables - declaring them, assigning values to them, changing their values and a host of other things. In the third part: We learned how to work with user inputs and external files. Now let us go into today's business properly:

What Is An Operaor:

In computer programming, an operator is simply a predifined concept that perform certain operations on one or more variables or values(Usually refered to as operands) and return a result. An operand on the other hand is any quantity on which a certain operation is to be performed. Operators in python are generally divided into the following groups:
  • Arithmetic operators.
  • Assignment operators
  • Comparison operators.
  • Logical operators.
  • Identity operaors
  • Membership operators
  • Bitwise operators
Let us look at them and practice their usage one after the other:

Arithmetic operators.

As the name implies they perform arithmetic or mathematical operations on one or more numeric values and return a result. Open our tutorial folder in vscode if you have been following this series and create a new operators.py file as we look at arithmetic operations and their usage. Subtraction Subtracts the right operand from the left operand and return a result . e.g

first_number = 10
second_number = 5
third_number = 89
fourth_number = 44

print(first_number - second_number)
print (third_number - fourth_number)
run the code and observe the result of the operation. Multiplication Multiplies operators and return a result.

first_number = 10
second_number = 5
third_number = 89
fourth_number = 44

print(first_number * second_number)
print (third_number * fourth_number)
Addition
first_number = 10
second_number = 5
third_number = 89
fourth_number = 44

print(first_number + second_number)
print (third_number + fourth_number)

Division

first_number = 10
second_number = 5
third_number = 89
fourth_number = 44

print(first_number / second_number)
print (third_number / fourth_number)

Modulus The modulus is a special type of operator that divides the left operand with the right operand and returns the remainder.

first_number = 10
second_number = 5
third_number = 89
fourth_number = 44

print(first_number % second_number)
print (third_number % fourth_number)

Exponential Used for power. it raises the left operand to the power of the right operand.

first_number = 10
second_number = 5
third_number = 89
fourth_number = 44

print(first_number ** second_number)
print (third_number ** fourth_number)

Floor Division It divides the left operand with the right operand and returns an approximated results. All fractions in the result are thrown away.

first_number = 10
second_number = 5
third_number = 89
fourth_number = 44

print(first_number // second_number)
print (third_number // fourth_number)

Assignment operators

Their primary purpose is to assign values to variables. The most common ones include: = Assigns a cetain value to a variable. e.g
num1 = 6
num5 = 7
+= Adds a given value to the previous value. e.g
num1 = 6
num1 +=7
print(num1)
the statement in line 2 of the above code simply means num1 = previous value of num1 + 7. if you run the code, you'll realize that the 13 is printed as the new value of num1. -= Subtracts a given value from the previous value of a variable. e.g
num1 = 6
num1 /=7
print(num1)
the statement in line 2 of the above code simply means num1 = previous value of num1 - 7. if you run the code, you'll realize that the -1 is printed as the new value of num1. *= Multiplys a given value by the previous value of a variable. e.g
num1 = 6
num1 /=7
print(num1)
the statement in line 2 of the above code simply means num1 = previous value of num1 * 7. if you run the code, you'll realize that the 42 is printed as the new value of num1. /= Divides a variable by a given value and saves the result to the variable. e.g
num1 = 6
num1 /=7
print(num1)
the statement in line 2 of the above code simply means num1 = previous value of num1 + 7. if you run the code, you'll realize that the 13 is printed as the new value of num1.

Logical operators.

This are operators that combine conditional statements(The next part of the series will deal with conditional statements. Here are examples of logical operors and their uses: and Returns 'True' only if both statements are true.

a= 20
print(a > 10 and a > 15)

The above code will return true if you run it because the value of 'a' which is 20 is greater than both 10 and 15. it will return false if one or both numbers are greater than 20. or Returns 'True' as far as one statement is true.

a= 20
print(a > 10 or a > 476)

The above code will return true if you run it because the value of 'a' which is 20 is greater than both 10 - one of the two values given. it will return false only if both numbers are greater than 20. not Just does an inverse of the real situation.

a= 20
print(not(a > 10 and a > 476))

run it and observe.

Iddentity operators.

This operators compare two objects based on their type and other factors but not equality. below are the uses:
a= 20
b = "String"
print(a is b)
The above statement returns a false because both variables are different. A is a numeric variable while b is a string variable. The inverse version of the iddentity operator is 'is not'. This will be the end of our dicussion on operators for this level. Next time on this series, we'll move to 'Conditional Statements'. I remain Isaiah Ozadhe and i write regularly on programming in very simple terms. stay tuned.

| | Views(550)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services