# CS 1358 Introduction to Programming in Python SC4
###### tags: `pythonSC`
# 1. Definitions and Short Answers - functions
1. Given the command shown on the lecture slide
```shell
$ uniq mary.txt
```
What is
> the prompt : $
> the program name : uniq
> the command-line argument mary.txt
2. What does the uniq program do?
> filters out repeated lines in a file
3. What does the cat program do?
> "concatenate and print files"
4. What does the grep program do?
> print matched lines in the input file(s)
5. Is it possible that uniq and cat produce the same output? How?
> Yes, when there are no repeated lines in a file.
6. Given the command shown on the lecture slide
```shell
$ grep class myfile.py
```
What is the purpose of
> class : the keyword you want to find in myfile.py
> myfile.py : the file you want to find.
7. Given the command
```shell
$ cat *.py
```
What is the meaning of *.py?
> find all file which filename end with .py
8. What does the following command do?
```shell
$ python3 prog.py
```
> executing prog.py
9. What is a shbang in a Python program? Where is it placed inside a Python program?
> #!, on 1st line to indicate interpreter
10. What does the command do:
```shell
$ chmod +x prog.py
```
change the prog.py's mod
11. What is the value of
```python
len([3, 7, 2, 0, 8])
len(['hello', 'world', 'goodbye'])
len('admin')
```
```console
5
3
5
```
12. Suppose you run the command
```shell
$ python3 showargs.py hello world goodbye
```
Inside the showargs.py program, suppose you have
```python
import sys
```
What is the value of sys.argv?
```python
['showargs.py', 'hello', 'world', 'goodbye']
```
What is the value of len(sys.argv)?
```python
4
```
What is the value of sys.argv[1:]?
```python
['hello', 'world', 'goodbye']
```
13. If the command $ python3 showargs.py hello world is used to run the Python program, what is printed by the statement
```python
import sys
sys.stderr.write('cannot open input file %s\n' % sys.argv[1])
```
```shell
cannot open input file hello
```
14. If the file mary.txt contains the following lines
```
Mary had a little lamb
little lamb, little lamb
Mary had a little lamb
its fleece was white as snow
```
what is the value of L after executing the following statements?
```python
fh = open('mary.txt', 'r')
L = fh.readlines()
fh.close()
```
```python
['Mary had a little lamb\n',\
'little lamb, little lamb\n',\
'Mary had a little lamb\n',\
'its fleece was white as snow'\n]
```
15. What is the purpose of end='' in the statement
```python
print(line, end='')
```
> end with nothing
16. Explain why
```pyhton
'hello'.find('e')
```
results in the integer value of 1, while
```python
'hello'.find('a')
```
results in -1.
> find will return the first index that you want to find in the iterable, and return -1 when target is not in iterable.
17. Rewrite the string literal "hello, I'm John." using
```python
'hello, I\'m John.' #single quotes
'''
hello, I\'m John.'
''' #triple single quotes
"""
hello, I'm John.
""" #triple double quotes
r"hello, I'm John." #instead of double quotes.
```
18. Rewrite the string literal 'she says, "This is great!" and left' using
```python
"she says, \"This is great!\" and left" #double quotes
'''she says, "This is great!" and left'''#triple single quotes
"""she says, \"This is great!\" and left"""#triple double quotes
r'she says, "This is great!" and left'#instead of single quotes.
```
19. Rewrite the string literal '\\n means newline' using a raw string.
```python
r'\n means newline'
```
20. After executing the statement
```python
t = 'hello' "world"
```
What is the value of t?
```console
'helloworld'
```
21. What is the value of
```python
len("hello")
len("I\tam\there")
len('McDonald\'s')
```
```console
5
9
10
```
22. Rewrite the following triple-quoted string literal using a non-triple-quoted string literal
```python
sourceCode = '''<html>
<body>Welcome</body>
<html>'''
```
on one single line
```python
sourceCode = '<html>\n<body>Welcome</body>\n<html>'
```
on three separate lines
```python
sourceCode = '<html>\n\
<body>Welcome</body>\n\
<html>'
```
23. Assume
```pyhton
month = 7
day = 4
year = 2019
```
How do you format the date using % formatting so that it appears as strings (expressed as string literals)
```python
'%d/%d/%d' % (month, day, year)
'7/4/2019'
'%02d/%02d/%d' % (month, day, year)
'07/04/2019'
```
24. What is the value of
```python
'%9.2f' % 13.5
'%9.2f' % 123456789.0193
```
```python
' 13.50'
'123456789.02'
```
25. What is the meaning of 5e2? What is its data type?
> 5*10^2, float
26. What is the value of 5e-2?
> 5*10^-2
27. What is the value of '%c' % 100, given that ord('a') has the value of 97?
> 'd'
28. What is the format string S such that S.format(month, day, year)is equivalent to the traditional formatting of
```python
'%d/%d/%d' % (month, day, year)
```
```python
S = '{}/{}/{}'
```
29. What is the value of the expression
```python
'one {0}, two {0}s, three {0}s'.format('apple')
```
```python
'one {apple}, two {apple}s, three {apple}s'
```
30. What is the format string S such that
```python
S.format(12)
```
evaluates to the string
```python
'12 decimal is 0c hex and 14 octal'
```
```python
S = '{0} decimal is {0:02x} hex and {0:02o} octal'
```
31. What is the value of the expression
```python
'lastname {1}, firstname {0}'.format('John', 'Smith')
```
```python
'lastname Smith, firstname John'
```
32. Rewrite the following expressions as f-string:
```python
'%d/%d/%d' % (month, day, year)
'{:02d}/{:02d}/{:04d}'.format(month, day, year)
```
```python
f'{month}/{day}/{year}'
f'{month:02d}/{day:02d}/{year:02d}'
```
33. What is the value of the expression
```python
'www.nthu.edu.tw'.split('.')
'Mary had a\nlittle lamb'.split()
```
```python
['www', 'nthu', 'edu', 'tw']
['Mary', 'had', 'a', 'little', 'lamb']
```
34. Suppose you type the unix command wc (lightblue) and get the output (lightgreen) as shown below:
```shell
$ wc mult.py
9 32 249 mult.py
```
What are the meanings of 9, 32, and 249?
>lineCount = 9
>wordCount = 32
>charCount = 249
35. What is the value of the expression
```python
'(' + ')('.join(['a', 'b', 'c', 'd']) + ')'
''.join('Mary had a little lamb'.split())
```
```python
'(a)(b)(c)(d)'
'Maryhadalittlelamb'
```
36. Assume you have
```python
import string
```
What is the value of
```python
string.punctuation
string.digits
string.ascii_lowercase
string.whitespace
string.printable
```
```python
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
'0123456789'
'abcdefghijklmnopqrstuvwxyz'
' \t\n\r\x0b\x0c'
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
```