Notes on Python

Chapter 1: Preparation

This lecture mainly covers lab00, which guides you through setting up your Python environment configuration.

1.Software Setup

  1. Terminal:

    Windows comes with Powershell, and I have installed Git Bash.

  2. Python:

    Python 3.11 is recommended for this course. However the Microsoft Store only provides versions 3.12 and 3.13 now. So I installed versions 3.12.(Note: The Microsoft Store version installs automatically to a protected directory, which may cause path issues later. Consider downloading the installer from python.org for more control.)

    Verrification:

    1
    2
    3
    test@▒ѩ▒ MINGW64 /e/Computer Science System Building/CS61A/lab
    $ python3 --version
    Python 3.12.10
  3. Text Editor:

    The Python interpreter is used to run Python code, but we need a text editor to write Python code.

    VS Code is recommended for this course (thought I initially expected Pycharm would be suggested…).

    Download Link

    Note: Disable Copilot! Press Ctrl+Shift+X to open the Extensions panel and search for GitHub Copilot -> click the gear -> disable feature.

2. Personalizing Your Terminal

VS Code -> Manage(gear icon in the bottom-left corner) -> Settings -> Features -> Terminal

  • [x] Enable “Integrated: Copy On Selection”
  • [x] Enable “Integrated: Cursor Blinking”
  • Set “Integrated: Cursor Style” to “line”
  • Set “Terminal › Integrated › Default Profile: Windows” to “Git Bash”

3. Implementation of lab00

  1. finish the code

    1
    2
    3
    4
    5
    6
    7
    8
    def twenty_twenty_six():
    """Come up with the most creative expression that evaluates to 2026
    using only numbers and the +, *, and - operators (or ** and % if you'd like).

    >>> twenty_twenty_six()
    2026
    """
    return 2*1000+2*10+6
  2. test the code locally

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    test@□□ѩ□□□□ MINGW64 /e/Computer Science System Building/CS61A/lab/lab00
    $ python3 -m doctest -v lab00.py
    Trying:
    twenty_twenty_six()
    Expecting:
    2026
    ok
    1 items had no tests:
    lab00
    1 items passed all tests:
    1 tests in lab00.twenty_twenty_six
    1 tests in 2 items.
    1 passed and 0 failed.
    Test passed.
  3. test by the ok program

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    test@□□ѩ□□□□ MINGW64 /e/Computer Science System Building/CS61A/lab/lab00
    $ python3 ok
    =====================================================================
    Assignment: Lab 0
    OK, version v1.18.1
    =====================================================================

    Performing authentication
    Please enter your school email (.edu):*****
    Successfully logged in as wye18280075427@gmail.com
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Running tests


    There are still locked tests! Use the -u option to unlock them
    ---------------------------------------------------------------------
    Test summary
    Locked: 1
    1 test cases passed! No cases failed.

    Backup... 100% complete
    Backup past deadline by 111 days, 6 hours, 8 minutes, and 5 seconds
    Backup successful for user: wye18280075427@gmail.com
    URL: https://okpy.org/cal/cs61a/sp26/lab00/backups/62QYM7

    OK is up to date

4. Key Takeaways

  1. Understanding the command python3 -m doctest -v lab00.py ?

python3 invokes the Python 3 interpreter

-m Tells Python to run a module as a script

doctest The built-in module for testing docstrings

-v Short for --verbose, outputs detailed test results

Chapter 2: Building Abstractions with Functions

1. Names

Expressions representing numbers may be combined with mathematical operators to form a compound expression, which the interpreter will evaluate:

1
2
3
4
5
6
>>> 42
42
>>> -1 - -1
0
>>> 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + 1/128
0.9921875

These mathematical expressions use infix notation, where the operator (e.g., +, -, *, or /) appears in between the operands (numbers).

本文链接:https://wangyier.top/Notes-on-Python/

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 The Great Library