802 Lab6 S10
802 Lab6 S10
Code:
Explanation of code:
This Python code makes the `print()` method useful for lots of different purposes. It
first computes the value of 1 divided by 7 and prints this along with "1/7," so it's
surprising that `1/7 0.14285714285714285` looks like that. The `end=''` parameter
makes it subsequently print "Hello" and "World" on the same line, without leaving a
space between them either, and a new line is added following to make it easier to
read.
Task2
Code:
Explanation:
This tiny snippet of Python code demonstrates the formatting of strings with
`format()`. It begins by printing a message that includes a string combined with a
floating-point number formatted to six decimal places. It then prints the average
score, which is formatted to two decimal places before being rounded to zero.
Finally, it demonstrates this flexibility in formatting with the last two lines by
showing how the integer 100 could be converted to its binary and octal forms.
Task3
Code:
Explanation:
This Python code sample shows some of the ways you can format strings: dictionary
unpacking, f-strings, and positional parameters. First, `str.format()` is applied to
employee data to nicely print out on both names and wages. The code can then be
streamlined directly by incorporating variables using the f-string function. It also
applies functions that demonstrate integer powers of comparison between
structured and disorganized results. As it's pretty straightforward, you can easily
show how formatting helps you make your output more readable by including an
example that asks the user for input.
Task4
Code:
Explanation:
This example code showing how to use the `locale` module in Python when
formatting currency values to various locales. As an example, it shows what
formatting would look like both with and without grouping as you switch between
British English explaining what the format looks like, then picks up the grouping by
switching to American English and showing the same number again in USD, and
then finally showing how the currency looks in Yen using Japanese style and displays
rounding.
Task5
Code:
Explanation:
This code snippet is an illustration of the use of the `datetime` module in Python to
return a string of the current date and time. The function
`datetime.datetime.today()` records the current date and time, and it is assigned to
the variable `current_date`. There are several options by which the date can be
displayed: full and abbreviated weekday and month names and 12- and 24-hour
forms. Finally, the flexibility of Python's date formatting facilities is demonstrated by
printing formatted date strings to the console at the end.
Task6
Code:
Explanation:
This code sample demonstrates how to create a phonebook using Python
dictionaries. After initializing a `phonebook` dictionary including names along with
associated numbers, it retrieves Jack's phone number. A loop processing a second
dictionary, `table`, produces a formatted output showing each name along with
their associated phone number. It is an instance that shows the advantage of
organized data management using dictionaries.
Task7
Code:
Explanation:
This small program shows the function `addNumbers`, which takes a variable
number of arguments via `*args`. It then adds each integer from the parameters
with `sum` initialized to zero. There are three calls to this function: one with no
input (zero), one with four numbers (26), and one with 10 numbers (56). All results
are reported.
Task8
Q1
Code:
Explanation:
This function takes the provided radius, computes a circle's area, then prints the
answer to two decimal places. To use this clearly for example of the computation, an
example radius of 2.5 will be used to illustrate the computation effectively.
Q2.
Code
Explanation:
This function employs an f-string to format the areas to two decimal places after
calculating the area of circles for a given list of radii. The following example
illustrates how to call the function by passing a list of radius values.
Q3.
Code
Explanation:
This code also keeps a clean output by capturing the date and time going into the
system and printing it out clearly. Because the dollars and yen are formatted with
currency grouping, both of these figures are easier to read.
Q4
Reflection:
Variadic functions allow a function to take an arbitrary number of arguments. With
them, you can easily work with dynamic inputs, which is useful when you have
different requirements all the time. But, as you might have guessed, this flexibility is
a breeding ground for potential crashes when not handled properly, especially with
formatted outputs. Formatted strings demand that the number and type of
arguments match the placeholders exactly. The mistake then would result from a
mismatch, for instance, a program attempting to input in a string where an integer
is required; a run time error may be incurred or a program may crash. Not even the
case of variadic functions where the number and type of the inputs are not
determined; hence this increases the importance of validating inputs before
formatting. More critically, the developers would need to check that inputs are
correctly matched to the format string lest a runtime error would be incurred.
However, with proper error handling in place, these problems are caught and dealt
with, preventing the software from crashing: therefore, the overall reliability
improves.