Python - Getting started

We use the following book as the main reference(linked to Amazon): 

The reference book is: Beginning Python: From Novice to Professional 3rd Edition

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
Collecting colorama
Downloading https://files.pythonhosted.org/packages/c9/dc/45cdef1b4d119eb96316b3117e6d5708a08029992b2fee2c143c7a0a5cc5/colorama-0.4.3-py2.py3-none-any.whl
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
Collecting 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
Uninstalling 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. 

import colorama
from colorama import init
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?"))
num=1+3+ \
5
print(num)
print(Style.RESET_ALL)
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 \(f(x)=5x^2+4x-17\). How many numbers in \(f(2), f(3)\) up to \(f(5000)\), 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.

Sample output:

f(7)=256=16^2

Solution is available upon request. Tutorial services are also available.

Last modified: Tuesday, 14 January 2020, 4:16 PM