# CS 1358 Introduction to Programming in Python SC2
###### tags: `pythonSC`
# 1. Definitions and Short Answers - functions
1. What is a comment in a program and what is its purpose?
> to help the other programer understand what is the code doing for.
2. What is an operator? Give some examples of arithmetic operators in Python.
> +-*/
3. What is a comparison operator? What are possible results of a comparison?
> == != < >, True False
4. What is a logical operator? What are possible results of a logical operation?
> and or not , True False
5. What is 20 in hexadecimal representation? in octal representation?
> 0x14, 0o24
6. Why does Python support two division operators? What is their difference?
>/, //
7. What is the difference between '12' and 12 in Python?
> '12' is string 12 is interger
8. What is the difference between x = 3 and x == 3 in Python?
> x = 3 is assign 3 to x
> x == 3 is check if x is equal to 3
9. Assuming the variable y has been assigned the integer value of 4, which of the following are legal in Python and what do they do? which are illegal in Python?
```python
y = 4
4 = y #illegal
y == 4
4 == y
'y' = y #illegal
'y' == '4'
'4' = y #illegal
```
10. Assume variable x has integer value 3, and variable y has integer value of 4. What is the result of the following operator expressions, if they are legal in Python? Which of the following are not legal?
```python
x * y #12
'x' * y #'xxxx'
x * 'y' #illegal
'x' * 'y' #illegal
x + y #7
'x' + 'y' #'xy'
'x' + y #illegal
x + 'y' #illegal
```
11. What is the data type of
```python
['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
```
list
12. if
```python
L = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
```
what are the values of the following expressions if they are legal Python? Which are illegal?
```python
L[3] #'Wed'
L[1:5] #[Mon','Tue','Wed','Thu']
L[5:1] #[]
L(2:3) #illegal
L[1,2,3] #illegal
L{3} #illegal
L[1-5] #'Wed'
L['3'] #illegal
```
13. Assume
```python
T = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')
```
which of the following are allowed in Python, and what are their output or effect? Which are not allowed, for what reasons?
```python
print(T[3]) #'Wed'
print(T(3)) #illegal
print(T{3}) #illegal
T[3] = 'WED' #illegal
T[3] == 'WED' #False
print(T[3:5]) #('Wed', 'Thu')
print(T[3, 5]) #illegal
print(T['3']) #illegal
```
14. Assume
```python
S = {'Sun','Mon','Tue','Wed','Thu','Fri','Sat'}
```
which of the following are allowed in Python, and what are its output or effect? Which are not allowed, for what reasons?
```python
print(S[3]) #illegal
print(S(3)) #illegal
print(S{3}) #illegal
S[3] = 'WED' #illegal
S[3] == 'WED' #illegal
print(S[3:5]) #illegal
print(S[3, 5]) #illegal
print(S['3']) #illegal
```
15. Assume
```python
D = {'Sun':0, 'Mon':1, 'Tue':2, 'Wed':3, 'Thu':4, 'Fri':5, 'Sat':6}
```
which of the following are legal in Python, and what are their values?
```python
D[3] #illegal
D['Thu'] #4
D[0:3] #illegal
D[2, 6] #illegal
D{'Sun'} D{'Sun'}
D(0) #illegal
D{3} #illegal
D('Sun') #illegal
```
16. What is the value of
```python
{ 2, 3, 4 } | { 3, 4, 5 }
```
```python
{2, 3, 4, 5}
```
17. What is the value of
```python
{ 2, 3, 4 } & { 3, 4, 5 }
```
```python
{4}
```
18. Suppose you have the following sequence of Python statements:
```python3
x = 3
y = 2
if x > y:
print("x is bigger than y")
elif x == y:
print("x and y are the same")
else:
print("x is smaller than y")
```
What is printed?
>x is bigger than y
19. What is wrong with the following code, which is supposed to compute the total of a list of numbers?
```python
L = [3, 2, 6, 5]
for i in L:
total = total + i #NameError: name 'total' is not defined
print(total)
```
How can it be fixed?
```python
L = [3, 2, 6, 5]
total = 0
for i in L:
total = total + i
print(total)
```
20. What is the difference between
```python
x = 0
while x < 100:
x = x + 1
```
and
```python
x = 0
if x < 100:
x = x + 1
```
> 第一個會去做迴圈的動作 所以最後的結果為100
> 第二個沒有迴圈 所以最後的結果為1
21. What is an example of a function in Python? How do you call a function? What is a parameter?
> print()
22. What is an example of calling a (built-in) function that returns a value?
a. does input() return a value?
> Yes
b. does print() return a value?
> return None if function do thing normal
c. what other built-in functions do you know that returns a value?
> max(), min().....
23. Python supports two kinds of loops. What are they?
> for, while
24. What is a suite? What is the pronunciation of "suite"?
> indented statement block inside a control construct
25. What does import math do? How do you call the cos function (cosine) defined in the math module in Python?
> import math module, math.cos()
26. To read a file, it is common to see fh = open('filename'). What kind of data is fh called? Give an example of using fh for accessing (e.g., reading or writing) a file.
> defult a Text file, fh = open('filename','r')
27. if s = 'hello', Python supports two styles of “calls” (or “invocation”):
len(s) is an example of a function call
s.upper() is another form of call. What kind of call is it?
> method
28. How are class and instance related to each other?
> instance is a object created from class
29. Why is it incorrect to split the statement
```python
f = a + b * 2 + c / 2 - 4 * d
```
onto two separate lines as the following
```python
f = a + b * 2 + c / 2
- 4 * d
```
How can it be fixed so Python will accept it?
```python
f = a + b * 2 + c / 2\
- 4 * d
```
30. If you want to swap the values of two variables x and y, why can't you just do
```python
x = y
y = x
```
Give two different ways you can swap their values correctly in Python.
```python
temp = x
x = y
y = temp
y, x = x, y
```
31. What is a keyword in Python? Give some example keywords in Python.
> words with reserved meaning in Python language
> if, for, def
32. Which of the following are legal and illegal identifiers in Python?
```
myname
my_name
_myname
MyName
myname_
my-name #illegal
my11name
myname11
11myName #illegal
my_11Name
_11myName
@myname #illegal
my@name #illegal
myname@ #illegal
in #illegal
out #illegal
_in
_out
IN
OUT
and #illegal
or #illegal
but
function
integer
number
class #illegal
instance #illegal
global #illegal
local #illegal
you+me #illegal
I_love_$$ #illegal
```
33. What is an example of a snake-case identifier? a camel-case identifier?
> camel-case : averageMidtermScore,
> snake-case : average_midterm_score,