Author

Tony Duan

1 Python Installation Guide

1.1 Managing Python Versions in Quarto

Code
library(reticulate)
py_require(c('pandas','pipdeptree'))
use_python("/Library/Frameworks/Python.framework/Versions/3.13/bin/python3.13")

1.2 Check Python Version

Code
# Import the sys module to access system-specific parameters and functions
import sys
# Print the Python version to the console
print(sys.version)
3.13.2 (v3.13.2:4f8bb3947cf, Feb  4 2025, 11:51:10) [Clang 15.0.0 (clang-1500.3.9.4)]

1.3 Check Python location

Code
print(sys.executable)
/Library/Frameworks/Python.framework/Versions/3.13/bin/python3.13

1.4 Delete Old Python Version on Mac

1.4.1 Delete Framework

Go → Computer → Macintosh HD → Library → Frameworks → Python.framework → Versions

Delete the old version Python folder:

1.4.2 Delete Python Files

List all Python files:

Code
ls /usr/local/bin/python*
/usr/local/bin/python3
/usr/local/bin/python3-config
/usr/local/bin/python3-intel64
/usr/local/bin/python3.11
/usr/local/bin/python3.11-config
/usr/local/bin/python3.11-intel64
/usr/local/bin/python3.12
/usr/local/bin/python3.12-config
/usr/local/bin/python3.12-intel64
/usr/local/bin/python3.13
/usr/local/bin/python3.13-config
/usr/local/bin/python3.13-intel64
/usr/local/bin/python3.13t
/usr/local/bin/python3.13t-config
/usr/local/bin/python3.13t-intel64
/usr/local/bin/python3t
/usr/local/bin/python3t-config
/usr/local/bin/python3t-intel64

Delete the old version:

Code
brew uninstall python@3.11

1.5 Set Python Version in RStudio

List all files in home path:

Code
Terminal
ls -a

Find .Renviron and edit to new version:

Code
Terminal
nano .Renviron

2 Install Jupyter

Code
Terminal
python3 -m pip install jupyter

3 Basic Data Types

Python has several built-in data types to store different kinds of values. Understanding these fundamental types is crucial for writing effective Python code.

3.1 Numbers

Python supports various numerical types, including integers (whole numbers) and floating-point numbers (numbers with decimal points).

3.1.1 Integers

Integers are whole numbers, positive or negative, without a decimal point.

# Example of an integer
my_integer = 10
print(f"Value: {my_integer}, Type: {type(my_integer)}")

3.1.2 Floating-Point Numbers

Floating-point numbers (floats) are numbers that have a decimal point.

# Example of a floating-point number
my_float = 10.5
print(f"Value: {my_float}, Type: {type(my_float)}")

3.2 Strings

