Python Tutorials — Chapter 4 — Basic Operators

In chapter 4 of Python Tutorial, we will learn about Basic Operators

An operator is a symbol that performs a certain operation on two operands. Python, like any other programming languages, contains some operators, as listed below.

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Arithmetic Operators

In Python, there are many arithmetic operators such as + (addition), – (subtraction), * (multiplication), / (divide), % (modulo), ** (exponent) and // (floor division) operators.

# assigning values to two variables 
x = 15 
y = 5 

# Addition 
adding = x + y 

# Subtraction 
sub = x - y 

# Multiplication 
mult = x * y 

# Divide(float) 
divisn1 = x / y 

# Floor Division 
divisn2 = x // y 

# print results 

print(adding) 
print(sub) 
print(mult) 
print(divisn1) 
print(divisn2)

Modulo (%) operator returns the integer remainder of the division. Another operator (**) provides power relationship.

# Modulo
modu = x % y

# Exponent
pwr = x ** y

#print result
print(modu)
print(pwr)

Try to predict what the answers will be for above operators exercise. You can try in the IDE at the end of this page.

Arithmetic Operators with Strings

In Python, you can concatenating strings using the addition operator and also, multiple strings to form a string with a repeating sequence:

#adding two strings
addstr = "Hello" + " " + "World"
print(addstr)

#multiple strings
multiplestr = "str" * 10
print(multiplestr)


#Output for adding two strings
Hello World

#Output for multiple strings
strstrstrstrstrstrstrstrstrstr

As you can see the output above, after adding two strings we got the output as “Hello World”.

Regarding the multiplying of strings, we got the output as “strstrstrstrstrstrstrstrstrstr” since we multiplied “str” 10 times. Please try these exercises in IDE!

Arithmetic Operators with Lists

We can join two lists using the addition operators. Also, using multiplication operator, Python generates new list with a repeating sequence:

#adding lists
list1 = [1,2,3,4]
list2 = [5,6,7,8]

add_list = list1 + list2
print(add_list)

#multiple lists
multiple_list = [2,4,6,8] * 3



#output for add_list
[1,2,3,4,5,6,7,8]

#output for multiple_list
[2,4,6,8,2,4,6,8,2,4,6,8]

Moving ahead, in chapter 4 of Python tutorial, we will briefly learn about Assignment and Comparison operators. Remaining operators will be covered in upcoming tutorials!

Assignment Operator:

To assign value to variables, we use this operator. By now, you are familiar with assigning value to variables, right?

# Assigning value to variable
x = 50

# Another way of assigning value to variable
y = x
print(y)

# We can use addition and assigning at the same time as below
y += x
print(y)

# Similarly, subtract and assigning value
y -= x
print(y)

# Try multiplication operator and assign value together. Try these exercises in IDE below

Comparison Operator:

To compare variables, we use comparison operators such as greater than (>), less than (<), equal to (==), not equal to (!=), greater than and equal to(>=), less than and equal to(<=). After comparing the values of variables, it will give out an output whether the comparison is True or False.

#examples of comparison operators
x = 17
y = 59

# checking greater than operator. Output will be True or False
print(x > y)

# checking less than operator. Output will be True or False
print(x < y)

#checking equal to operator. Output will be True or False
print(x == y)

# checking not equal to operator. Output will be True or False
print(x != y)

# checking greater than/equal to operator. Output will be True or False
print(x >= y)

# checking less than/equal to operator. Output will be True or False
print(x <= y)


#Output for checking greater than operator
False

#Output for checking less than operator
True

#Output for checking equal to operator
False

#Output for checking not equal to operator
True

#Output for checking greater than/equal to operator
False

#Output for checking less than/equal to operator
False

In the above example, we have taken variables ‘x’ and ‘y’ and assigned the values 17 and 59. After this, we simply compare these two values such as greater than, less than, either they are equal or not, etc..,

IDE:

DataCamp Light | Standalone example

That brings to the end of chapter 4 of Python tutorial, see you in the next tutorial!

By the way, to configure/install Python and Pycharm IDE on Windows OS, please visit below links to learn on how to install/configure Python and Pycharm IDE on Windows OS.

How to Install Python 3 and Pycharm IDE on Windows

How to configure Pycharm IDE and “Run” your first Python 3 program

For your reference, to download Pycharm IDE, visit below link:

https://www.jetbrains.com/pycharm/download/?section=windows

If you have any questions or comments about chapter 4 of Python tutorial, please let us know in the comments section below.

Leave a Reply