Python - Getting started
We use the following book as the main reference(linked to Amazon):
Suppose that you have installed Python 3.8 on a windows machine, you can open a terminal windows ( See: https://en.wikipedia.org/wiki/Cmd.exe), do the following:
py -m pip install colorama
The command, follow up command for upgrading pip and the outputs are shown below.
C:\WINDOWS\system32>py -m pip install colorama
Installing collected packages: colorama
Successfully installed colorama-0.4.3
WARNING: You are using pip version 19.2.3, however version 19.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
C:\WINDOWS\system32>py -m pip install --upgrade pip
Downloading https://files.pythonhosted.org/packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl (1.4MB)
|████████████████████████████████| 1.4MB 930kB/s
Installing collected packages: pip
Found existing installation: pip 19.2.3
Successfully uninstalled pip-19.2.3
Successfully installed pip-19.3.1
Note that it gives a warning after installing colorama and wants you to upgrade pip.
from colorama import init
from colorama import Fore, Back, Style
print("The result of: ", end='') # "end=''" prevents line break
print(r'repr("How are \nyou?")', end='')
print(" is the following ")
print(Fore.RED + repr("How are \nyou?"))
print(Fore.RED + 'TEST Color: some red text')
print(str("How are \nyou?"))
print('Color is back to normal now!')
Note that color will remain to be red until it is reset by
print(Style.RESET_ALL) and in the code
print(Fore.RED + 'TEST Color: some red text'),
Fore.RED + is not necessary.
Python IDLE may not show the colored text. If you save the above as a .py file and click it directly, it will show some text color you are looking for.
I hope the above can save you some time. Now, here is a short exercise that also involves a small math trick, please try your best to do it.
Exercise: Let. How many numbers in up to , are perfect squares? For each perfect square, you need to print the result like follows, including the space patterns (the first line, there are spaces around the equal sign but the second line, no spaces). Of course, you might end up googling some info that can help you. Your code should have no more than 12 lines, comments to code is optional but recommended.
Solution is available upon request. Tutorial services are also available.