Skip to content

Practise #02

Assignment 06

  1. What are escape characters, and how do you use them?
  2. What do the escape characters \n and \t stand for?
  3. What is the way to include backslash characters in a string?
  4. The string "Howl's Moving Castle" is a correct value. Why isn't the single quote character in the word Howl's not escaped a problem?
  5. How do you write a string of newlines if you don't want to use the n character?
  6. What are the values of the given expressions?

    1
    2
    3
    4
    'Hello, world!'[1]
    'Hello, world!'[0:5]
    'Hello, world!'[:5]
    'Hello, world!'[3:]
    
  7. What are the values of the following expressions?

    1
    2
    3
    'Hello'.upper()
    'Hello'.upper().isupper()
    'Hello'.upper().lower()
    
  8. What are the values of the following expressions?

    1
    2
    'Remember, remember, the fifth of July.'.split()
    '-'.join('There can only one.'.split())
    
  9. What are the methods for right-justifying, left-justifying, and centering a string?

  10. What is the best way to remove whitespace characters from the start or end?

Assignment 07

  1. What is the name of the feature responsible for generating Regex objects?
  2. Why do raw strings often appear in Regex objects?
  3. What is the return value of the search() method?
  4. From a Match item, how do you get the actual strings that match the pattern?
  5. In the regex which created from the r'(\d\d\d)-(\d\d\d-\d\d\d\d)', what does group zero cover? Group 2? Group 1?
  6. In standard expression syntax, parentheses and intervals have distinct meanings. How can you tell a regex that you want it to fit real parentheses and periods?
  7. The findall() method returns a string list or a list of string tuples. What causes it to return one of the two options?
  8. In standard expressions, what does the | character mean?
  9. In regular expressions, what does the character stand for?
  10. In regular expressions, what is the difference between the + and * characters?
  11. What is the difference between {4} and {4,5} in regular expression?
  12. What do you mean by the \d, \w, and \s shorthand character classes signify in regular expressions?
  13. What do means by \D, \W, and \S shorthand character classes signify in regular expressions?
  14. What is the difference between .*? and .* ?
  15. What is the syntax for matching both numbers and lowercase letters with a character class?
  16. What is the procedure for making a normal expression in regax case insensitive?
  17. What does the . character normally match? What does it match if re.DOTALL is passed as 2nd argument in re.compile()?
  18. If numReg = re.compile(r'\d+'), what will numRegex.sub('X', '11 drummers, 10 pipers, five rings, 4 hen') return?
  19. What does passing re.VERBOSE as the 2nd argument to re.compile() allow to do?
  20. How would you write a regex that match a number with comma for every three digits?

    It must match the given following:

    1
    2
    3
    '42'
    '1,234'
    '6,368,745'
    
    but not the following:

    1
    2
    '12,34,567' (which has only two digits between the commas)
    '1234' (which lacks commas)
    
  21. How would you write a regex that matches the full name of someone whose last name is Watanabe?
    You can assume that the first name that comes before it will always be one word that begins with a capital letter.

    The regex must match the following:

    1
    2
    3
    'Haruto Watanabe'
    'Alice Watanabe'
    'RoboCop Watanabe'
    
    but not the following:

    1
    2
    3
    4
    'haruto Watanabe' (where the first name is not capitalized)
    'Mr. Watanabe' (where the preceding word has a nonletter character)
    'Watanabe' (which has no first name)
    'Haruto watanabe' (where Watanabe is not capitalized)
    
  22. How would you write a regex that matches a sentence where

    1. the first word is either Alice, Bob, or Carol;
    2. the second word is either eats, pets, or throws;
    3. the third word is apples, cats, or baseballs; and
    4. the sentence ends with a period?

    This regex should be case-insensitive. It must match the following:

    1
    2
    3
    4
    5
    'Alice eats apples.'
    'Bob pets cats.'
    'Carol throws baseballs.'
    'Alice throws Apples.'
    'BOB EATS CATS.'
    
    but not the following:

    1
    2
    3
    'RoboCop eats apples.'
    'ALICE THROWS FOOTBALLS.'
    'Carol eats 7 cats.'
    

Assignment 08

  1. Is the Python Standard Library included with PyInputPlus?
  2. Why is PyInputPlus commonly imported with import pyinputplus as pypi?
  3. How do you distinguish between inputInt() and inputFloat()?
  4. Using PyInputPlus, how do you ensure that the user enters a whole number between 0 and 99?
  5. What is transferred to the keyword arguments allowRegexes and blockRegexes?
  6. If a blank input is entered three times, what does inputStr(limit=3) do?
  7. If blank input is entered three times, what does inputStr(limit=3, default='hello') do?

Assignment 09

  1. To what does a relative path refer?
  2. What does an absolute path start with your operating system?
  3. What do the functions os.getcwd() and os.chdir() do?
  4. What are the . and .. folders?
  5. In C:\bacon\eggs\spam.txt, which part is the dir name, and which part is the base name?
  6. What are the three "mode" arguments that can be passed to the open() function?
  7. What happens if an existing file is opened in write mode?
  8. How do you tell the difference between read() and readlines()?
  9. What data structure does a shelf value resemble?

Assignment 10

  1. How do you distinguish between shutil.copy() and shutil.copytree()?
  2. What function is used to rename files??
  3. What is the difference between the delete functions in the send2trash and shutil modules?
  4. ZipFile objects have a close() method just like File object's close() method. What ZipFile method is equivalent to File object's open() method?
  5. Create a programme that searches a folder tree for files with a certain file extension (such as .pdf or .jpg). Copy these files from whatever location they are in to a new folder.

SOLUTIONS

Click Here
  • Assignment 06 Solution
  • [Assignment 07 Solution]
  • [Assignment 08 Solution]
  • [Assignment 09 Solution]
  • [Assignment 10 Solution]
Back to top