Python is a high-level and interpreted programming language with a focus on readability. Python was released in 1991 by Guido van Rossum.
The first example for all programming languages, is the so-called "Hello world!" program. In that case the string "Hello world!" is printed. Let us have a look into the Python code:
print("Hello, World!")
Hello, World!
Here, we executed the hello world code using Jupyter notebooks. Another option is to run a file with the code
%%writefile helloworld.py
print("Hello, World!")
Overwriting helloworld.py
%%bash
python3 helloworld.py
Hello, World!
Most programming languages use parentheses, like curly ${}$ ones, to specify the scope and indentation is used only for readability. In Python there are no parentheses and indentation is very important.
if 5 > 2:
print("Five is greater than two!")
Five is greater than two!
Here, we use the if statement for conditional statements. Only if the condition 5 > 2 is evaluated as true all the indented code is executed.
In Python we do not have types, like int, double, or string as in other languages. An the type of the variable is determined by the assignment.
# Integer
x = 5
# Floating point value
y = 5.5
# String
name = "Medical physics"
For in-code documentation the hashtag # is used and this lien of code is not executed. Python also supports multi line comments
"""
"the meaning of life,
the universe, and everything
"""
x=42
The programmer can specify the variable type, by casting the right-hand side to the desired type.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Getting the type of a variable
print(type(x))
<class 'str'>
print(type(z))
<class 'float'>
We can define a string using 'hello' (single quotation) or "hello" (double quotation).
string = "Medical Physics"
print(string[0:7])
Medical
for s in string[8:len(string)]:
print(s)
P h y s i c s
5 + 5
10
2**8
256
10 % 4
2
9 // 4 #Floor division
2
x=4
x += 4
x
8
x /= 8
x
1.0
2 == 2
True
4 != 5
True
9 >= 34
False
x = 1
x < 5 and x < 10
True
x < 0 or x < 5
True
not x < -1
True
Lists are used to store multiple items in a single variable
list = ["Medical",'Physics']
list[0]
'Medical'
list[1]
'Physics'
len(list)
2
# Lists can have mixed data types
list = ["Medical",'Physics','MDEP',7098]
list[0] = ""
print(list)
['', 'Physics', 'MDEP', 7098]
list.insert(3,"Patrick")
print(list)
['', 'Physics', 'MDEP', 'Patrick', 7098]
list.remove("Patrick")
print(list)
['', 'Physics', 'MDEP', 7098]
list.pop(0)
print(list)
['Physics', 'MDEP', 7098]
list = [100, 50, 65, 82, 23]
list.sort()
print(list)
[23, 50, 65, 82, 100]
def compare(n):
return -n
list.sort(key = compare)
print(list)
[100, 82, 65, 50, 23]
For loops can be used to access a list element by element or loop over an range.
for l in list:
print(l)
100 82 65 50 23
for i in range(0,4):
print(i**2)
0 1 4 9
def pow(i):
return i**2
print(pow(2))
4
def names(*name):
for n in name:
print(n)
names("Raphael", "Donatello", "Michaelangelo", "Leonardo")
Raphael Donatello Michaelangelo Leonardo
Python provides the module import math with additional mathematical functions
import math
math.pi
3.141592653589793
math.sqrt(2)
1.4142135623730951
A complete list with all functions and methods is available here.
Python is an object oriented programming language. An object can contain variables and functions.
__init__ method is called each time, we generate a new objectself is used to access the internal class variables and functionsclass Person:
def __init__(self, name,age):
self.name = name
self.age = age
def birthday(self):
self.age +=1
student = Person("Mike",20)
print(student.name)
print(student.age)
student.birthday()
print(student.age)
Mike 20 21