Strings are sequences of characters, used for text. They are enclosed in single quotes (') or double quotes (").

# Example of a string
my_string = "Hello, Python!"
print(f"Value: {my_string}, Type: {type(my_string)}")

3.3 Booleans

Booleans represent one of two values: True or False. They are primarily used in conditional statements and loops.

Code
# Example of a boolean
my_boolean = True
print(f"Value: {my_boolean}, Type: {type(my_boolean)}")
Value: True, Type: <class 'bool'>

4 Work with Files

4.1 Get Current Directory

Code
# Import the os module, which provides a way of using operating system dependent functionality
import os
# Get the current working directory
os.getcwd()

4.2 Get All File Names Under Current Directory

Code
# Import the os module for interacting with the operating system
import os
# Import the pandas library for data manipulation
import pandas as pd
# Get a list of all files and directories in the current working directory
nameList=os.listdir(os.getcwd()) 
# Create an empty list to store file sizes
size_list=[]
# Loop through each file in the nameList
for file in (nameList):
  # Get the size of the file in megabytes and append it to the size_list
  size_list.append((os.stat(file).st_size)/1000000)
  

# Create a pandas DataFrame from the file names and sizes
file_size=pd.DataFrame(list(zip(nameList, size_list)),
columns=['file','size_MB'])
# Display the DataFrame
file_size
                        file    size_MB
0                  .Rhistory   0.017908
1                test_folder   0.000064
2       0 Basic python_files   0.000096
3   0 Basic python.rmarkdown   0.012419
4                  .DS_Store   0.008196
5   0-Basic-python.rmarkdown   0.012419
6                     images   0.000960
7         0 Basic python.qmd   0.012418
8                 hotels.csv  16.855599
9                     .RData   0.002595
10      3 statistic Book.qmd   0.000454
11         1 Python Book.qmd   0.000644
12           2 Data Book.qmd   0.001401
13                      data   0.000096

4.3 Get File Info

Code
# Import the os module
import os
# Get file status information for '3 statistic Book.qmd'
a=os.stat('3 statistic Book.qmd')
# Print the file status
a
os.stat_result(st_mode=33188, st_ino=129018382, st_dev=16777231, st_nlink=1, st_uid=501, st_gid=20, st_size=454, st_atime=1762156194, st_mtime=1759644922, st_ctime=1762156194)

Show st_atime:

Code
# Import the datetime module
import datetime as dt
# Get the last access time of the file and format it as YYYYMMDD
dt.date.fromtimestamp(a.st_atime).strftime('%Y%m%d')
'20251103'

4.4 Create Folder

Create it if it does not exist:

Code
# Check if the folder 'test_folder' does not exist
if not os.path.exists('test_folder'): 
  # Create the folder
  os.mkdir('test_folder') 

4.5 Delete Folder

Code
# Import the os module
import os
# Delete the folder 'test_folder'
os.rmdir('test_folder')

4.6 Delete File

Code
# Import the os module
import os
# Delete the file 'test.csv'
os.remove('test.csv')

4.7 Copy File

Code
# Import the shutil module, which provides a higher-level interface for file operations
import shutil

# Copy the file 'test.csv' to 'test2.csv'
shutil.copyfile('test.csv', 'test2.csv')

4.8 Download File Online

Code
# Import the urllib.request module for working with URLs
import urllib.request

# Define the URL of the file to download
url="https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-11/hotels.csv"

# Download the file from the URL and save it as 'hotels.csv'
urllib.request.urlretrieve(url, "hotels.csv")

5 Conditional Statements with if/elif/else

Code
# Assign values to variables a and b
a = 200
b = 33
# Check if b is greater than a
if b > a:
  # If b is greater than a, print this message
  print("b is greater than a")
# If b is not greater than a, check if a and b are equal
elif a == b:
  # If a and b are equal, print this message
  print("a and b are equal")
# If neither of the above conditions are true, execute the else block
else:
  # Print this message because a is greater than b
  print("a is greater than b")
a is greater than b

6 Loops

6.1 for Loop

Code
# Create a list of fruits
fruits = ["apple", "banana", "cherry"]
# Iterate through the fruits list
for x in fruits:
  # Print each fruit
  print(x)
apple
banana
cherry

6.2 For Enumerate Loop

It will add an index:

Code
# Create a list of fruits
fruits = ["apple", "banana", "cherry"]

# Use enumerate to get both the index and value of each fruit
list(enumerate(fruits))
[(0, 'apple'), (1, 'banana'), (2, 'cherry')]
Code
# Iterate through the fruits list with index
for index,i in enumerate(fruits):
  # Print the index
  print("The index is:",index)
  # Print the element
  print("The element is:",i)
The index is: 0
The element is: apple
The index is: 1
The element is: banana
The index is: 2
The element is: cherry

6.3 while Loop

Code
# Initialize a counter variable
i = 1
# Loop as long as i is less than 4
while i < 4:
  # Print the value of i
  print(i)
  # Increment i by 1
  i += 1
1
2
3

6.4 With a Break Statement

Code
# Initialize a counter variable
i = 1
# Loop as long as i is less than 6
while i < 6:
  # Print the value of i
  print(i)
  # If i is equal to 3, break out of the loop
  if i == 3:
    break
  # Increment i by 1
  i += 1
1
2
3

7 Functions

7.1 Without Arguments

Code
# Define a function named my_function
def my_function():
  # Print a message inside the function
  print("Hello from a function")
Code
# Call the my_function
my_function()
Hello from a function

7.2 With Arguments

Code
# Define a function named my_function that takes one argument, x
def my_function(x):
  # Print the argument x concatenated with a string of exclamation marks
  print(x + " !!!!!!!!!!!!!!!!!")
Code
# Call the my_function with the argument 'hello'
my_function('hello')
hello !!!!!!!!!!!!!!!!!

7.3 With Default Arguments

Code
# Define a function named my_function that takes one argument, x, with a default value of 'hello'
def my_function(x='hello'):
  # Print the argument x concatenated with a string of exclamation marks
  print(x + " !!!!!!!!!!!!!!!!!")
Code
# Call the my_function without any arguments, so it will use the default value for x
my_function()
hello !!!!!!!!!!!!!!!!!

7.4 Return Result

Code
# Define a function named adding_ten that takes one argument, x
def adding_ten(x):
  # Add 10 to x and store the result in a
  a=x+10
  # Return the value of a
  return(a)
Code
# Call the adding_ten function with the argument 3 and store the result in the variable result
result=adding_ten(3)
# Print the value of result
result
13

7.5 Lambda Function

It is a faster way to define a function:

Code
# Define a lambda function named lambda_adding_ten that takes one argument, x, and returns x + 10
lambda_adding_ten=lambda x:x+10
Code
# Call the lambda_adding_ten function with the argument 4
lambda_adding_ten(4)
14

8 List Comprehension

It’s a for loop in a list:

Code
# Create a list of fruits
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]

