Try   HackMD

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

    ​​​​$ 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

    ​​​​$ 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

    ​​​​$ cat *.py
    

    What is the meaning of *.py?

    find all file which filename end with .py

  8. What does the following command do?

    ​​​​$ 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:

    ​​​​$ chmod +x prog.py
    

    change the prog.py's mod

  11. What is the value of

    ​​​​len([3, 7, 2, 0, 8])
    ​​​​len(['hello', 'world', 'goodbye'])
    ​​​​len('admin')
    
    ​​​​5
    ​​​​3
    ​​​​5
    
  12. Suppose you run the command

    ​​​​$ python3 showargs.py  hello  world  goodbye
    

    Inside the showargs.py program, suppose you have

    ​​​​import sys
    

    What is the value of sys.argv?

    ​​​​['showargs.py', 'hello', 'world', 'goodbye']
    

    What is the value of len(sys.argv)?

    ​​​​4
    

    What is the value of sys.argv[1:]?

    ​​​​['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

    ​​​​import sys
    ​​​​sys.stderr.write('cannot open input file %s\n' % sys.argv[1])
    
    ​​​​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?

    ​​​​fh = open('mary.txt', 'r')
    ​​​​L = fh.readlines()
    ​​​​fh.close()
    
    ​​​​['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

    ​​​​print(line, end='')
    

    end with nothing

  16. Explain why

    ​​​​'hello'.find('e')
    

    results in the integer value of 1, while

    ​​​​'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

    ​​​​'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

    ​​​​"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.

    ​​​​r'\n means newline'
    
  20. After executing the statement

    ​​​​t = 'hello' "world"
    

    What is the value of t?

    ​​​​'helloworld'
    
  21. What is the value of

    ​​​​len("hello")
    ​​​​len("I\tam\there")
    ​​​​len('McDonald\'s')
    
    ​​​​5
    ​​​​9
    ​​​​10
    
  22. Rewrite the following triple-quoted string literal using a non-triple-quoted string literal

    ​​​​sourceCode = '''<html>
    ​​​​<body>Welcome</body>
    ​​​​<html>'''
    

    on one single line

    ​​​​sourceCode = '<html>\n<body>Welcome</body>\n<html>'
    

    on three separate lines

    ​​​​sourceCode = '<html>\n\
    ​​​​<body>Welcome</body>\n\
    ​​​​<html>'
    
  23. Assume

    ​​​​month = 7
    ​​​​day = 4
    ​​​​year = 2019
    

    How do you format the date using % formatting so that it appears as strings (expressed as string literals)

    ​​​​'%d/%d/%d' % (month, day, year)
    ​​​​'7/4/2019'
    ​​​​'%02d/%02d/%d' % (month, day, year)
    ​​​​'07/04/2019'
    
  24. What is the value of

    ​​​​'%9.2f' % 13.5
    ​​​​'%9.2f' % 123456789.0193
    
    ​​​​'    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

    ​​​​'%d/%d/%d' % (month, day, year) 
    
    ​​​​S = '{}/{}/{}'
    
  29. What is the value of the expression

    ​​​​'one {0}, two {0}s, three {0}s'.format('apple')
    
    ​​​​'one {apple}, two {apple}s, three {apple}s'
    
  30. What is the format string S such that

    ​​​​S.format(12)
    

    evaluates to the string

    ​​​​'12 decimal is 0c hex and 14 octal'
    
    ​​​​S = '{0} decimal is {0:02x} hex and {0:02o} octal'
    
  31. What is the value of the expression

    ​​​​'lastname {1}, firstname {0}'.format('John', 'Smith')
    
    ​​​​'lastname Smith, firstname John'
    
  32. Rewrite the following expressions as f-string:

    ​​​​'%d/%d/%d' % (month, day, year)
    ​​​​'{:02d}/{:02d}/{:04d}'.format(month, day, year)
    
    ​​​​f'{month}/{day}/{year}'
    ​​​​f'{month:02d}/{day:02d}/{year:02d}'
    
  33. What is the value of the expression

    ​​​​'www.nthu.edu.tw'.split('.')
    ​​​​'Mary had a\nlittle lamb'.split()
    
    ​​​​['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:

    ​​​​$ 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

    ​​​​'(' + ')('.join(['a', 'b', 'c', 'd']) + ')'
    ​​​​''.join('Mary had a little lamb'.split())
    
    ​​​​'(a)(b)(c)(d)'
    ​​​​'Maryhadalittlelamb'
    
  36. Assume you have

    ​​​​import string
    

    What is the value of

string.punctuation
string.digits
string.ascii_lowercase
string.whitespace
string.printable
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
'0123456789'
'abcdefghijklmnopqrstuvwxyz'
' \t\n\r\x0b\x0c'
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'