LLP109 – Digital Application Development

Dr. Hyun Lim
[email protected]
Institute for Digital Technologies
Loughborough University London
Introduction to
LLP109 – Digital Application Development
Disclaimers
I wrote this lecture notes: Introduction to Python
while I was learning it myself. I have tried to run
all of the code examples included in the lecture
notes. However, these notes may contain trivial/
factual errors as the syntax slightly differs
according to the version of Python, e.g. Ver. 2.7
and 3.7.
Homework
“The only way to learn to code is to do it”:
You should type, run, and test codes for yourself.
3
Project-based Learning
Basic skills > Mini project (Coursework)
> Sophisticated skills > Extra knowledge
Proactively taking lectures, studying examples &
exercise in the lecture slides, and doing homework
mean you are already doing your coursework.
Books (ebook/hardcopy)
 Python programming : an introduction to computer science,
John M. Zelle
 Learning Python : learn to code like a professional with
Python, Fabrizio Romano
 Learning Python, Mark Lutz
Reading List
Some other books, including ebooks, are also available in the main/ London
Library. Have a search of the library catalogue: https://vufind.lboro.ac.uk/
5
Excellent Free e-Books
• Allen Downey, Jeff Elkner and Chris Meyers, Learning with
Python How to Think Like a Computer Scientist
https://greenteapress.com/wp/learning-with-python/
• C.H. Swaroop, A Byte of Python
https://python.swaroopch.com/
6
Various module-specific resources are available from the library
and online.
• Introduction
• Data Types/Structure
• Control flow
Contents
First Computer
Programmer
Ada Lovelace (1815-1852)
Lord Byron’s daughter, an
English mathematician and
writer, proposed mechanical
general-purpose computer,
the Analytical Engine. She was
the first to recognise that the
machine can be applications
beyond pure calculation, and
published the first algorithm.
– Wikipedia
8
9
“Monty Python’s Flying Circus”,
a BBC comedy series (1970s)
©Sebastian Niedlich
• C++ (1979)
• Python (1991)
• JAVA (1991)
has been published in 1991
Popular Programming Languages
10
© Codeeval 2016
Development Environments
(editors and compilers)
1. IDLE (default if you have installed Python)
Download and install Python 3 in your computer at
softwarecenter:SoftwareID=ScopeId_369856DC-9EA9-48E8-8227-
77CCDB1E4E48/Application_019d72ab-618f-42ea-a61b-
8b45e348d795
Or https://www.python.org/downloads/
2. Visual Studio
Install additional software modules including Python
Visual Studio installer > More/Modify > [V] Python
3. PyDev
4. PyCharm (Educational Edition: free)
Let’s agree that you do NOT ask me how to use your compiler tools after
today, especially during the lectures as it is not within the scope of the lectures. 11
Documentation, tutorials, beginner’s guide, discussion, Q&A
 http://python.org/
 http://wiki.python.org/moin/PythonBooks
 http://code.activestate.com/recipes/langs/python/
Useful Websites
12
Python †IDLE Shell
– Interactive mode (Interpreter prompt)
>>> print (“Hello World!”)
Hello World!
>>> 1+2*3
7
>>> name = “Hyun”
>>> name
Hyun
>>> print (“Hello”, name)
Hello Hyun
>>>
Just write your code at the prompt and press the Enter key
† IDLE (Integrated Development and Learning Environment) 13
Compiler vs Interactive mode
• The interactive mode in Python takes one statement
at a time and executes the instructions.
• The compiler converts high-level programming
language (of the whole program), i.e. Python, to lowlevel language at once, i.e. machine language that is
to execute the program.
14
IDLE Shell
• Interactive mode: to run only a single line of your code, use a
commend prompt window:
>>> 1+2
3
>>>
15
• Compiler mode (Source Code Encoding): to run a program including
multiple lines:
(at the top menu) File > New file > (write your code) >Save as >
xxxx.py
then, run the whole program by choosing Run > Run module (top
menu) or just pressing F5 key
Comments in Python

>>> print (“Hello World!”)
“””
# This is my first code in Python.

Triple quotes (“) will work just like a block comment, such as,
1+2*3
print “Hello again”
“””
• A comment starts with a #, then Python will ignore them.
• In-code documentation
• There is no block comment in Python. However, triple quotes
(“”” “””or ”’ ”’) will work just like a block comment.
• In Visual Studio (only)
Comment selection: ctrl+k & ctrl+c, Uncomment: ctrl+k & ctrl+u
16
Newline, tab, backslash
\n : newline
\t : tab
\\ : \ (backslash)
>>> print (“Hello\t\tWorld!”, “\n\n\n”)
Hello World!
# 2 tabs (empty space)
# 3 new (empty) lines
>>>
17
Variables
>>>x = 4 # x is of type int
>>>y = “Sally” # y is of type str
>>>print(x)
# assign values to multiple variables in one line:
>>>f0,f1,f2=”Orange”,”Banana”,”Cherry”
>>>f0
‘Orange’
18
# A variable name cannot start with a number, e.g. 1x, 2_item
# By the way, this is a comment
# A computer does not recognise whatever you write after #
Variable Types – Numbers

