pprint.pprint()
function will “pretty print” the contents of a list or dictionary to the screen, while the pprint.pformat()
function will return this same text as a string instead of printing it. Not only is this string formatted to be easy to read, but it is also syntactically correct Python code. Say you have a dictionary stored in a variable and you want to save this variable and its contents for future use. Using pprint.pformat()
will give you a string that you can write to .py file. This file will be your very own module that you can import whenever you want to use the variable stored in it.For example, enter the following into the interactive shell:
that stored the passwords in a dictionary. Updating the passwords required changing the source code of the program. This isn’t ideal because average users don’t feel comfortable changing source code to update their software. Also, every time you modify the source code to a program, you run the risk of accidentally introducing new bugs. By storing the data for a program in a different place than the code, you can make your programs easier for others to use and more resistant to bugs.Q: | 1. What is a relative path relative to? |
Q: | 2. What does an absolute path start with? |
Q: | 3. What do the |
Q: | 4. What are the |
Q: | 5. In C:\bacon\eggs\spam.txt, which part is the dir name, and which part is the base name? |
Q: | 6. What are the three “mode” arguments that can be passed to the |
Q: | 7. What happens if an existing file is opened in write mode? |
Q: | 8. What is the difference between the |
Q: | 9. What data structure does a shelf value resemble? |
For practice, design and write the following programs.
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.
The program would find these occurrences and prompt the user to replace them.
Enter an adjective: silly Enter a noun: chandelier Enter a verb: screamed Enter a noun: pickup truck
The following text file would then be created:
The silly panda walked to the chandelier and then screamed. A nearby pickup truck was unaffected by these events.
The results should be printed to the screen and saved to a new text file.