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)

Say Hello to Python

As I mentioned partly in my last post, I am trying my skills working from a few months in new environment totally different then used to…

Python differs significantly from the Visual Basic or C++ language, although it also provides many opportunities to process input data and generate results in line with our expectations. Python’s strong value is its extensive number of libraries, with which we can implement solutions in the field of statistics, data analysis and artificial intelligence.

Today I will present first examples of code for beginners. As usual you have a choice with which intepreter you would like to work with, even in notepad. I have installed Python 3.6 and write my programs in PyCharm.

If you decided to work in PyCharm, first open might need a few minutes more for self-configure environment and its components. Before dev. editor installation check if you alredy have Python on you application list:

If so, go to PyCharm (IDE) and select New Project

In our Project path we need to insert python file

First of all, in opossite to Visual Basic, there’s no need to declare data type for our variables, however we can change it and modify whenever we need to. Let’s write few simple lines of code.

Name = "John"
Surname = "Smith"
Age = 35
Sex = "Man"

to check the result we will use a print declaration. Let’s write some simple math operations for our variables:

print(Name + " " + Surname, Age, Sex)

As you can see there’s two methodes: we might declare a variable to introduce mathematical operations or we might just use a print before the argument. Now I will add three lines to check type of my variables:

print(type(Name))
print(type(Age))

To see first results let’s click on the Run

We can click on Debug before Run, to check for errors
#indexing

A = Name + Surname
print (A)
print(A[0:2])
print(A[3:6])
print(A[:-6])
print(A[:6])
print(A[:-6])
print(A[2:-6])

OK, this is where we end the first lesson. Try by yourself.