Home Subscribe

1. Introduction

In this course, Python, version 3.7 or above is required. Here, the Ubuntu operating system is used to test the codes. You may use any other operating system. There are various ways to install Python on your computer. In this course, we recommend using Anaconda to install Python. Anaconda will automatically install most of the required libraries in this course.

To write and edit the Python programs you can use any plain text editor, such as gedit, nano, vi/vim, notepad, or any other. In this course, we recommend Visual Studio Code Editor, but you can use any other editor of your choice.

To install Python in Ubuntu using the Anaconda and to install the Visual Studio Code Editor, follow the following steps.

1.1. Install Anaconda

1.1.1. Installing Anaconda on Linux

  1. Download Anaconda for Linux x86 system from https://www.anaconda.com/download/#linux. It may take a few minutes to download. In this case, Anaconda3-2022.10-Linux-x86_64.sh was downloaded (~ 738 MB). In your case, you may have downloaded a later version, which is fine.

  2. Launch terminal using Ctrl+Alt+T

  3. Navigate to the Downloads directory where the Anaconda download was saved. Use cd Downloads

  4. To check if Anaconda3-2022.10-Linux-x86_64.sh was successfully downloaded in the Downloads directory, use ls as shown in Figure 1.

anaconda-ls-downloads
Figure 1. Check if Anaconda exists in the Downloads directory.
  1. Verify the installer integrity using SHA-256. shasum -a 256 Anaconda3-2022.10-Linux-x86_64.sh. Replace Anaconda3-2022.10-Linux-x86_64.sh with your file path shasum -a 256 /PATH/FILENAME. This step is not mandatory but it is recommended. You should see an output such as e7ecbccbc197ebd7e1f211c59df2e37bc6959d081f2235d387e08c9026666acd

  2. Install Anaconda by running bash Anaconda3-2022.10-Linux-x86_64.sh and follow the instructions. Press Enter to read every page of the agreement document or press Q to go to "Do you accept the license terms?". Type yes to agree and continue or no to quit and stop the installation. Press Enter to confirm the installation directory and continue with the installation.

  3. To check if Anaconda has been installed successfully as shown in Figure 2, run anaconda -V or anaconda --version. You should see a terminal output, such as anaconda Command line client (version 1.11.0).

anaconda-installed-version
Figure 2. Check if Anaconda is installed.

Here is the summary of the above commands, after downloading the Anaconda.

1cd Downloads
2ls
3shasum -a 256 Anaconda3-2022.10-Linux-x86_64.sh
4bash Anaconda3-2022.10-Linux-x86_64.sh
5anaconda --version

1.1.2. Installing Anaconda on Windows

To easily follow the commands in this course on Windows, before installing Python using Anaconda, we recommend that you install Windows Subsystem for Linux (WSL):

  1. Open Power Shell or Command Prompt as an administrator.

  2. Type wsl --install and Enter

  3. Wait until the download is complete. If it freezes at a certain percentage and takes too long, the installation is most likely complete. Quit using CTRL+C.

  4. Reboot the computer.

  5. After you log in to Windows, WSL will ask for username and password. These details do not have to be the same as that for Windows.

  6. You can now install Anaconda on this WSL terminal by following the above steps.

1wsl --install

For details, refer to (Install WSL | Microsoft Learn, n.d.).

To install Python using Anaconda on other operating systems, refer to (Installation — Anaconda Documentation, n.d.).

1.2. Install Visual Studio Code Editor (vscode)

Downloading and installing vscode is rather straightforward.

  • Download vscode from https://code.visualstudio.com/ and follow installation instructions.

  • For details refer to (Documentation for Visual Studio Code, n.d.).

  • Having installed the vscode, open the terminal Ctrl+Alt+T and type code and Enter. This will open the vscode and it will be ready to save your programs in Documents directory. If you want to open vscode in another directory, say, Documents/Python, navigate to that directory using cd Documents/Python. Alternatively, you can go to the installed programs, find vscode, and click it to open.

  • If vscode may ask you to install python extensions, please accept and install them.

To activate WSL on vscode on Windows, install WSL extension. Click the icon on bottom left corner and you may select WSL: New WSL Window using Distro or WSL: Reopen Folder in WSL. For details, refer to (Developing in the Windows Subsystem for Linux with Visual Studio Code, n.d.).

1.3. Test if Python Works

  • In vscode, go to File  New File…​ Make a new file and name it test.py. and write the following short program.

  • An easy way to open the vscode and at the same time create the new file is as follows.

    • Open terminal Ctrl+Alt+T

    • Using one command, open vscode, navigate to documents directory, and create a new file: code Documents/test.py then Enter

    • Type the following program and then save it Ctrl+S

1.3.1. Python Test Program

  • On the left side of vscode, right-click on the test.py  Open in Integrated Terminal.

  • To run the python code type the following command in the terminal python test.py

  • This should print the following. See Figure 3

1print(u'\u2705'+" Python is successfully installed in this computer")
run test py in terminal
Figure 3. Python unicode test program.

To clear the terminal, enter the following command clear. Test the following program and run it the same way as above. You can just write the following program just below the above program.

1.3.2. Python Calendar Program

1# This program will print any calendar year and month you like.
2
3import calendar 
4
5year = int( input("Enter any calendar year: ") )
6month = int( input("Enter any calendar month: ") )
7
8print(calendar.month(year, month))
python-calendar
Figure 4. Python calendar on the terminal.

You can print the full year using calendar.calendar(year) or check for a leap year using calendar.isleap(year)

1.4. Installing Libraries Using conda

Sometimes we may need a special library that will help us to easily write a program that would otherwise be more complicated to write. In this section, we shall install install python library using pip and conda, and test it using a python code.

  • To install pip via conda, open command terminal or use the same vscode terminal type this command:

    • conda install pip

  • Install the following library.

    • conda install pywhatkit this library can help us to change a given typed text into handwritten notes.

  • Using the same vscode terminal, enter the following command code handwriting.py to make and open a new python file called handwriting.py

  • Type the following program and run it by using python handwriting.py.

1.4.1. Python Typed-Text-to-Handwritten Program

1import pywhatkit as pw
2
3msg = '''The best way to make somebody
4remember you is to borrow money
5from them.'''
6
7pw.text_to_handwriting(msg,"msg-img.png")
text-to-image
Figure 5. Python typed text to a handwritten image.

1.5. Basic Linux Terminal Commands

If you are using Ubuntu, open terminal Ctrl+Alt+T
If you are using wsl and vscode on Windows, go to vscode menu, click Terminal  New Terminal.

print the current working directory (pwd)

sam@siliconwit:~$ pwd

You should see an output such as this

/home/sam

list the contents of a folder/directory (ls)

sam@siliconwit:~$ ls

You should see an output list such as this Documents, Downloads and other contents in your directory.

anaconda3          bin         Pictures
Music              Public      opt
Desktop            Documents   Downloads

change the directory (cd)

sam@siliconwit:~$ cd Downloads
sam@siliconwit:~/Downloads$

The above command should take you to the Downloads folder if it exists in the path provided.

sam@siliconwit:~/Downloads$ ls
Anaconda3-2022.10-Linux-x86_64.sh
ABDTR894278.bibtex


Add Comment

* Required information
1000
Drag & drop images (max 3)
What is the fifth month of the year?

Comments

No comments yet. Be the first!