Wikipedia:
"Computer programming is the process of building and designing an executable computer program for accomplishing a specific computing task"
Endless possibilities!
Old versions | Python 3 |
---|---|
Python 1.0 - January 1994 | Python 3.0 - December 3, 2008 |
Python 1.0 - January 1994 | Python 3.1 - June 27, 2009 |
Python 1.2 - April 10, 1995 | Python 3.2 - February 20, 2011 |
Python 1.3 - October 12, 1995 | Python 3.3 - September 29, 2012 |
Python 1.4 - October 25, 1996 | Python 3.4 - March 16, 2014 |
Python 1.5 - December 31, 1997 | Python 3.5 - September 13, 2015 |
Python 1.6 - September 5, 2000 | Python 3.6 - December 23, 2016 |
Python 2.0 - October 16, 2000 | Python 3.7 - June 27, 2018 |
Python 2.1 - April 17, 2001 | Python 3.8 - October 14, 2019 |
Python 2.2 - December 21, 2001 | Python 3.9 - October 5, 2020 |
Python 2.3 - July 29, 2003 | Python 3.10 - October 4, 2021 |
Python 2.4 - November 30, 2004 | Python 3.11 - October 24 2022 |
Python 2.5 - September 19, 2006 | Python 3.12 - October 2 2023 |
Python 2.6 - October 1, 2008 | Python 3.13 - October 2024 |
Python 2.7 - July 3, 2010 |
At the end of the course, you should be able to:
At the end of the course, you should be able to:
How to seek help:
# A simple loop that adds 2 to a number
i = 0
while i < 10:
u = i + 2
print('u is' + str(u))
i += 1
u is2 u is3 u is4 u is5 u is6 u is7 u is8 u is9 u is10 u is11
'this is a string'
"this is also a string"
3 # here we can put a comment so we know that this is an integer
3.14 # this is a float
True # this is a boolean
type(True)
bool
[3, 5, 7, 4, 99] # this is a list of integers
('a', 'b', 'c', 'd') # this is a tuple of strings
{'a', 'b', 'c'} # this is a set of strings
{'a':3, 'b':5, 'c':7} # this is a dictionary with strings as keys and integers as values
type([3, 5, 7, 4, 99])
list
That depends on their type:
'a string'+' another string'
2 + 3.4
'a string ' * 3
'a string ' * 3.4
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [32], in <cell line: 4>() 2 2 + 3.4 3 'a string ' * 3 ----> 4 'a string ' * 3.4 TypeError: can't multiply sequence by non-int of type 'float'
Type Operations
int + - / ** % // ...
float + - / * % // ...
string +
Used to store values and to assign them a name.
Examples:
i = 0
counter = 5
snpname = 'rs2315487'
snplist = ['rs21354', 'rs214569']
width = 42
height = 20
snpname = 'rs56483 '
snplist = ['rs12345','rs458782']
snpname * 3
width * height
840
Allowed: Not allowed:
Var_name 2save
_total *important
aReallyLongName Special%
with_digit_2 With spaces
dkfsjdsklut (well, allowed, but NOT recommended)
NO special characters:
+ - * $ % ; : , ? ! { } ( ) < > “ ‘ | \ / @
→ Notebook Day_1_Exercise_1 (~30 minutes)
width = 5
height = 3.6
snps = ['rs123', 'rs5487']
snp = 'rs2546'
active = True
nums = [2,4,6,8,4,5,2]
int(height)
3
x = 4
y = 3
z = [2, 3, 6, 3, 9, 23]
pow(x, y)
64
x = 5
y = 3
y != x
True
x = 2
y = 3
x == 2 and y == 5
x = [2,4,7,3,5,9]
y = ['a','b','c']
2 in x
4 in x and 'd' in y
False
# A simple loop that adds 2 to a number and checks if the number is even
i = 0
even = [2,4,6,8,10]
while i < 10:
num = i + 2
print('num is '+str(num)+'. Is this number even? '+str(num in even))
i += 1
num is 2. Is this number even? True num is 3. Is this number even? False num is 4. Is this number even? True num is 5. Is this number even? False num is 6. Is this number even? True num is 7. Is this number even? False num is 8. Is this number even? True num is 9. Is this number even? False num is 10. Is this number even? True num is 11. Is this number even? False
# A simple loop that adds 2 to a number, check if number is even and below 5
i = 0
even = [2,4,6,8,10]
while i < 10:
num = i + 2
print('num is '+str(num)+'. Is this number even and below 5? '+\
str(num in even and num < 5))
i += 1
num is 2. Is this number even and below 5? True num is 3. Is this number even and below 5? False num is 4. Is this number even and below 5? True num is 5. Is this number even and below 5? False num is 6. Is this number even and below 5? False num is 7. Is this number even and below 5? False num is 8. Is this number even and below 5? False num is 9. Is this number even and below 5? False num is 10. Is this number even and below 5? False num is 11. Is this number even and below 5? False
x = 5
y = 7
z = 2
x == 5 and y < 7 or z > 1
# and binds stronger than or
x > 4 or y == 6 and z > 3
x > 4 or (y == 6 and z > 3)
(x > 4 or y == 6) and z > 3
False
# BEWARE!
x = 5
y = 8
#xx == 6 or xxx == 6 or x > 2
x > 42 and (xx > 1000 or y < 7)
False
Python does short-circuit evaluation of operators
Lists (and strings) are an ORDERED collection of elements where every element can be accessed through an index.
l = [2,3,4,5,3,7,5,9]
s = 'some longrandomstring'
#'o' in s
l[0]
s[4:7]
s[0:8:2]
s[-1]
l[0] = 42
s[0] = 'S'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [42], in <cell line: 10>() 8 s[-1] 9 l[0] = 42 ---> 10 s[0] = 'S' TypeError: 'str' object does not support item assignment
Mutable objects can be altered after creation, while immutable objects can't.
Immutable objects: Mutable objects:
int
• list
float
• set
bool
• dict
str
tuple
s = [0,1,2,3,4,5,6,7,8,9]
s.insert(5,10)
s.reverse()
s.append(10)
s
[9, 8, 7, 6, 5, 10, 4, 3, 2, 1, 0, 10]
→ Notebook Day_1_Exercise_2 (~30 minutes)
fruits = ['apple','pear','banana','orange', 'grapes', 'pears']
print(fruits[0])
print(fruits[1])
print(fruits[2])
print(fruits[3])
print(fruits[4])
print(fruits[5])
apple pear banana orange grapes pears
fruits = ['apple','pear','banana','orange', 'grapes']
for fruit in fruits:
print(fruit)
print('DONE!')
apple pear banana orange grapes DONE!
Always remember to INDENT your loops!
For
loop¶fruits = ['apple','pear','banana','orange']
mystring = 'mylongstring'
for fruit in fruits:
print(fruit)
apple pear banana orange
While
loop¶fruits = ['apple','pear','banana','orange']
i = 0
while i < len(fruits):
print(fruits[i])
i = i + 1
print(i)
apple pear banana orange 4
For
loop
Is a control flow statement that performs a fixed operation over a known amount of steps.
While
loop
Is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.
Which one to use?
For
loops better for simple iterations over lists and other iterable objects
While
loops are more flexible and can iterate an unspecified number of times
→ Notebook Day_1_Exercise_3 (~20 minutes)
if/else
statementsshopping_list = ['bread', 'egg', 'butter', 'milk']
if len(shopping_list) > 3:
print('Go shopping!')
Go shopping!
shopping_list = ['bread', 'egg', 'butter', 'milk']
tired = False
if len(shopping_list) > 5:
if not tired:
print('Go shopping!')
else:
print('Too tired, I\'ll do it later')
else:
if not tired:
print('Better get it over with today anyway')
else:
print('Nah! I\'ll do it tomorrow!')
Better get it over with today anyway
Any longer pieces of code that have been used and will be re-used SHOULD be saved
Two options:
./script.py
python script.py
fruits = ['apple','pear','banana','orange']
for fruit in fruits:
print(fruit)
apple pear banana orange
fh = open('../files/fruits.txt', 'r', encoding = 'utf-8')
for line in fh:
print(line)
fh.close()
apple pear banana orange
'string'.strip()
Removes whitespace
'string'.split()
Splits on whitespace into list
s = ' an example string to split with whitespace in end '
sw = s.strip()
sw
swl = sw.split()
swl = s.strip().split()
swl
['an', 'example', 'string', 'to', 'split', 'with', 'whitespace', 'in', 'end']
fh = open('../files/fruits.txt', 'r', encoding = 'utf-8')
for line in fh:
print(line.strip())
fh.close()
apple pear banana orange
How much money is spent on ICA?
fh = open("../files/bank_statement.txt", "r", encoding = "utf-8")
total = 0
for line in fh:
expenses = line.strip().split() # split line into list
store = expenses[0] # save what store
price = float(expenses[1]) # save the price
if store == 'ICA': # only count the price if store is ICA
total = total + price
fh.close()
print('Total amount spent on ICA is: '+str(total))
Total amount spent on ICA is: 1186.71
How much money is spent on ICA in September?
fh = open("../files/bank_statement_extended.txt", "r", encoding = "utf-8")
total = 0
for line in fh:
if not line.startswith('store'):
expenses = line.strip().split()
store = expenses[0]
year = expenses[1]
month = expenses[2]
day = expenses[3]
price = float(expenses[4])
if store == 'ICA' and month == '09': # store has to be ICA and month september
total = total + price
fh.close()
out = open("../files/bank_statement_results.txt", "w", encoding = "utf-8") # open a file for writing the results to
out.write('Total amount spent on ICA in september is: '+str(total))
out.close()
for file in os.scandir("../files/"):
print(time.ctime(os.stat(file).st_mtime), '\t', file.name)
Tue Oct 11 18:39:02 2022 250.imdb Thu May 20 17:46:00 2021 bank_statement.txt Thu May 20 17:46:00 2021 bank_statement_extended.txt Fri Oct 6 12:35:06 2023 bank_statement_results.txt Thu May 20 17:46:00 2021 blocket_listings_selected.txt Thu May 20 17:46:01 2021 cheat_sheet.pdf Thu May 20 17:46:01 2021 fruits.txt Thu May 20 17:46:01 2021 fruits_extended.txt Wed Oct 12 08:43:09 2022 imdb_reformatted.txt Fri Sep 30 15:40:44 2022 schedule.csv Thu May 20 17:46:01 2021 somerandomfile.txt
For
loops and While
loopsIf/Else
statement are used when deciding actions depending on a condition that evaluates to a booleanIf/Else
statements can be nestedopen()
can be used to read in text files→ Notebook Day_1_Exercise_4