Python Bridging2023
Python Bridging2023
3. Python Programming
} Numbers, Strings, and Lists
} Dictionary, Arrays and Data Frames
} Flow control: For loop, While loop, If-else
} Python functions and modular programming
} Data exploration and visualization
J. LIU AEF HKBU
Command-line Interface
} Unlike graphic user interface (GUI), Command-line Interface (CLI) interacts
with the operating system using text-based commands.
} Terminal (macOS) or Command Prompt (Windows)
Prompt Prompt
cd <relative path>
cd <absolute path>
cd <absolute path>
Move one layer up from CWD
Move one layer up again
cd <relative path>
move two layers up
cd <relative path>
list all files in CWD
} Open a file
} (macOS) open <file> (Windows) <file>
} Rename a file
} (macOS) mv <source file> <new file> rename (by moving)
} (Windows) ren <source file> <new file>
Note: Path name of the folders/files can be either relative or absolute
J. LIU AEF HKBU
Note: Path name of the files/folders can be either relative or absolute
} Copy files/folders
} (macOS) cp <source file> <destination folder>
} cp <file 1> <file 2> <destination folder> copy two files
} cp *.* <destination folder> copy all types of files
} (Windows) copy <source file> <destination folder>
} Delete files/folders
} (macOS) rm <file> delete <file>
} (macOS) rm –r <folder> delete <folder> and all files/folders in it
} (Windows) del <file> delete <file>
} (Windows) rmdir /s <folder> delete <folder> and all files/folders in it
J. LIU AEF HKBU
Go to the folder
Delete new_file123.txt
Delete new_file123.txt
Delete new_folder
Exercise 1 (20 mins)
} Open the CLI, create folders and files by completing the below steps.
1. Create a folder (my_folder) at a place you can easily find, navigate to it. (mkdir, cd)
2. Within the work_folder, create an empty txt file test1.txt and a new folder sub_folder.
(touch/type, mkdir)
3. Navigate to sub_folder and create another empty file test2.txt. (cd, touch/type)
4. Copy test1.txt from my_folder to sub_folder, and rename the copy as test1_copy.txt.
(cp/copy, mv/ren)
5. Move test1.txt from my_folder to sub_folder as well. (mv/ move)
6. Check the list of files in sub_folder. (ls/dir)
my_folder my_folder
test2.txt
test2.txt
test1.txt
} Windows Commands
} https://docs.microsoft.com/en-us/windows-
server/administration/windows-commands/windows-commands
Python Interpreter
} Open your CLI and check if the Python interpreter is properly
installed in your computer, with below commands:
} python3 --version
} where python3
} which python3 (only for macOS)
Prompt
} With a Code Editor, you can write a python script: open a new file editing
window (File -> New File), type in Python codes and save it as test.py in
your work folder.
J. LIU AEF HKBU
In windows Command Prompt, you may
use python if python3 is not found.
Create a
new project
} Import the module in another python script and test those functions.
} pip3 is the official package manager and pip command for Python3.
Try below commands in CLI.
} pip3 --version check pip version
} pip3 list check installed python packages
} pip3 install <package> install a package
} pip3 uninstall <package> uninstall a package
} pip3 install --upgrade <package> upgrade a package
} pip3 freeze > requirement.txt record an environment’s package list in a
txt file requirement.txt for future use.
Activate my_env1
Check python packages installed in my_env1
Create a new
environment using
different tools
Decide whether to
inherit packages
from the global
system , same as A virtual environment
virtualenv venv -- named venv (default name)
system-site-
packages
Note this step is optional, you can use Jupyter Notebook with the default
python environment.
} Softwares
} Command-line interfaces
} Python Interpreter and IDLE
} Python IDEs: PyCharm, Jupyter Notebook
} Techniques
} Interact with your computer using commands
} Install and manage python packages using pip3
} Manage virtual environments using virtualenv
} Python programming