802 Lab6 S9
802 Lab6 S9
802 Lab6 S9
Code:
Output
Explain:
The above Python code shows two applications of the `print()` function. It
evaluates the expression 1/7 and prints it out next to the string "1/7", which
gives `1/7 0.14285714285714285`. It then prints "Hello" and "World" on the
same line through use of `end=''` to prevent spaces from appearing. Finally,
a newline is inserted after "HelloWorld" for readability:
Q2.
Code:
Output:
Explanation:
The above Python code illustrates the best string formatting using the
`format()` method. This section of the code first prints out a message that
contains a floating-point number formatted to six decimal places and
appended with a string. The code formats an average score to two decimal
places and rounds another output to no decimal places. Finally, the integer
value 100 is converted to both binary and octal representations.
Q3
Code:
Output:
\
Explanation:
The above Python code presents all the options of string formatting along
with the usage of `str.format()`, f-strings, and dictionary unpacking. To start
with this, here is a presentation of the display of employee names and
salaries employing positional arguments in formatting. F-strings are used to
directly include variables into the string for even more complex situations.
Organized versus unorganized outputs are compared using functions that
determine the integer powers. Following this, the use of user input is brought
in to demonstrate the essence of proper output formatting.
Q4:
Code:
Output:
Explanation:
This Python code applies the locale module to format currency values in
different regional formats. The same currency amount is formatted in British
English with grouped and ungrouped presentation, and then the locale is set
to American English for showing the same amount in USD, demonstrating the
grouping effect of the locale. Finally, it switches to a Japanese locale that
rounds the amount to display in Yen.
Q5.
Code
Output:
Explanation:
This Python code snippet uses the module `datetime` to format the current
date and time in a variety of ways. It is going to get the current date and
time for the local machine with the line `datetime.datetime.today()` and
assign that to `current_date`. Then it applies a few formatting styles to the
date: abbreviated and full weekday and month names as well as different
numeric formats. Finally, demonstrates both 12-hour and 24-hour time
formats.:. The formatted outputs are printed, illustrating Python's capability
in date formatting.
Q6
Code
Output:
Explanation:
This python program illustrates the use of Python dictionaries to store and
retrieve information on a phonebook. A `phonebook` dictionary is declared
with keys that are names, and it retrieves Jack's phone number and prints it
out. There is also a second-defined dictionary called `table`, and it contains
items in a loop in which it prints every name along with its phone number in
a formatted way. This shows how useful dictionaries can be when it comes to
handling data.
Q7
Code:
Output:
Explanation:
The code defines a function called `addNumbers` which can actually be
passed any number of arguments using the `*args` syntax. It calculates the
total sum by initializing a variable at zero and incrementing it with each
argument passed in a loop. Three calls are made to the function,
demonstrating its capabilities. The first gives a result of 0 when no
arguments are passed, the second gives a result of 26 with four numbers,
and the third gives a result of 56 from ten inputs, showing each result.
Q8
1. Code:
Output:
Explanation:
This calculates the area of a circle given a specified radius and prints
the result rounded to two decimal places. Usage Example
Demonstrates how to call this function with a radius of 10.
2. Code
Output:
Explanation:
It takes multiple radius inputs, calculates the corresponding areas for
each, and returns the results rounded to two decimal places. Here's an
example of how the function is applied with different radius values.
3. Code
Output:
Explanation:
It simply prints the current date and time without additional formatting.
It uses currency symbols for both amounts without being vague about
the output. It is quite a simple methodology, making the code a
readable one.
4. Reflection:
Variadic functions are flexible because a function can take a different
number of arguments while yet being useful in dynamic applications.
However, the flexibility offered by variadic functions might bring about
some vulnerabilities, especially when using formatted outputs. The
most common vulnerability lies in the fact that the format string is not
matched with the number or type of arguments that pass through the
function. For instance, if there is a function that expects an integer but
receives a string, the software may crash due to all the mismatches.
This is particularly daunting in variadic functions since input may vary
so much. To prevent such crashes, validation of the input has to
precede the passing unto the formatted output. The developer is
supposed to check that the number and type of arguments are suitable
for the format expected. In addition, through a try-except block, errors
may be identified and the program is protected from crashing, hence
improving the robustness of the software and the user's experience.