>>>x=1
>>>y=2.8
# int
# float

>>>z=1+3j # complex
# Type Casting

>>>a=float(x)
>>>b=int(y)
#convert from int to float:
#convert from float to int:
>>>c=complex(x) #convert from int to complex:
>>>d=str(y)
>>>d+2
# d is a string ‘2.8’. d is not a number
# d is not a number. ‘2.8’+2 is not possible

TypeError: cannot concatenate ‘str’ and ‘int’ objects
19
Last Session
• Course overview, including aims, objectives, ILO,
assessment (coursework), off-/online resources
• Python development environments: IDLE, VS, PyCham
• Have created your first Python code: “Hello World!”
• Variables: str, int, flot
• # Comment
• Introduction
• Data Types/Structure
• Control flow
Contents
Data Structures – String, Tuple, List
immutable mutable
Composite data type
(Character/text) String
>>> abc=”abcdefgi” # It is a string. ‘ ‘ is also fine, instead of ” “
>>> abc[0] # [ ] calls elements of a string
‘a’
>>> abc[1]
‘b’

>>> abc[-1]
‘i’
>>> abc[1:5]
‘bcde’
>>> abc[5:-1]
‘fg’
?
?
# A[-1] returns the last item
# A[n:m] returns items from n to m-1
# i.e. from n (inclusive) to m (exclusive)

>>> abc[1] =’k’ # immutable
TypeError: ‘str’ object does not support item assignment
? ? ?
22
(index)
How to update part of a string array
>>> my_email = “[email protected]
>>> my_email = “jim” + my_email[5:]
>>> my_email
[email protected]
# [n:] means all the items from the element n.
>>> “%s lives in %s at latitude %f” % (“John”, “London”, 51.50)
‘John lives in London at latitude 51.500000’
# [email protected]
23
>>> my_email = “john” + my_email[- 12:]
>>> my_email
[email protected]
>>> my_email[:5] # ?
Quiz ?
-1
-?
% : a print format string operator
-2
Tuple –
a fixed length, immutable sequence

>>> a_tp = (1, 2, 3)
>>> a_tp[0]
# or just, a_tp = 1,2,3

1
>>> a_tp[1:]
(2,3)
>>> a_tp[0] = 5 # immutable
Traceback (most recent call last):
File “<pyshell#189>”, line 1, in <module>
a_tp[0] =5
TypeError: ‘tuple’ object does not support item assignment
>>> a_str = ‘12345’ # a string
>>> b_tp = tuple(a_str) # type casting a string to a tuple
(‘1’, ‘2’, ‘3’, ‘4’, ‘5’)
?
?
24
Tuple – Complex data type
names_tp = ( ‘Tom’, ‘James’, ‘Chen’)
# or just, names_tp = ‘Tom’, ‘James’, ‘Chen’
b_tp = (‘1’, ‘2’, ‘3’, ‘4’, ‘5’)
c_tp = (1, [2, 6, 10], 3, ‘name’)
# [ ] is a List – another complex data type

>>> b_tp + c_tp # + operator, no –
(‘1’, ‘2’, ‘3’, ‘4’, ‘5’, 1, [2, 6, 10], 3, ‘name’)
>>> b_tp*2 # * operator, no /

(‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘1?’, ‘2’, ‘3’, ‘4’, ‘5’ )
25
Tuples, Lists in a tuple

>>> a1_tp = ((1, 2), 3, (4, 5, 6))
>>> a1_tp[2]
(4, 5, 6)
>>> c_tp = (1, [2, 6], 3, ‘name’)
# Tuples () in a tuple ()
# A List [] in a tuple ()

>>> c_tp[1]
[2, 6]
? ?
26
List
A compound data type:
[0]
[1.2, 3.4]
[7, “Hi”, “there”, 99]
[]
List – Use [ ] to call items
>>> names = [“James”, “Chen”, “Kim”, “Sergey”]
>>> names
>>> names[0]
‘James’
>>> names[1]
‘Chen’
>>> names[5]
Traceback (most recent call last):
IndexError: list index out of range
>>> names[-1]
‘Sergey’
>>> names[-2]
‘Kim’
>>> names[-4]
‘James’
 Out of range values raise
an exception
 Negative values go
