Updated Note Formatting to be more Atomic
This commit is contained in:
parent
a1da61e826
commit
b3909559ae
4 changed files with 66 additions and 75 deletions
|
@ -1,17 +1,10 @@
|
|||
## What is it?
|
||||
Argument passing refers to how Python processes the script name and additional arguments provided during execution. These are stored as a list of strings in the `argv` variable within the `sys` module. This is a sub concept of [Invoking the Interpreter](./Invoking%20the%20Interpreter.md).
|
||||
## How to Use It:
|
||||
1. **Behavior of `sys.argv`**:
|
||||
- The script name and additional arguments are stored in `sys.argv` as a list of strings.
|
||||
- The length of the list is at least one:
|
||||
- When no script or arguments are provided, `sys.argv[0]` is an empty string.
|
||||
- When the script name is `'-'` (standard input), `sys.argv[0]` is set to `'-'`.
|
||||
- When using [`-c command`](https://docs.python.org/3/using/cmdline.html#cmdoption-c), `sys.argv[0]` is set to `'-c'`.
|
||||
- When using [`-m module`](https://docs.python.org/3/using/cmdline.html#cmdoption-m), `sys.argv[0]` is set to the full name of the located module.
|
||||
2. **Handling of Arguments After `-c` or `-m`**:
|
||||
- Options following `-c command` or `-m module`
|
||||
- These options remain in `sys.argv` for the command or module to handle.
|
||||
## References:
|
||||
- Python Official Docs: [Command Line Option -c](https://docs.python.org/3/using/cmdline.html#cmdoption-c)
|
||||
- Python Official Docs: [Command Line Option -m](https://docs.python.org/3/using/cmdline.html#cmdoption-m)
|
||||
- Python Official Docs: [2.1.1 Argument Passing](https://docs.python.org/3/tutorial/interpreter.html#argument-passing)
|
||||
Argument passing refers to how Python processes the script name and additional arguments provided during execution. These are stored as a list of strings in the `argv` variable within the `sys` module. This is a sub concept of [Invoking the Interpreter](./Invoking%20the%20Interpreter.md).
|
||||
|
||||
The script name and additional arguments are stored in `sys.argv` as a list of strings. The length of the list is always at least one. When no script or arguments are provided, `sys.argv[0]` is an empty string. If the script name is `'-'` (indicating standard input), `sys.argv[0]` is set to `'-'`. When using [`-c command`](https://docs.python.org/3/using/cmdline.html#cmdoption-c), `sys.argv[0]` is set to `'-c'`. When using [`-m module`](https://docs.python.org/3/using/cmdline.html#cmdoption-m), `sys.argv[0]` is set to the full name of the located module.
|
||||
|
||||
Options following `-c command` or `-m module` are not processed as Python interpreter options. Instead, they remain in `sys.argv` for the command or module to handle.
|
||||
|
||||
Explore these resources for further clarification:
|
||||
- [Command Line Option -c](https://docs.python.org/3/using/cmdline.html#cmdoption-c)
|
||||
- [Command Line Option -m](https://docs.python.org/3/using/cmdline.html#cmdoption-m)
|
||||
- [Argument Passing](https://docs.python.org/3/tutorial/interpreter.html#argument-passing)
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
### What is it?
|
||||
Interactive mode in Python is a mode where the interpreter reads commands directly from a terminal (tty). It is designed for testing and immediate execution of commands. When Python starts in this mode, it displays a welcome message that includes the version number and prompts the user for input. The interactive mode is part of the broader functionality of the Python Interpreter, which is detailed in [Invoking the Interpreter](./Invoking%20the%20Interpreter.md).
|
||||
|
||||
Interactive mode in Python is a mode where the interpreter reads commands directly from a terminal (tty). It is designed for testing and immediate execution of commands. When Python starts in this mode, it displays a welcome message that includes the version number and prompts the user for input. The interactive mode is part of the broader functionality of the Python Interpreter, which is detailed in [Invoking the Interpreter](./Invoking%20the%20Interpreter.md)
|
||||
### How to Use It:
|
||||
#### Starting Interactive Mode
|
||||
- Start Python in interactive mode by typing `python` or `python3.13` (for Python 3.13) in a terminal.
|
||||
- The interpreter prompts with `>>>` (primary prompt) for a new command or `...` (secondary prompt) for continuation lines in multi-line constructs.
|
||||
#### Example Session
|
||||
To start Python in interactive mode, type `python` or `python3.13` (for Python 3.13) in a terminal. The interpreter prompts with `>>>` (primary prompt) for a new command or `...` (secondary prompt) for continuation lines in multi-line constructs.
|
||||
|
||||
For example:
|
||||
```plaintext
|
||||
python3.13
|
||||
Python 3.13 (default, April 4 2023, 09:25:04)
|
||||
|
@ -14,8 +10,8 @@ Python 3.13 (default, April 4 2023, 09:25:04)
|
|||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>>
|
||||
```
|
||||
#### Example of a Multi-Line Construct
|
||||
|
||||
An example of a multi-line construct:
|
||||
```python
|
||||
>>> the_world_is_flat = True
|
||||
>>> if the_world_is_flat:
|
||||
|
@ -23,14 +19,13 @@ Type "help", "copyright", "credits" or "license" for more information.
|
|||
...
|
||||
Be careful not to fall off!
|
||||
```
|
||||
#### Exiting Interactive Mode
|
||||
- Type `exit()` or press `Ctrl+D` (Linux/Mac) or `Ctrl+Z` (Windows) to leave interactive mode.
|
||||
### Insights:
|
||||
#### Key Features of Interactive Mode
|
||||
|
||||
1. **Primary Prompt (`>>>`)**: Used for new commands.
|
||||
2. **Secondary Prompt (`...`)**: Used for continuation lines in constructs like loops or conditionals.
|
||||
3. **Welcome Message**: Displays Python version and other information before the first prompt.
|
||||
4. **Dynamic Testing**: Ideal for quick testing and debugging small code snippets.
|
||||
### References:
|
||||
- Python Official Docs: [Interactive Mode](https://docs.python.org/3/tutorial/appendix.html#tut-interac)
|
||||
To exit interactive mode, type `exit()` or press `Ctrl+D` (Linux/Mac) or `Ctrl+Z` (Windows).
|
||||
|
||||
Interactive mode provides several key features:
|
||||
- The **primary prompt** (`>>>`) is used for new commands.
|
||||
- The **secondary prompt** (`...`) is used for continuation lines in constructs like loops or conditionals.
|
||||
- The **welcome message** displays Python version information before the first prompt.
|
||||
- It is ideal for quick testing and debugging small code snippets.
|
||||
|
||||
[Interactive Mode](https://docs.python.org/3/tutorial/appendix.html#tut-interac)
|
||||
|
|
|
@ -1,40 +1,22 @@
|
|||
```table-of-contents
|
||||
title:
|
||||
style: nestedList # TOC style (nestedList|nestedOrderedList|inlineFirstLevel)
|
||||
minLevel: 0 # Include headings from the specified level
|
||||
maxLevel: 0 # Include headings up to the specified level
|
||||
includeLinks: true # Make headings clickable
|
||||
debugInConsole: false # Print debug info in Obsidian console
|
||||
```
|
||||
## What is it?
|
||||
The Python interpreter is a program that executes Python code. It can be run interactively or by executing scripts, with various command-line options available to customize its behavior.
|
||||
## How to Use It:
|
||||
1. **Location of the Interpreter**:
|
||||
- On Unix, it is often installed as `/usr/local/bin/python3.13`. Adding `/usr/local/bin` to the Unix shell’s search path enables you to invoke it with:
|
||||
```
|
||||
python3.13
|
||||
```
|
||||
- On Windows:
|
||||
- If installed from the Microsoft Store: Use `python3.13`.
|
||||
- If the `py.exe` launcher is installed: Use `py`.
|
||||
- See [Excursus: Setting environment variables](https://docs.python.org/3/using/windows.html#setting-envvars) for other ways to launch Python.
|
||||
1. **Exiting the Interpreter**:
|
||||
- Use `Control-D` (Unix) or `Control-Z` (Windows) to exit with a zero exit status.
|
||||
- Alternatively, type `quit()` to exit.
|
||||
2. **Line-Editing Features**:
|
||||
- Includes interactive editing, history substitution, and code completion (requires GNU Readline library).
|
||||
- To check if command-line editing is supported, type `Control-P`:
|
||||
- If it beeps, command-line editing is available.
|
||||
- If `^P` is echoed or nothing happens, it isn’t supported.
|
||||
> [!info] Personal Insight
|
||||
> On my [Lenovo Legion 5](https://www.bestbuy.com/site/lenovo-legion-5-15-gaming-laptop-amd-ryzen-7-5800h-nvidia-geforce-rtx-3050-ti-8gb-memory-512gb-ssd-phantom-blue/6455136.p?skuId=6455136), purchased in July 2022 and still performing great as of today, I verified that line-editing was enabled. Using the [Windows Terminal app](https://www.microsoft.com/store/productId/9N0DX20HK701?ocid=pdpshare) as my terminal, I ran a command, `print("Hi!")`, pressed `Enter`, held the `Ctrl` key, and tapped `P`. The previous command appeared in the prompt.
|
||||
1. **Modes of Operation**:
|
||||
- **Interactive Mode**: Reads and executes commands interactively when standard input is connected to a tty device. See [Interactive Mode](./Interactive%20Mode.md) for details.
|
||||
- **Script Mode**: Executes a script when a file name is provided as an argument or via standard input.
|
||||
2. **Command-Line Options**:
|
||||
- **`-c command [arg] ...`**: Executes the specified statements in the command string. For details on passing arguments, see [Argument Passing](./Argument%20Passing.md).
|
||||
- **`-m module [arg] ...`**: Executes a module as a script by its full name.
|
||||
- **`-i`**: Runs a script and then enters interactive mode.
|
||||
## References:
|
||||
- Python Official Documentation: [Command line and environment](https://docs.python.org/3/using/cmdline.html)
|
||||
- Python Official Documentation: [2.1 Invoking the Interpreter](https://docs.python.org/3/tutorial/interpreter.html#invoking-the-interpreter)
|
||||
The Python interpreter is a program that executes Python code. It can be run interactively or by executing scripts, with various command-line options available to customize its behavior. On Unix, it is often installed as `/usr/local/bin/python3.13`. Adding `/usr/local/bin` to the Unix shell’s search path enables you to invoke it with `python3.13`. On Windows, if installed from the Microsoft Store, you can use `python3.13`, or if the `py.exe` launcher is installed, you can use `py`. For additional ways to launch Python, see [Excursus: Setting environment variables](https://docs.python.org/3/using/windows.html#setting-envvars).
|
||||
|
||||
To exit the interpreter, use `Control-D` on Unix or `Control-Z` on Windows for a zero exit status. Alternatively, type `quit()` to exit.
|
||||
|
||||
The interpreter supports line-editing features such as interactive editing, history substitution, and code completion, provided the GNU Readline library is supported. To test if command-line editing is enabled, type `Control-P`:
|
||||
- If it beeps, command-line editing is available.
|
||||
- If `^P` is echoed or nothing happens, it is not available.
|
||||
|
||||
> [!todo] My verified method for Command-Line Editing
|
||||
> On my [Lenovo Legion 5](https://www.bestbuy.com/site/lenovo-legion-5-15-gaming-laptop-amd-ryzen-7-5800h-nvidia-geforce-rtx-3050-ti-8gb-memory-512gb-ssd-phantom-blue/6455136.p?skuId=6455136), I verified that line-editing was enabled by using the [Windows Terminal app](https://www.microsoft.com/store/productId/9N0DX20HK701?ocid=pdpshare) as my terminal. I ran a command, `print("Hi!")`, pressed `Enter`, held the `Ctrl` key, and tapped `P`. The previous command appeared in the prompt and allows editing.
|
||||
|
||||
The interpreter operates in two modes:
|
||||
- **Interactive Mode**: Reads and executes commands interactively when standard input is connected to a tty device. See [Interactive Mode](./Interactive%20Mode.md) for more details.
|
||||
- **Script Mode**: Executes a script when a file name is provided as an argument or via standard input.
|
||||
|
||||
The interpreter also supports various command-line options:
|
||||
- `-c command [arg] ...`: Executes the specified statements in the command string. For details on passing arguments, see [Argument Passing](./Argument%20Passing.md).
|
||||
- `-m module [arg] ...`: Executes a module as a script by its full name.
|
||||
- `-i`: Runs a script and then enters interactive mode.
|
||||
|
||||
[Command Line and Environment](https://docs.python.org/3/using/cmdline.html)
|
||||
[Invoking the Interpreter](https://docs.python.org/3/tutorial/interpreter.html#invoking-the-interpreter)
|
21
Python/Source Code Encoding.md
Normal file
21
Python/Source Code Encoding.md
Normal file
|
@ -0,0 +1,21 @@
|
|||
Python source files are treated as UTF-8 encoded by default. This encoding supports most world languages for string literals, identifiers, and comments. However, the standard library uses ASCII characters for identifiers, a convention that portable code should follow. To ensure compatibility, your editor must recognize UTF-8 and use a font that supports all characters in the file.
|
||||
|
||||
If a different encoding is needed, it can be declared by adding a special comment as the first line of the file:
|
||||
```python
|
||||
# -*- coding: encoding -*-
|
||||
```
|
||||
|
||||
Replace `encoding` with a valid codec supported by Python, such as `cp1252`. For example:
|
||||
```python
|
||||
# -*- coding: cp1252 -*-
|
||||
```
|
||||
|
||||
When the file starts with a UNIX shebang line (e.g., `#!/usr/bin/env python3`), the encoding declaration should be on the second line:
|
||||
```python
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: cp1252 -*-
|
||||
```
|
||||
|
||||
UTF-8 ensures broad compatibility across languages and platforms, making it the recommended default for most scenarios. Declaring an explicit encoding can help prevent issues when working with files that use non-UTF-8 encodings.
|
||||
|
||||
[Encoding Declarations](https://docs.python.org/3/reference/lexical_analysis.html#encoding-declarations).
|
Loading…
Reference in a new issue