Python Tutorial 3: Basic Operations

Python Tutorial 3: Basic Operation

1. Quick Math

Remember first learning about Maths in primary school? It is similar in Python.

Commonly used operators:

Symbol Meaning
+ Addition
Subtraction
* Multiplication
/ Division
** Exponential
% Modulus (returns the remainder)
In [3]:
a = 2
b = 2
In [12]:
sumofAB = a+b
print(sumofAB)
4
In [13]:
c = 1
minusC = sumofAB - c
print(minusC)
3
In [14]:
multiplyAB = a * b
print(multiplyAB)
4
In [15]:
divide = a / b
print(divide)
1.0
In [16]:
exponential = a ** b
print(exponential)
4
In [17]:
WhatIsTheReminder = b % a
print(WhatIsTheReminder)
0

2. String Operations

The ‘+’ operator behaves differently when it comes to strings. Instead of ‘addition’, we call this ‘concatenation’.

In [2]:
c = "2"
d = "3"
In [3]:
print(c + d)
23
In [6]:
x = "Hello"
y = "World!"
print(x + " " + y)
Hello World!

If we had concatenated x and y without adding the white space in between the following will be the result:

In [7]:
print(x + y)
Out[7]:
'HelloWorld!'

Concatenating strings with integers

We cannot concatenate strings with integers. To do so we need to convert the integer to a string, using the function str().

In [8]:
e = 23
print(c + d + str(e))
2323

Similarly, in order to add a string (which is a number) to an integer, we can also convert the string to an integer using int().

In [9]:
print(int(c) + int(d) + e)
28

Try it out!

Assign 100 to principal, 1.05 to rate and calculate the product. Then, concatenate the output with “At the end of the year, I would have $” and print the result.

In [10]:
principal = 100
rate = 1.05
print("At the end of the year, I would have $" + str(principal*rate))
At the end of the year, I would have $105.0

3. Logical operators

In the previous chapter we covered some of the commonly used logical operators, which will return either True or False.

Here are more logical operators.

Equal to? Not Equal to?

‘!=’ is the opposite of ‘==’. It means not equal to.

In [11]:
x = 6
print(x != 6)
False
In [12]:
print(x != 8)
True

Is something inside a string?

We can find out whether a substring is present in another string using the ‘in’ operator. While this may sound redundant for now, it is useful to find whether a certain element is present in a list. You will more about lists in the next chapter.

In [14]:
a = 'at'
b = 'bat'
c = 'bath'
In [15]:
print(a in b)
True
In [16]:
print(b in c)
True
In [17]:
print(c in a)
False

Putting ‘not’ in the boolean expression will return the opposite output.

In [18]:
print(a not in b)
False
In [19]:
print(b not in c)
False
In [20]:
print(c not in a)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s