# Create a new list containing only fruits that have the letter "a" in their name
newlist = [x for x in fruits if "a" in x]

# Print the new list
print(newlist)
['apple', 'banana', 'mango']

9 Packages

9.1 Install Package

The Python Package Index (PyPI).https://pypi.org//

9.2 Check One Package Version

Code
# Import the os module
import os
# Execute the shell command 'pip show pandas' to display information about the pandas package
os.system('pip show pandas')
0

9.3 Check All Package Versions

Code
# Import the os module
import os
# Execute the shell command 'pip list' to display a list of all installed packages and their versions
os.system('pip list')
0

9.4 Check Package Installation Location

Code
import site; site.getsitepackages()

9.5 Show One Package’s Dependencies

Code
# Import the os module
import os
# Execute the shell command 'pipdeptree --packages pandas' to display the dependency tree for the pandas package
os.system('pipdeptree --packages pandas')
0

9.6 Show All Package Dependencies

Code
# Import the os module
import os
# Execute the shell command 'pipdeptree' to display the dependency tree for all installed packages
os.system('!pipdeptree')
32512

10 Check IP Location

Code
import requests

def get_location():
    response = requests.get("http://ip-api.com/json/")
    if response.status_code == 200:
        data = response.json()
        print(f"IP Address: {data['query']}")
        print(f"City: {data['city']}")
        print(f"Region: {data['regionName']}")
        print(f"Country: {data['country']}")
        print(f"ISP: {data['isp']}")
        print(f"Latitude: {data['lat']}, Longitude: {data['lon']}")
        
    else:
        print("Failed to get location")

get_location()
IP Address: 160.30.128.172
City: Sheung Shui
Region: North
Country: Hong Kong
ISP: Bgpnet Pte. Ltd.
Latitude: 22.5112, Longitude: 114.126

11 Check Internet Speed and Connection

Code
import socket

def test_connection(host, port=80, timeout=3):
    try:
        socket.setdefaulttimeout(timeout)
        socket.socket().connect((host, port))
        return True
    except Exception:
        return False

def check_sites():
    sites = ["www.baidu.com", "www.google.com"]
    print("\n🌍 Site Connectivity:")
    for site in sites:
        reachable = test_connection(site)
        status = "✅ Reachable" if reachable else "❌ Unreachable"
        print(f"  {site:<20} {status}")


check_sites()

🌍 Site Connectivity:
  www.baidu.com        ✅ Reachable
  www.google.com       ✅ Reachable
Code
import requests
import time
import requests
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


def test_download_speed(url, test_size_bytes=1_048_576):  # 1MB
    start = time.time()
    response = requests.get(url, stream=True, verify=False)
    bytes_downloaded = 0
    for chunk in response.iter_content(4096):
        bytes_downloaded += len(chunk)
        if bytes_downloaded >= test_size_bytes:
            break
    duration = time.time() - start
    mbps = (bytes_downloaded * 8) / (duration * 1_000_000)  # bits/sec -> Mbit/sec
    return mbps

def test_upload_speed(url, test_size_bytes=1_048_576):  # 1MB
    data = b'x' * test_size_bytes
    start = time.time()
    response = requests.post(url, data=data)
    duration = time.time() - start
    mbps = (test_size_bytes * 8) / (duration * 1_000_000)
    return mbps
Code
download_url = "https://www.thinkbroadband.com/download/100MB.zip"
upload_url = "http://speedtest.cc.cloudxns.net/speedtest/upload.php"
Code
print("Testing download speed...")
Testing download speed...
Code
down_speed = test_download_speed(download_url)
print(f"Download speed: {down_speed:.2f} Mbps")
Download speed: 0.08 Mbps
Code
print("Testing upload speed...")
Testing upload speed...
Code
up_speed = test_upload_speed(upload_url)
print(f"Upload speed: {up_speed:.2f} Mbps")
Upload speed: 11.00 Mbps
Back to top