Python Operators and variables Tutorial - Beginners Guide

(Lisa Mathew) #1

Python Operator

When it comes to scripting, math operators play the main role. In this tutorial, we will look into the main math symbols which you can use in python.

Following are the main arithmetic operators which can be used in python.

+ plus
- minus
/ slash
* asterisk
% percent
< less-than
> greater-than
= Assignment Operator
<= less-than-equal
>= greater-than-equal

Let’s look at few examples.

  print("Single Operation:", 3 + 5)

  print("Multiple Mixed Operations", 3 + 5 - 3 - 3 )

  print("True or False", 3 == 3)

  print("Find Modulo", 5 % 3)

Python Integer and Float division

For pyton integer division we use // and float division, we use /

For example,

print(4//3) would output 1 

print(4/3) would output 1.3333333...

Python variables

Variable( used to store values. You can assign a value to a varibale using the assignment operator.

Variable Assignation

string values are assigned using quotes.

For example,

name = "scripty"

Float and interger values are assigned directly without quotes.

For example,

count = 20
length = 5.2

Multiple variable Assignation

You can assign values to variables in a single statement as shown below.

x, y, z = 1, 5.4, "scripty"

Also, you can assign a single value to multiple variables as shown below.

x, y, z = 45

it assigns 45 to x, y and z.