Tutorial 1 - Mini intro to Python

Hello guys!

Please note that this material is a very basic introduction to the Python language. Only a few concepts necessary for our classes are demonstrated, considering that you have knowledge of programming logic.

I strongly suggest that you use the Google Colab environment for the discipline. There are those who love and there are those who hate the use of Notebooks (Colab, Jupyter) and its use is certainly inappropriate in many cases, but it will make it much easier during the semester. The Colab environment eliminates the need for local installation, comes with almost all the necessary libraries, is in Python 3 and facilitates code sharing.

Note before starting: code identation is mandatory in Python!


Types of data

Numbers

1 + 1
2
2.5 + 2.3
4.8
2**3
8

Strings

"Double quotation marks"
'Double quotation marks'
'Single quotation marks'
'Single quotation marks'

Lists

Lists are mutable sequences of objects. They are similar to the arrays/vectors of other languages.

Some manipulations with lists are demonstrated at the end of this material.

[1,2,3]
[1, 2, 3]
["Python", "Java"]
['Python', 'Java']
["Python", 5, 6.2, [2,"Java"]]
['Python', 5, 6.2, [2, 'Java']]

Assignment of variables

The Python language works with dynamic typing, requiring no type declaration when creating a variable. Note, however, that the variables have well-defined types!

a = 5

type(a)
int
a = "Biomedical Informatics"

type(a)
str
a = ["My", "list", "of", "words"]

type(a)
list

Print

name = "Darcy Nusite"

print(f"Hi {name}, how are you?")

print("Hi " + name + ", how are you?")

print("Hi", name, ", how are you?")
Hi Darcy Nusite, how are you?
Hi Darcy Nusite, how are you?
Hi Darcy Nusite , how are you?

Comparison operators

1 < 2
True
2 > 4
False
2 == 2
True
[0, 3] == [0, 2]
False
2 != 3
True
2 <= 2
True
2 >= 5
False

Logical operators

(1 > 2) and (2 < 3)
False
(1 > 2) or (2 < 3)
True

Conditional operators

fav_language = "Python"

if fav_language == "Python":
    print("Good!")
Good!
fav_language = "R"

if fav_language == "Python":
    print("Good!")

else:
    print("Too bad!")
Too bad!
fav_language = "R"

if fav_language == "Python":
    print("Good!")

elif fav_language == "R":
    print("Nice")

else:
    print("Too bad!")
Nice

Loops

for

courses = ["Biomedical Informatics", "Chemistry", "Biomedical Sciences"]

for course in courses:
    print(curso)
Biomedical Informatics
Chemistry
Biomedical Sciences
for number in range(0, 10):
    print(number)
0
1
2
3
4
5
6
7
8
9

while

i = 0

while i < 5:
    print(i)
    i = i+1
0
1
2
3
4

Functions

def my_sum_function(a, b):
    return a + b

my_sum_function(2,3)
5

More about lists

Indexing

patients = ["John", "Mary", "Jaspion"]

patients[0]
'John'
patients[-1]
'Jaspion'
patients[0:2]
['John', 'Mary']
list_of_lists = [5,7,9,[11,13]]
list_of_lists[3][0]
11

Attribution

patients[0] = "Cristiano Ronaldo"
patients
['Cristiano Ronaldo', 'Mary', 'Jaspion']

Add new element

patients.append("Messi")
patients
['Cristiano Ronaldo', 'Mary', 'Jaspion', 'Messi']

Length

our_class = ["Harry", "Rony", "Hermione"]
len(our_class)
3