IC208 - Programming for Finance Seminar 1
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
IC208 - Programming for Finance
Seminar 1 - Solutions
1) Consider simple codes as follows:
Identify variables, classes, objects in each line of the codes.
Answer:
Line 1: m is a variable, an object, an instance of the integer class. 5 is the value/data we assign to m
Line 2: type of the object m
Line 3: an attribute of the object m
Line 4: a method applying on the object m
Line 5: l is a variable, an object, an instance of the list class.
2) Which of the following cannot be a variable?
a) _init_
b) in
c) it
d) on
Answer: b – in is a python keyword
3) Which of the following is an invalid statement?
a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000,000
Answer: b - Spaces are not allowed in variable names
4) Which of the following is true?
a) Variable names in Python is unlimited length
b) all names in Python must have leading and trailing underscores
c) underscore and ampersand are the only two special characters allowed in variable names
d) none of the above
Answer: a --‐ Variable names can be of any length
5) Which of the following is an invalid variable?
a) my_string_ 1
b) 1st_string
c) zzz
d) _abc
Answer: b --‐ A variable name has to be in accordance with the following three rules:
1) It can be only one word
2) It can use only letters, numbers, and the underscore (_) character
3) It can’t begin with a number
6) Use the len method to print the length of the string in the following codes.
Answer: print(len(x))
7) Get the characters from position 2 to 4 (llo) in the following codes
Answer: x=txt[2:5]
8) What is the output of thefollowing codes?
a) 3
b) 4
c) 5
d) 8
Answer: c --‐ to access to elements in a list (slice), we use index. It’s worth to note that Python index always starts from 0.
9) Fill in the white blank to change the value from "apple" to "kiwi" in the following codes
Answer: fruits[0] = “kiwi”
10) Use the append method to add "orange" to the fruits list
Answer: fruits.append(“orange”)
2023-03-06