backwards from the last
element.
?
28
a_lst = [(‘Tatiana’, ‘book I’), (‘James’, ‘book II’), (‘Sergey’, ‘book III’)]
# It is a kind of 2-D array
# If we want to print the details of a_lst[0] and a_lst[1]
print(‘Author:’,a_lst[0][0])
print(‘Book title:’,a_lst[0][1],’\n’)
print(‘Author:’,a_lst[1][0])
print(‘Book title:’,a_lst[1][1],’\n’)
29
Author: Tatiana
Book title: book I
Author: James
Book title: book II
Lists/ Tuples in a List
(Nested List/ Tuple)
[ atiana’, ‘book I’], [‘James’, ‘book II’], [‘Sergey’, ‘book III’]]

012
0 1

Length of a List
>>> names = [“James”, “Chen”, “Kim”, “Sergey”]
# Use len() to get the length of a list
>>> len(names) # length
4
>>> names[1]
Chen?
30
List- Other useful methods
>>> ids = [“p0”, “p1”, “p2”, “p3”]
>>> ids.append(“p4”)
# to append the whole object, e.g. a string, tuple, list, to the list
>>> ids
[‘p0’, ‘p1’, ‘p2’, ‘p3’, ‘p4’]
>>> id1=[“abc”, “def”]
>>> ids.extend(id1)
# to extend the list by adding all elements of an object to the end.
>>> ids
[‘p0’, ‘p1’, ‘p2’, ‘p3’, ‘p4’, ‘abc’, ‘def’]

>>> ids.append(id1)
[‘p0’, ‘p1’, ‘p2’, ‘p3’, ‘p4’,[‘abc’, ‘def’] ]
# .extend vs .append

>>> ids[5]
[‘abc’, ‘def’]
31
Exercise
bk1=’Programming I’
bk2=’Programming II’
bk3=’Programming III’
# bk1=’Programming I’; bk2=’Programming II’; bk3=’Programming III’
a_lst=[bk1]
a_lst.append(bk2) # cf. a_lst.extend(bk2)
a_lst.append(bk3)
print(a_lst[1])
>>>’Programming II’
>>>a_lst
[‘Programming I’, ‘Programming II’, ‘Programming III’]
32
Exercise
bk1=[‘Tatiana’, ‘book I’, 1969]
bk2=[‘John’, ‘book II’, 1988]
bk3=[‘Ange’,’book III’, 2019]
a_lst=[ ]
a_lst.append(bk1)
a_lst.append(bk2)
a_lst.append(bk3)
print(a_lst[1])
>>>a_lst
[[‘Tatiana’, ‘book I’, 1969], [‘John’, ‘book II’, 1988], [‘Ange’, ‘book III’, 2019]]
# nested lists (lists in a list)
33
List- Other useful methods

>>>del ids[0]
>>>ids
# The keyword (commend) del deletes an element
[‘p1’, ‘p2’, ‘p3’, ‘p4’,[‘abc’, ‘def’] ]
>>>ids.sort() # sort by default order, i.e. 1,2,3,a,b,c
# (from the smallest to the largest)
# .sort is a method
>>>ids
[[‘abc’, ‘def’], ‘p1’, ‘p2’, ‘p3’, ‘p4’]
>>>ids.reverse() # sort by reverse order

[‘p4’, ‘p3’, ‘p2’, ‘p1’, [‘abc’, ‘def’]]
>>>ids.reverse()
[[‘abc’, ‘def’], ‘p1’, ‘p2’, ‘p3’, ‘p4’]
>>>ids.insert(2, “p1.5”) # insert an object into a specific position
[[‘abc’, ‘def’], ‘p1’, ‘p1.5’, ‘p2’, ‘p3’, ‘p4’]
>>>a_lst.insert(1,bk3)
>>>a_lst
* string and tuple also have their own methods. 34
Summary – String, Tuple, List
• String : character (text) string, immutable
 a_str = ‘abcde’
 a_str = ‘12345’
• Tuple : a fixed length composite data type, immutable
 a_tp = (1,2,3,4,5)
 a_tp = 1,2,3,4,5
 a_tp = tuple(a_str) # type casting a string to a tuple
 a_tp[1]=4 # illegal
• List : composite data type, mutable
 a_lst = [1,2,’a’,’b’,’3pf’,’@email.com’]
 a_lst = list(a_tp) # type casting a tuple to a list
 a_lst[1]=4 # legal, Okay
35
 a_str[1]=8 # illegal
 a_tp= 1,2,’a’,’b’,’3pf’,’@email.com’
• Assignment
 string: ” “
 tuple: ( ), tuple(), or no bracket
 list: [ ]
• Calling a data element from a string/ tuple/ list
 data_structure_name[element_number]
36
Summary – Use of ” “, ( ), [ ]
37
S

Leave a Reply

Your email address will not be published. Required fields are marked *