Basic operations in Python


We have written the first lines of code in Python, now let’s check what possibilities we have by working with text and numeric variables.

#String type

VName = John

and push debugging.


Well, something went wrong. Let’s read the details: name ‘John’ is not definied. What does it mean? In Python, similar to VB or other programming language we need to use “” for string expression.
#characters

VName = "John"
VSurname = "Rivera"
print(VName+" "+VSurname)

What I really appreciate, besides the easiest way of var declaration is that I don’t have to wirte each of them while it is suggested automatically in PyCharm:

For string values, tuples etc. we might use indexing to receive part of information, take a look at this examples:

#indexing
VNameSurname = VName+" "+VSurname
print(VNameSurname)

print(VNameSurname[0])
print(VNameSurname[0:4])
print(VNameSurname[:4])
print(VNameSurname[5:])
print(VNameSurname[-4])
print(VNameSurname[2:-4])

indexing begins with 0, if we want to obtain first letter/sign. We can find elements from right or left using “-“.

Time to introduce simple functions use for string values, mostly used by me as below. In opossite to exel functions, we use them at the end of value.


We cannot put expression into functions.
#functions
print(VNameSurname.lower())
print(VNameSurname.upper())
print(VNameSurname.find("n"))
print(VNameSurname.isnumeric())
print(VNameSurname.isdigit())
print(VNameSurname.replace("r","t"))
print(VNameSurname.split(" "))

We also have this opportunity to use functions suggestes in PyCharm or check the parameters of built-in methods

#Math ops

Let’s write down some numerical variables below:

a = 1
b = 3
c = 9

and print them with basic operations symbols:

print(a,b,c)
print(a+b)
print(c-b)
print(c/b)
print(b*c)
print(type(c/b))
print(type(b*c))

#output


now let’s add a reference to the built-in “math” library

import math
d = math.e
print(d)
print(round(d,2))
c = math.sqrt(9)
print(c)