Extra ExercisesΒΆ

  1. The module “keyword” determines if a string is a keyword. keyword.iskeyword(s) where s is the string will return either True or False depending on whether or not the string is a Python keyword. Import this module and test to see if the words in list test are keywords. Save the answers in the list, keyword_test.

1.1 Import the module called keyword and use the method called iskeyword that takes one string as input and returns True or False in order to check if certain words are keywords in python. Follow instructions in the comments for specific assignment statements.

1.2 The module called keyword has an attribute called kwlist that is a list of all Python keywords. Import the keyword module and assign this list to the variable kws. Then, produce a list of all three-letter keywords and assign it to the variable kw3.

  1. The module “operator” exports functions that correspond to operators of Python. operator.lt(a,b) is one example that is equivalent to a < b. Another example is operator.neg(a) which returns -a. Import this module and use its functions to find the negative values of x and y and compare the values to see if the negative value of y is greater than the negative value of x. Save the response to the variable output.

2.1 Provided are two variables, a and c. Import the module operator and assign to the variable adding, the result of using the method .add, which takes two numbers as its input. Assign to the variable multi the result of using the method .mul, which takes two numbers as its input. In both cases, use a and c as their input.

2.2 The operator module contains functions that correspond to mathematical operations (such as .add and .sub). Import the operator module and use the .pow method, which takes two numbers as input and returns the first number raised to the second number, on the variables a and b. Assign the output to the variable c. Then, use the .div method, which takes two numbers as input and returns the first number divided by the second number, to find c divided by d. Save this output to the variable e.

  1. The module “math” provides access to mathematical functions. Import this module and use math.exp(x), which is equivalent to e**x, to populate the list exp with the value of e to the power of each number in the list numbs.

3.1 Import the module Math and use the .ceil and .floor methods. .ceil takes a float as input and returns the integer above the float, .floor takes a float as input and returns the integer below the float. Assign to the variable top the return value when using the .ceil method on the float 2.09. Assign to the variable bottom the return value when using the .floor method on the float 94.999.

3.2 The math module contains mathematical functions, including trigonemetric ones. Import the math module and use the .sin, .cos, and .tan methods to prove that sin(0.6)/cos(0.6) = tan(0.6). Save sin(0.6) to the variable s, save cos(0.6) to the variable c, and save tan(0.6) to the variable t. Test whether the two values are equal, and save the result - which will be a Boolean - to the variable test.

  1. The module “string” provides several constants, such as ascii_letters which returns all lowercase and uppercase letters, and digits, which returns the numbers 0-9. Using these constants and the string module, go through the string, str1, and determine whether each element is a number or a letter. If it is a number, the string “number” should return. If it is a letter, the string “letter” should return. Save your responses in the list, resp.

4.1 The module string provides several constants, such as .punctuation and .printable where punctionation returns a string of ASCII charaters that are considered punctionation, and printable returns all ASCII characters that are able to be printed, such as digits, letters, punctuation, and whitespace. For every element in options, if it is in .punctionation or .printable, then add it to a new list called small_options.

4.2 The string module provides sequences of various types of Python characters. It has an attribute called digits that produces the string ‘0123456789’. Import the module and assign this string to the variable nums. Below, we have provided a list of characters called chars. Using nums and chars, produce a list called is_num that consists of tuples. The first element of each tuple should be the character from chars, and the second element should be a Boolean that reflects whether or not it is a Python digit.

Next Section - Requesting data from the Internet