802 Lab6 S11
802 Lab6 S11
802 Lab6 S11
Explanation:
Two key aspects of the `print()` function are illustrated in the Python code below: it
divides 1 by 7 and then prints that with the label "1/7" to yield output `1/7
0.14285714285714285`. It goes on to use the argument `end=''` to print "Hello" followed
by "World" on the same line, without spaces. A new line is attached after "HelloWorld" to
ensure that the output is formatted neatly.
Output:
Explanation:
This example code shows the flexibility of the `format()` function to format strings in
Python. First, it prints a sentence with a string and a float number with six decimals.
Then, it shows a version with zero decimal places, followed by an average score with
two decimal places. Finally, it produces several numeric forms by converting the integer
100 to both binary and octal representations.
Output
Explanation:
This Python code efficiently illustrates several ways of string formatting, namely
dictionary unpacking, f-strings, and `str.format()`. To make it more understandable,
positional parameters are used first to format employee details, followed by f-strings
where variables can be put directly inside the strings. There are two functions in the
code that show integer powers differently in order to compare structured versus
disorganized results. Lastly, the influence of formatting on overall readability by the
user's output is shown.
Output:
Explanation:
The following code is used to express currency in different locales using Python's
`locale` package. Currency representations are shown both grouped and ungrouped,
and it begins by setting the locale to British English. It formats the value in USD, using
American English, but emphasizes the grouping effect; it shows, at the end, the
rounding effect in the monetary representation - by applying Japanese formatting to the
yen.
Output
Explanation:
This Python example program will use the `datetime` module to format today's date and
time. The current date and time are assigned to `current_date` following its retrieval with
`datetime.datetime.today()`. The code displays a date along with both full and
abbreviated weekday and month names. A numeric representation is also shown in
several methods of formatting. The code also prints out both 12-hour and 24-hour times.
The date strings are then formatted, showing just a few examples of the date formatting
options available in Python.
Output:
Explanation:
This utility of dictionaries in managing phonebook entries is demonstrated by the Python
code that fetches the telephone number for "Jack" for output and creates a `phonebook`
dictionary with names and their corresponding telephone numbers. Using a loop, the
elements in the second dictionary, `table`, are prepared and outputted. It illustrates how
one uses dictionaries for structuring data representation.
Output:
Explanation:
This code applies the use of `*args` to create the function `addNumbers` to add an
arbitrary number of values. It calculates the sum by iterating over the input values,
starting with the variable `sum` set to zero. To demonstrate how the function works, it is
defined and called three times: the first time with no arguments-it prints out 0; the
second time with four arguments-5, 6, 7, 8-it prints out 26; and finally, with 10
arguments, printing out 56.
Task8.1
Output
Explanation:
The function here takes an otherwise predefined radius to compute the area of a circle,
and then prints that area out to two decimal places. The function is called from a sample
use, like this.
Task8.2
Output:
Explanation:
This version takes the radii as an argument, calculates the area of circles, and returns
the output with a round to two decimal points as the result. As can be seen in the
example below for a couple of values of radius, the use of f-strings ensures that the
output is clear and precise.
Task8.3
Output:
Explanation:
For clarity in expression, this code uses strftime for date and time formatting. That way,
the result will be clear, readable, and well-organized since it is grouping both currency
amounts. In addition, clear indications are made for each amount for clarity.
Task8.4
Reflection:
The use of variadic functions makes it extremely easy to handle multiple arguments in
programming, and thus flexible in many applications. This flexibility, however, increases
the possibility of crashes when combined with formatted output. If the number or type of
arguments passed to a formatted string is not under that defined in the format
specifiers, the program will run into runtime errors; this may thus crash the whole thing.
This problem is mostly experienced with variadic functions, where the input has so
much variation. To prevent such issues, developers should implement input validation to
ensure that the arguments that reach the function are in the same format as expected.
Techniques of error handling include try-except blocks which capture errors and then
control them so that the program does not crash.