Testing Note Formats
This commit is contained in:
parent
b3909559ae
commit
10cb611211
5 changed files with 264 additions and 1 deletions
|
@ -6,7 +6,7 @@ The interpreter supports line-editing features such as interactive editing, hist
|
|||
- 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
|
||||
> [!note] 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:
|
||||
|
|
82
Python/Lists.md
Normal file
82
Python/Lists.md
Normal file
|
@ -0,0 +1,82 @@
|
|||
Python’s list is a versatile compound data type used to group multiple values. Lists are defined with square brackets, and items are separated by commas. They can hold items of different types but typically contain items of the same type:
|
||||
```python
|
||||
>>> squares = [1, 4, 9, 16, 25]
|
||||
>>> squares
|
||||
[1, 4, 9, 16, 25]
|
||||
```
|
||||
#### Indexing and Slicing
|
||||
Lists support indexing and slicing, similar to strings:
|
||||
```python
|
||||
>>> squares[0] # First item
|
||||
1
|
||||
>>> squares[-1] # Last item
|
||||
25
|
||||
>>> squares[-3:] # Slice of last three items
|
||||
[9, 16, 25]
|
||||
```
|
||||
#### Concatenation
|
||||
Lists can be concatenated using the `+` operator:
|
||||
```python
|
||||
>>> squares + [36, 49, 64, 81, 100]
|
||||
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
|
||||
```
|
||||
#### Mutability
|
||||
Unlike strings, lists are mutable, allowing modification of their content:
|
||||
```python
|
||||
>>> cubes = [1, 8, 27, 65, 125] # Incorrect value
|
||||
>>> cubes[3] = 64 # Correcting the value
|
||||
>>> cubes
|
||||
[1, 8, 27, 64, 125]
|
||||
```
|
||||
|
||||
New items can be added using the `append()` method:
|
||||
```python
|
||||
>>> cubes.append(216) # Add cube of 6
|
||||
>>> cubes.append(7 ** 3) # Add cube of 7
|
||||
>>> cubes
|
||||
[1, 8, 27, 64, 125, 216, 343]
|
||||
```
|
||||
#### Slicing and Shallow Copies
|
||||
All slice operations return a new list containing the requested elements. This means slices create a [shallow copy](https://docs.python.org/3/library/copy.html#shallow-vs-deep-copy) of the list:
|
||||
```python
|
||||
>>> rgb = ["Red", "Green", "Blue"]
|
||||
>>> rgba = rgb[:]
|
||||
>>> rgba.append("Alpha")
|
||||
>>> rgb
|
||||
['Red', 'Green', 'Blue']
|
||||
>>> rgba
|
||||
['Red', 'Green', 'Blue', 'Alpha']
|
||||
```
|
||||
|
||||
> [!important] Additional Reading
|
||||
> Datacamp: [Python Copy List: What You Should Know](https://www.datacamp.com/tutorial/python-copy-list)
|
||||
> Geeks for Geeks: [Difference Between Deep and Shallow Copy](https://www.geeksforgeeks.org/difference-between-shallow-and-deep-copy-of-a-class/)
|
||||
#### Assigning to Slices
|
||||
Slices can be used to modify, remove, or clear items:
|
||||
```python
|
||||
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
|
||||
>>> letters[2:5] = ['C', 'D', 'E'] # Replace
|
||||
# Returns ['a', 'b', 'C', 'D', 'E', 'f', 'g']
|
||||
>>> letters[2:5] = [] # Remove
|
||||
# Returns ['a', 'b', 'f', 'g']
|
||||
>>> letters[:] = [] # Clear list
|
||||
```
|
||||
#### Length
|
||||
The `len()` function returns the number of items in a list:
|
||||
```python
|
||||
>>> letters = ['a', 'b', 'c', 'd']
|
||||
>>> len(letters)
|
||||
4
|
||||
```
|
||||
|
||||
#### Nesting
|
||||
Lists can also contain other lists (nesting):
|
||||
```python
|
||||
>>> a = ['a', 'b', 'c']
|
||||
>>> n = [1, 2, 3]
|
||||
>>> x = [a, n]
|
||||
>>> x
|
||||
[['a', 'b', 'c'], [1, 2, 3]]
|
||||
>>> x[0][1] # Access nested item
|
||||
'b'
|
||||
```
|
77
Python/Numbers.md
Normal file
77
Python/Numbers.md
Normal file
|
@ -0,0 +1,77 @@
|
|||
The Python interpreter can be used as a simple calculator, supporting arithmetic operations with operators such as `+`, `-`, `*`, and `/`. Parentheses (`()`) can be used for grouping to control the order of operations. For example:
|
||||
|
||||
```python
|
||||
>>> 2 + 2
|
||||
4
|
||||
>>> (50 - 5 * 6) / 4
|
||||
5.0
|
||||
>>> 8 / 5 # Division always returns a float
|
||||
1.6
|
||||
```
|
||||
|
||||
Python recognizes two primary numeric types:
|
||||
- **Integers (`int`)**: Whole numbers, such as `2`, `4`, and `20`.
|
||||
- **Floating-point numbers (`float`)**: Numbers with fractional parts, such as `5.0` and `1.6`.
|
||||
### Division and Remainders
|
||||
- Division (`/`) always produces a float result.
|
||||
- Floor division (`//`) discards the fractional part and returns an integer.
|
||||
- The modulo operator (`%`) calculates the remainder of a division:
|
||||
```python
|
||||
>>> 17 / 3 # Classic division
|
||||
5.666666666666667
|
||||
>>> 17 // 3 # Floor division
|
||||
5
|
||||
>>> 17 % 3 # Modulo
|
||||
2
|
||||
>>> 5 * 3 + 2 # Floored quotient * divisor + remainder
|
||||
17
|
||||
```
|
||||
### Exponentiation
|
||||
The `**` operator calculates powers:
|
||||
```python
|
||||
>>> 5 ** 2 # 5 squared
|
||||
25
|
||||
>>> 2 ** 7 # 2 raised to the power of 7
|
||||
128
|
||||
```
|
||||
### Variables and Assignments
|
||||
The equal sign (**=**) assigns values to variables. No output is displayed during an assignment:
|
||||
```python
|
||||
>>> width = 20
|
||||
>>> height = 5 * 9
|
||||
>>> width * height
|
||||
900
|
||||
```
|
||||
|
||||
Using an undefined variable results in an error:
|
||||
```python
|
||||
>>> n # Undefined variable
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
NameError: name 'n' is not defined
|
||||
```
|
||||
### Mixed Types and Floating-Point Arithmetic
|
||||
Operators with mixed-type operands convert integers to floats:
|
||||
```python
|
||||
>>> 4 * 3.75 - 1
|
||||
14.0
|
||||
```
|
||||
### The `_` Variable
|
||||
In interactive mode, the last printed expression is automatically assigned to the `_` variable, which simplifies calculations:
|
||||
```python
|
||||
>>> tax = 12.5 / 100
|
||||
>>> price = 100.50
|
||||
>>> price * tax
|
||||
12.5625
|
||||
>>> price + _
|
||||
113.0625
|
||||
>>> round(_, 2)
|
||||
113.06
|
||||
```
|
||||
This variable should be treated as read-only to avoid overwriting its special behavior.
|
||||
### Additional Numeric Types
|
||||
In addition to `int` and `float`, Python supports:
|
||||
- `Decimal` and `Fraction` for precise numerical representations.
|
||||
- `Complex numbers` using the `j` or `J` suffix for the imaginary part (e.g., `3+5j`).
|
||||
|
||||
[3.1.1 Numbers](https://docs.python.org/3/tutorial/introduction.html#numbers)
|
96
Python/Text.md
Normal file
96
Python/Text.md
Normal file
|
@ -0,0 +1,96 @@
|
|||
Python can manipulate text using the `str` type, representing characters, words, sentences, and more. Strings can be enclosed in single quotes (`'...'`) or double quotes (`"..."`) with the same result:
|
||||
```python
|
||||
>>> 'spam eggs' # Single quotes
|
||||
'spam eggs'
|
||||
>>> "Got your back! :) Yay!" # Double quotes
|
||||
'Got your back! :) Yay!'
|
||||
>>> '1975' # Digits enclosed in quotes are strings
|
||||
'1975'
|
||||
```
|
||||
### Quoting and Escaping
|
||||
To include quotes inside strings, either escape them with a backslash (`\`) or use the other type of quotation mark:
|
||||
```python
|
||||
>>> 'doesn\'t' # Escape single quote
|
||||
"doesn't"
|
||||
>>> "\"Yes,\" they said." # Escape double quotes
|
||||
'"Yes," they said.'
|
||||
```
|
||||
### Special Characters and Raw Strings
|
||||
Special characters, such as `\n` (newline), are displayed literally without `print()` but interpreted when printed:
|
||||
```python
|
||||
>>> s = 'First line.\nSecond line.'
|
||||
>>> s # Without print()
|
||||
'First line.\nSecond line.'
|
||||
>>> print(s) # With print()
|
||||
First line.
|
||||
Second line.
|
||||
```
|
||||
|
||||
Raw strings, prefixed with `r`, prevent special characters from being interpreted:
|
||||
```python
|
||||
>>> print(r'C:\some\name') # Raw string
|
||||
C:\some\name
|
||||
```
|
||||
### Multiline Strings
|
||||
Triple quotes (`"""..."""` or `'''...'''`) can define multiline strings, with end-of-lines automatically included:
|
||||
```python
|
||||
>>> print("""\
|
||||
Usage: thingy [OPTIONS]
|
||||
-h Display this usage message
|
||||
-H Hostname to connect to
|
||||
""")
|
||||
Usage: thingy [OPTIONS]
|
||||
-h Display this usage message
|
||||
-H Hostname to connect to
|
||||
```
|
||||
### Concatenation and Repetition
|
||||
Strings can be concatenated using `+` or repeated with `*`:
|
||||
```python
|
||||
>>> 'Py' + 'thon'
|
||||
'Python'
|
||||
>>> 3 * 'un' + 'ium'
|
||||
'unununium'
|
||||
```
|
||||
### Indexing and Slicing
|
||||
Strings are immutable and can be indexed or sliced:
|
||||
```python
|
||||
>>> word = 'Python'
|
||||
>>> word[0] # First character
|
||||
'P'
|
||||
>>> word[-1] # Last character
|
||||
'n'
|
||||
>>> word[0:2] # Slice from position 0 to 2 (exclusive)
|
||||
'Py'
|
||||
>>> word[4:] # Slice from position 4 to the end
|
||||
'on'
|
||||
```
|
||||
### Length and Immutability
|
||||
The `len()` function returns the length of a string:
|
||||
```python
|
||||
>>> len('supercalifragilisticexpialidocious')
|
||||
34
|
||||
```
|
||||
|
||||
Strings cannot be changed. To create a new string, use concatenation:
|
||||
```python
|
||||
>>> 'J' + word[1:]
|
||||
'Jython'
|
||||
>>> word[:2] + 'py'
|
||||
'Pypy'
|
||||
```
|
||||
|
||||
> [!tip] See also...
|
||||
> [Text Sequence Type — str](https://docs.python.org/3/library/stdtypes.html#textseq)
|
||||
> - Strings are examples of _sequence types_, and support the common operations supported by such types.
|
||||
>
|
||||
> [String Methods](https://docs.python.org/3/library/stdtypes.html#string-methods)
|
||||
> - Strings support a large number of methods for basic transformations and searching.
|
||||
>
|
||||
> [f-strings](https://docs.python.org/3/reference/lexical_analysis.html#f-strings)
|
||||
> - String literals that have embedded expressions.
|
||||
>
|
||||
> [Format String Syntax](https://docs.python.org/3/library/string.html#formatstrings)
|
||||
> - Information about string formatting with [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format "str.format").
|
||||
>
|
||||
> [printf-style String Formatting](https://docs.python.org/3/library/stdtypes.html#old-string-formatting)
|
||||
> - The old formatting operations invoked when strings are the left operand of the `%` operator are described in more detail here.
|
8
Python/Using Python as a Calculator.md
Normal file
8
Python/Using Python as a Calculator.md
Normal file
|
@ -0,0 +1,8 @@
|
|||
Python can be used as a powerful calculator, enabling arithmetic, textual, and compound operations. The interactive mode allows users to test and execute commands on-the-fly, making it ideal for experimenting with Python’s capabilities.
|
||||
|
||||
This section includes the following subtopics:
|
||||
- [Numbers](./Numbers.md): Arithmetic operations, numeric types, and the use of `_` for intermediate results.
|
||||
- [Text](./Text.md): String manipulation using slicing, indexing, and special formatting methods.
|
||||
- [Lists](./Lists.md): Python’s versatile compound data type for managing collections of items.
|
||||
|
||||
These concepts build a foundational understanding of Python’s data types and their interactive capabilities.
|
Loading…
Reference in a new issue