Lab 1: Basic Python
Objective
To understand basic syntax of Python programming language.
Declare a variable
Python is strongly and dynamically typed.
Strongly typed means the type of a variable does not change unexpectedly. When a variable is defined as a string with only numerical digits, it stays string, it doesn’t become an integer or number.
Dynamically typed means the type of a variable can change when a value of a different type is assigned to the variable.
As Python is dynamically typed, we do not need to specify a type when we declare a variable. We just assign a value to the variable.
-
Define a variable named
val
and assign the value'a'
to the variable.val = 'a'
The type of the variable can be viewed in the variable explorer.
-
Continue from the previous code block, assign the value
1
to the same variable. The variable will now be of typeint
instead of typestr
.val = 1
Array manipulation
-
Array in Python can be declared using a set of square brackets (
[...]
). -
To assign a variable with an empty array,
arr = []
-
To define an array with a series of numbers,
arr = [1,2,3,4,5]
-
To define an array with a series of alphabets,
arr = ['a','b','c','d','e']
-
An array can also be defined with values of mixed types.
arr = [1,'a',2,'b',3,'c']
-
Python arrays (or more commonly known as lists) are zero indexed arrays; it means to access the first element in the array
arr
,# in IPython console arr[0] # gives the output of 1 arr[1] # gives the output of 'a'
-
Python arrays also support negative indexing; this means to get the last element in the array
arr
,# in IPython console arr[-1] # gives the output of 'c'
-
Colon (
:
) can be used to extract multiple elements from an array. Maximum two (2) colons can be used for indexing/slicing an array.arr[0:5:2]
The value before the first colon is the starting index, the value after the first colon is the ending index (exclusive), the value after the second colon is the number of steps.If the first value is empty, it is assumed as
0
.If the second value is empty, it is assumed as the length of the array, i.e. up till the last element in the array.
If the third value is empty, it is assumed as
1
.# in IPython console arr # [1, 'a', 2, 'b', 3, 'c'] arr[1:5] # ['a', 2, 'b', 3] arr[1:5:2] # ['a', 'b'] arr[:3] # [1, 'a', 2] arr[4:] # [3, 'c'] arr[2:-2] # [2, 'b'] arr[4:1] # [] arr[4:1:-1] # [3, 'b', 2] --> slice in the reverse order
Add an element to the end of an array
# in IPython console
arr.append(4)
arr
# [1, 'a', 2, 'b', 3, 'c', 4]
Add multiple elements to the end of an array
# in IPython console
arr.extend(['d',5,'e'])
arr
# [1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e']
Assign a value to a specific index
# in IPython console
arr[0] = 0
arr
# [0, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e']
Insert an element at a specific index
# in IPython console
arr.insert(1,1)
arr
# [0, 1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e']
Remove an element at a specific index
# in IPython console
del arr[0]
arr
# [1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e']
Combining arrays
zip
can be used to combine two or more arrays.
# in editor
joint_arr = list(zip([1,2,3], ['a','b','c']))
# in IPython console
joint_arr
# [(1,'a'),(2,'b'),(3,'c')]
Loops
-
Python supports
for
andwhile
loops. -
To loop through every element in the array
arr
and print them to the console,for a in arr: print(a) # 1 # a # 2 # ...
for a in arr
: loops through all elements inarr
and in each loop, an element inarr
is assigned to the variablea
. -
Note that in most other programming languages, code blocks are separated with delimiters such as the curly brackets (
{}
). This is not the case in Python. Code blocks in Python are defined by their indentation and normally initiated with a colon(:
).For example,
for a in arr: print(a) print(arr)
print(a)
is the command to be executed in each loop.print(arr)
is only executed after thefor
loop is completed.for a in arr: print(a) print(arr)
In this case,
print(arr)
is executed in each loop. -
Using
zip
we can loop through two arrays at once.for item in zip(['a','b','c','d'],['artificial','breadth','cost','depth']): print(item[0] + ' for ' + item[1])
-
enumerate
is useful in obtaining the index of the element in the array.for (index, item) in enumerate(['a','b','c','d']): print('Index of ' + item + ' is: ' + str(index))
-
Looping through a dictionary (
dict
) can also be done easily.x_dict = { 'd': 'depth', 'e': 'estimation', 'f': 'frontier' } for key,value in x_dict.items(): print(key + ' for ' + value)
-
while
loops can be defined similarly.x = 0 while x != 10: x += 1 print(x) print('while loop is completed')
-
The following example introduces nested array and multiple assignments.
arr = [['a',1],['b',2],['c',3],['d',4],['e',5],['f',6]] for [a,n] in arr: print(str(a) + ' is ' + str(n))
Conditions
-
The following operators can be used for conditional testing:
Operator Definition ==
Equivalence !=
Inequivalence <
Less than <=
Less than or equal to >
Greater than >=
Greater than or equal to -
Python also supports text operator for conditional testing:
Operator Definition Example (symbolic) Example (text) is
Equivalence a == 1
a is 1
not
Inequivalence a != 1
a is not 1
ornot (a is 1)
ornot a is 1
ornot(a == 1)
-
Combining two conditions can also be done with text operators
and
andor
.Symbolic operator Text operator &
and
|
or
User-defined functions
-
To define a custom function with the name of
custom_fcn
,def custom_fcn(): print('This is a custom function to display custom message')
-
To call the function,
custom_fcn() # This is a custom function to display custom message
User-defined functions with input arguments
-
To define a custom function with input arguments,
def custom_fcn(msg): print('This is a custom function to display ' + msg)
-
The function can be called by
custom_fcn('new message') # This is a custom function to display new message
User-defined functions with optional input arguments
-
To define a custom function with optional input arguments, we just need to provide the default value to the optional input arguments.
def custom_fcn(msg = 'default message'): print('This is a custom function to display ' + msg)
-
The input arguments can also be specified as named inputs.
custom_fcn(msg='new message') # This is a custom function to display new message
Exercise
-
Create a new file in Spyder. Define a variable named
friends
such that it is a nested array in which contains the name, home country, and home state/province of 10 of your friends (real or virtual). For example,friends = [["James", "Malaysia", "Malacca"], ["Goh", "Australia", "Brisbane"], ["Don", "Malaysia", "Pahang"]]
-
Create a function in the same file with three (3) optional input arguments,
name
,home_country
,home_state
.def filterFriend(name="", home_country="", home_state=""): ... return filtered
This function will filter
friends
based on the input arguments provided. The function will ignore the input argument if it has empty string, i.e.""
. If any of the input arguments is provided, the function will find the friends whose detail(s) matches the input.For example,
filterFriend(name="James")
will return[["James", "Malaysia", "Malacca"]]
filterFriend(home_country="Malaysia")
will return[["James", "Malaysia", "Malacca"], ["Don", "Malaysia", "Pahang"]]
.