Initial commit: Vault setup with .gitignore
This commit is contained in:
commit
f68821a4f3
142 changed files with 77519 additions and 0 deletions
|
@ -0,0 +1,34 @@
|
|||
---
|
||||
title: arithmeticOperators
|
||||
tags:
|
||||
- python
|
||||
- math
|
||||
- operators
|
||||
- syntax
|
||||
aliases:
|
||||
- math operators
|
||||
- arithmetic operations
|
||||
- python math
|
||||
---
|
||||
|
||||
> [!note] Math Operators in Python
|
||||
> Python supports standard arithmetic operations:
|
||||
>
|
||||
> > [!example] Operator Table
|
||||
> > | **Operator** | **Description** | **Example** | **Result** |
|
||||
> > |--------------|-----------------------|--------------|------------|
|
||||
> > | `+` | Addition | `2 + 3` | `5` |
|
||||
> > | `-` | Subtraction | `5 - 2` | `3` |
|
||||
> > | `*` | Multiplication | `2 * 3` | `6` |
|
||||
> > | `/` | Division (float) | `5 / 2` | `2.5` |
|
||||
> > | `//` | Floor Division | `5 // 2` | `2` |
|
||||
> > | `%` | Modulo (remainder) | `5 % 2` | `1` |
|
||||
> > | `**` | Exponentiation | `2 ** 3` | `8` |
|
||||
> >
|
||||
> > [!info]- 🔗 Related Notes
|
||||
> > - [[numericTypes]] — explains how `int`, `float`, and `complex` values behave
|
||||
> > - [[mixedTypeArithmetic]] — details what happens when combining types like `int + float`
|
||||
> > - [[operatorPrecedence]] — explains the order in which operators are evaluated
|
||||
> > - [[typeConversions]] — shows how conversions affect arithmetic results
|
||||
> > - [[pythonSyntaxOverview]] — broad context for where arithmetic fits in Python basics
|
||||
>
|
48
20-29 Areas/20 Programming/20.01 Python/booleans.md
Normal file
48
20-29 Areas/20 Programming/20.01 Python/booleans.md
Normal file
|
@ -0,0 +1,48 @@
|
|||
---
|
||||
title: booleans
|
||||
tags:
|
||||
- python
|
||||
- bool
|
||||
- logic
|
||||
- expressions
|
||||
aliases:
|
||||
- true
|
||||
- false
|
||||
- boolean
|
||||
- bools
|
||||
---
|
||||
|
||||
> [!note] Concept
|
||||
> Python uses the `bool` type to represent truth values. It has only two values: `True` and `False`, both of which are subtypes of `int`.
|
||||
>
|
||||
> > [!example] Assigning Boolean Values
|
||||
> > ```python
|
||||
> > true_variable = True
|
||||
> > false_variable = False
|
||||
> > ```
|
||||
>
|
||||
> > [!example] Boolean Expressions
|
||||
> > ```python
|
||||
> > result = True and True # True
|
||||
> > result = True and False # False
|
||||
> > result = False or True # True
|
||||
> > result = False or False # False
|
||||
> > result = not False # True
|
||||
> > result = not True # False
|
||||
> > ```
|
||||
>
|
||||
> > [!important] Best Practices
|
||||
> > - `True` and `False` must be capitalized.
|
||||
> > - Use boolean logic to control flow and conditionals.
|
||||
> > - Booleans are a subtype of `int`:
|
||||
> > ```python
|
||||
> > int(True) # 1
|
||||
> > int(False) # 0
|
||||
> > ```
|
||||
> >
|
||||
> > [!info]- 🔗 Related Notes
|
||||
> > - [[logicalOperators]] — details `and`, `or`, `not` in more depth
|
||||
> > - [[conditionals]] — where booleans are used for flow control
|
||||
> > - [[typeConversions]] — shows how other types convert to `bool`
|
||||
> > - [[numericTypes]] — relevant because `bool` is a subtype of `int`
|
||||
>
|
32
20-29 Areas/20 Programming/20.01 Python/comments.md
Normal file
32
20-29 Areas/20 Programming/20.01 Python/comments.md
Normal file
|
@ -0,0 +1,32 @@
|
|||
---
|
||||
title: "Comments"
|
||||
tags: [python, syntax, comments]
|
||||
aliases: [hash, commenting, commented-out code]
|
||||
---
|
||||
> [!note] What Are Comments?
|
||||
> Comments are lines in your code that start with a `#`. Python completely ignores them during execution.
|
||||
>
|
||||
> > [!example] Example
|
||||
> > ```python
|
||||
> > # This will not run
|
||||
> > print('This will run!')
|
||||
> > ```
|
||||
>
|
||||
> > [!info] Spacing and Readability
|
||||
> > - Blank lines are also ignored by Python.
|
||||
> > - Use blank lines and comments to group related code — just like paragraphs in writing.
|
||||
> > - Helps you (and others) read your code more easily later.
|
||||
>
|
||||
> > [!important] Best Practices
|
||||
> > - Use comments to **explain why**, not just **what** the code does.
|
||||
> > - Avoid redundant comments:
|
||||
> > ```python
|
||||
> > x = 5 # set x to 5 ❌ unnecessary
|
||||
> > ```
|
||||
> > - Use inline comments sparingly and only when clarity is needed.
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > - [[pythonSyntaxOverview]] — comments are part of Python's syntax rules
|
||||
> > - [[variableNamingBestPractices]] — both promote writing readable, self-explanatory code
|
||||
> > - [[conditionals]] — comments are often used to clarify branches or logic
|
||||
>
|
23
20-29 Areas/20 Programming/20.01 Python/dataTypes.md
Normal file
23
20-29 Areas/20 Programming/20.01 Python/dataTypes.md
Normal file
|
@ -0,0 +1,23 @@
|
|||
---
|
||||
title: dataTypes
|
||||
tags:
|
||||
- python
|
||||
- data-types
|
||||
- basics
|
||||
aliases:
|
||||
- types
|
||||
- python-types
|
||||
---
|
||||
|
||||
> [!note] Common Python Data Types
|
||||
> | **Data Type** | **Description** | **Example Values** |
|
||||
> |-------------------------|---------------------|----------------------------------|
|
||||
> | `int` (Integer) | Whole numbers | `-2`, `0`, `3`, `42` |
|
||||
> | `float` (Floating-Point)| Decimal numbers | `-1.25`, `0.0`, `2.718` |
|
||||
> | `str` (String) | Text and characters | `"a"`, `"Hello!"`, `"11 cats"` |
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[numericTypes]]
|
||||
> > [[typeConversions]]
|
||||
> > [[booleans]]
|
||||
> > [[pythonSyntaxOverview]]
|
37
20-29 Areas/20 Programming/20.01 Python/docStrings.md
Normal file
37
20-29 Areas/20 Programming/20.01 Python/docStrings.md
Normal file
|
@ -0,0 +1,37 @@
|
|||
---
|
||||
title: docstrings
|
||||
tags:
|
||||
- python
|
||||
- functions
|
||||
- comments
|
||||
- documentation
|
||||
aliases:
|
||||
- triple quotes
|
||||
- function docs
|
||||
---
|
||||
|
||||
> [!note] Concept
|
||||
> A **docstring** is a special string that documents a function, class, or module. It should be the first statement inside a function body and use triple double-quotes.
|
||||
>
|
||||
> > [!example] Multi-line Docstring
|
||||
> > ```python
|
||||
> > def complex(real=0.0, imag=0.0):
|
||||
> > """Form a complex number.
|
||||
> >
|
||||
> > Keyword arguments:
|
||||
> > real -- the real part (default 0.0)
|
||||
> > imag -- the imaginary part (default 0.0)
|
||||
> > """
|
||||
> > return complex(real, imag)
|
||||
> > ```
|
||||
>
|
||||
> > [!important] Best Practices
|
||||
> > - Use triple double quotes: `""" ... """`
|
||||
> > - Keep it concise and clear — first line should summarize purpose.
|
||||
> > - Follow [PEP 257](https://peps.python.org/pep-0257/) for conventions.
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[functions]]
|
||||
> > [[comments]]
|
||||
> > [[pythonSyntaxOverview]]
|
||||
>
|
30
20-29 Areas/20 Programming/20.01 Python/expressions.md
Normal file
30
20-29 Areas/20 Programming/20.01 Python/expressions.md
Normal file
|
@ -0,0 +1,30 @@
|
|||
---
|
||||
title: expressions
|
||||
tags:
|
||||
- python
|
||||
- expressions
|
||||
- syntax
|
||||
aliases:
|
||||
- expression
|
||||
- evaluate
|
||||
---
|
||||
|
||||
> [!note] Concept
|
||||
> An **expression** is a combination of **values**, **variables**, and **operators** that evaluates to a single value.
|
||||
>
|
||||
> > [!example] Examples
|
||||
> > ```python
|
||||
> > 2 + 3 # 5
|
||||
> > 'a' * 3 # 'aaa'
|
||||
> > myAge + 1 # if myAge is an integer
|
||||
> > ```
|
||||
>
|
||||
> > [!important] Best Practices
|
||||
> > - Expressions don’t store data — they compute and return a value.
|
||||
> > - You can use expressions inside function calls, assignments, etc.
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[arithmeticOperators]]
|
||||
> > [[booleans]]
|
||||
> > [[typeConversions]]
|
||||
> > [[pythonSyntaxOverview]]
|
39
20-29 Areas/20 Programming/20.01 Python/functions.md
Normal file
39
20-29 Areas/20 Programming/20.01 Python/functions.md
Normal file
|
@ -0,0 +1,39 @@
|
|||
---
|
||||
title: functions
|
||||
tags:
|
||||
- python
|
||||
- functions
|
||||
- def
|
||||
aliases:
|
||||
- function definition
|
||||
- function syntax
|
||||
- def
|
||||
---
|
||||
|
||||
> [!note] Function Basics
|
||||
> A function in Python is defined using the `def` keyword, followed by a name, parameters in parentheses, and a colon. The body of the function must be indented.
|
||||
>
|
||||
> > [!example] Defining and Calling Functions
|
||||
> > ```python
|
||||
> > def add(a, b):
|
||||
> > return a + b
|
||||
> >
|
||||
> > print(add(3, 4)) # 7
|
||||
> > ```
|
||||
>
|
||||
> > [!important] Indentation Matters
|
||||
> > Python uses **significant indentation**. Inconsistent spacing will raise an `IndentationError`.
|
||||
>
|
||||
> > [!example] Printing vs Returning
|
||||
> > ```python
|
||||
> > def add_two_nums(num_one, num_two):
|
||||
> > total = num_one + num_two
|
||||
> > print(total)
|
||||
> >
|
||||
> > add_two_nums(32512, 325235623) # 325268135
|
||||
> > ```
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[expressions]]
|
||||
> > [[docstrings]]
|
||||
> > [[pythonSyntaxOverview]]
|
32
20-29 Areas/20 Programming/20.01 Python/input().md
Normal file
32
20-29 Areas/20 Programming/20.01 Python/input().md
Normal file
|
@ -0,0 +1,32 @@
|
|||
---
|
||||
title: input()
|
||||
tags:
|
||||
- python
|
||||
- functions
|
||||
- input
|
||||
aliases:
|
||||
- input
|
||||
- input-function
|
||||
- user-input
|
||||
---
|
||||
|
||||
> [!note] Concept
|
||||
> The `input()` function waits for the user to type text and press `ENTER`, then returns it as a string.
|
||||
>
|
||||
> > [!example] Example
|
||||
> > ```python
|
||||
> > myName = input()
|
||||
> > ```
|
||||
> > If the user types `Al`, the result is equivalent to:
|
||||
> > ```python
|
||||
> > myName = 'Al'
|
||||
> > ```
|
||||
>
|
||||
> > [!important] Best Practices
|
||||
> > - Always assume `input()` returns a string — cast with `int()`, `float()`, etc. if needed.
|
||||
> > - Only use `input()` in Python 3. In Python 2, use `raw_input()` instead.
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[typeConversions]]
|
||||
> > [[functions]]
|
||||
> > [[pythonSyntaxOverview]]
|
39
20-29 Areas/20 Programming/20.01 Python/len().md
Normal file
39
20-29 Areas/20 Programming/20.01 Python/len().md
Normal file
|
@ -0,0 +1,39 @@
|
|||
---
|
||||
title: len()
|
||||
tags:
|
||||
- python
|
||||
- functions
|
||||
- strings
|
||||
aliases:
|
||||
- len
|
||||
- length
|
||||
- string-length
|
||||
---
|
||||
|
||||
> [!note] Concept
|
||||
> The `len()` function returns the number of characters in a string (including spaces and punctuation).
|
||||
>
|
||||
> > [!example] Example
|
||||
> > ```python
|
||||
> > len('hello') # 5
|
||||
> > len('My very energetic monster just scarfed nachos.') # 46
|
||||
> > len('') # 0
|
||||
> >
|
||||
> > name = "Alice"
|
||||
> > print("The length of your name is:")
|
||||
> > print(len(name)) # 5
|
||||
> > ```
|
||||
>
|
||||
> > [!important] Best Practices
|
||||
> > - `len()` always returns an integer.
|
||||
> > - You must convert the result to a string when concatenating:
|
||||
> > ```python
|
||||
> > print("I am " + str(29) + " years old.") # OK
|
||||
> > ```
|
||||
> > - Mixing integers and strings with `+` will raise a `TypeError`.
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[functions]]
|
||||
> > [[typeConversions]]
|
||||
> > [[expressions]]
|
||||
> > [[pythonSyntaxOverview]]
|
|
@ -0,0 +1,35 @@
|
|||
---
|
||||
title: mixedTypeArithmetic
|
||||
tags:
|
||||
- python
|
||||
- math
|
||||
- float
|
||||
- int
|
||||
- type-coercion
|
||||
aliases:
|
||||
- mixed type math
|
||||
- int float math
|
||||
- arithmetic between types
|
||||
---
|
||||
|
||||
> [!note] Arithmetic Between Types
|
||||
> Python allows arithmetic between `int` and `float`:
|
||||
> - `int` is automatically **converted to float** when mixed
|
||||
> - `/` always returns a `float`, even if both operands are `int`
|
||||
> - `//` performs floor division (returns `int` or `float`)
|
||||
> - `%` gives the remainder of division
|
||||
>
|
||||
> > [!example] Mixed Arithmetic Examples
|
||||
> > ```python
|
||||
> > 3 + 4.0 # 7.0
|
||||
> > 6 / 2 # 3.0
|
||||
> > 7 // 4 # 1
|
||||
> > 7 % 4 # 3
|
||||
> > 12.75 % 3 # 0.75
|
||||
> > ```
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[numericTypes]]
|
||||
> > [[arithmeticOperators]]
|
||||
> > [[typeConversions]]
|
||||
> > [[pythonSyntaxOverview]]
|
33
20-29 Areas/20 Programming/20.01 Python/numericTypes.md
Normal file
33
20-29 Areas/20 Programming/20.01 Python/numericTypes.md
Normal file
|
@ -0,0 +1,33 @@
|
|||
---
|
||||
title: numericTypes
|
||||
tags:
|
||||
- python
|
||||
- numbers
|
||||
- data-types
|
||||
- int
|
||||
- float
|
||||
- complex
|
||||
aliases:
|
||||
- numeric types
|
||||
- number types
|
||||
- python numbers
|
||||
- integer
|
||||
- decimal
|
||||
---
|
||||
|
||||
> [!note] Python Numeric Types
|
||||
> Python includes three primary numeric types:
|
||||
> - `int` — whole numbers like `1234`, `-10`, `0`
|
||||
> - `float` — decimal numbers like `3.14`, `-9.01`, `0.0`
|
||||
> - `complex` — imaginary numbers like `2 + 3j` *(rare for beginners)*
|
||||
>
|
||||
> Additional info:
|
||||
> - `int` supports **arbitrary precision**
|
||||
> - `float` uses **double-precision** (~15 digits, may have rounding errors)
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[dataTypes]]
|
||||
> > [[typeConversions]]
|
||||
> > [[mixedTypeArithmetic]]
|
||||
> > [[arithmeticOperators]]
|
||||
> > [[pythonSyntaxOverview]]
|
|
@ -0,0 +1,32 @@
|
|||
---
|
||||
title: operatorPrecedence
|
||||
tags:
|
||||
- python
|
||||
- syntax
|
||||
- operators
|
||||
- math
|
||||
aliases:
|
||||
- math order
|
||||
- expression order
|
||||
- operator rules
|
||||
---
|
||||
|
||||
> [!important] Operator Precedence
|
||||
> Python follows standard math precedence:
|
||||
> 1. `**` (exponentiation)
|
||||
> 2. `*`, `/`, `//`, `%` (multiplication/division)
|
||||
> 3. `+`, `-` (addition/subtraction)
|
||||
> Operations are evaluated **left to right** at each level.
|
||||
> Use parentheses `()` to override precedence and clarify expressions.
|
||||
>
|
||||
> > [!example] Precedence in Practice
|
||||
> > ```python
|
||||
> > 2 + 3 * 4 # 14
|
||||
> > (2 + 3) * 4 # 20
|
||||
> > ```
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[arithmeticOperators]]
|
||||
> > [[expressions]]
|
||||
> > [[mixedTypeArithmetic]]
|
||||
> > [[pythonSyntaxOverview]]
|
31
20-29 Areas/20 Programming/20.01 Python/print().md
Normal file
31
20-29 Areas/20 Programming/20.01 Python/print().md
Normal file
|
@ -0,0 +1,31 @@
|
|||
---
|
||||
title: print()
|
||||
tags:
|
||||
- python
|
||||
- functions
|
||||
- output
|
||||
aliases:
|
||||
- print
|
||||
- print-statement
|
||||
- output-text
|
||||
---
|
||||
|
||||
> [!note] Concept
|
||||
> The `print()` function outputs a message (usually a string) to the screen.
|
||||
>
|
||||
> > [!example] Example
|
||||
> > ```python
|
||||
> > print('Hello, world!')
|
||||
> > print('What is your name?') # ask for name
|
||||
> > print() # Blank Output
|
||||
> > ```
|
||||
>
|
||||
> > [!important] Best Practices
|
||||
> > - Use `print()` to confirm variable values or trace execution during debugging.
|
||||
> > - Add comments to describe the purpose of each printed message where helpful.
|
||||
> > For formatted strings, prefer:
|
||||
> > ```python
|
||||
> > name = "Alice"
|
||||
> > print(f"Hello, {name}!")
|
||||
> > ```
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
---
|
||||
title: pythonSyntaxOverview
|
||||
tags:
|
||||
- python
|
||||
- syntax
|
||||
- overview
|
||||
- basics
|
||||
aliases:
|
||||
- python syntax
|
||||
- syntax reference
|
||||
- python basics
|
||||
---
|
||||
|
||||
> [!note] Overview of Python Syntax
|
||||
> Python emphasizes **readability and simplicity**. Its syntax is designed to be clean, consistent, and human-friendly.
|
||||
>
|
||||
> Key syntax elements include:
|
||||
> - **Indentation** instead of braces for code blocks
|
||||
> - **Colons `:`** to begin blocks (like loops, functions, conditionals)
|
||||
> - **Comments** start with `#` and are ignored by the interpreter
|
||||
> - **Snake_case** for variable and function names
|
||||
> - **Whitespace** matters for structure, not between expressions
|
||||
>
|
||||
> > [!example] Minimal Example
|
||||
> > ```python
|
||||
> > # This is a comment
|
||||
> > def greet(name):
|
||||
> > print("Hello,", name)
|
||||
> >
|
||||
> > if True:
|
||||
> > greet("Void")
|
||||
> > ```
|
||||
>
|
||||
> > [!tip] Syntax Philosophy
|
||||
> > - “There should be one — and preferably only one — obvious way to do it.” — The Zen of Python
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > - [[variableNamingRules]]
|
||||
> > - [[variableNamingBestPractices]]
|
||||
> > - [[comments]]
|
||||
> > - [[booleans]]
|
||||
> > - [[arithmeticOperators]]
|
||||
> > - [[typeConversions]]
|
||||
> > - [[conditionals]]
|
||||
>
|
41
20-29 Areas/20 Programming/20.01 Python/return.md
Normal file
41
20-29 Areas/20 Programming/20.01 Python/return.md
Normal file
|
@ -0,0 +1,41 @@
|
|||
---
|
||||
title: return
|
||||
tags:
|
||||
- python
|
||||
- functions
|
||||
- return
|
||||
aliases:
|
||||
- return statement
|
||||
- return keyword
|
||||
- none
|
||||
---
|
||||
|
||||
> [!note] Concept
|
||||
> The `return` keyword ends a function and sends back a value. Without it, a function will return `None`.
|
||||
>
|
||||
> > [!example] Explicit Return
|
||||
> > ```python
|
||||
> > def sub_two_nums(a, b):
|
||||
> > return a - b
|
||||
> >
|
||||
> > result = sub_two_nums(100, 49)
|
||||
> > print(result) # 51
|
||||
> > ```
|
||||
>
|
||||
> > [!example] Implicit Return (None)
|
||||
> > ```python
|
||||
> > def no_return_example():
|
||||
> > x = 2 + 2
|
||||
> >
|
||||
> > print(no_return_example()) # None
|
||||
> > ```
|
||||
>
|
||||
> > [!important] Best Practices
|
||||
> > - Always use `return` when a function is expected to give a result.
|
||||
> > - Use `None` as a placeholder return value when the function’s main purpose is side effects.
|
||||
> > - Avoid mixing return types in the same function (e.g., returning both `int` and `None`).
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[functions]]
|
||||
> > [[len()]]
|
||||
> > [[pythonSyntaxOverview]]
|
31
20-29 Areas/20 Programming/20.01 Python/statements.md
Normal file
31
20-29 Areas/20 Programming/20.01 Python/statements.md
Normal file
|
@ -0,0 +1,31 @@
|
|||
---
|
||||
title: statements
|
||||
tags:
|
||||
- python
|
||||
- statements
|
||||
- syntax
|
||||
aliases:
|
||||
- statement
|
||||
- code-line
|
||||
---
|
||||
|
||||
> [!note] Concept
|
||||
> A **statement** performs an action. It could assign a value, call a function, define something, or control flow.
|
||||
>
|
||||
> > [!example] Examples
|
||||
> > ```python
|
||||
> > x = 10 # assignment statement
|
||||
> > print("Hello") # function call statement
|
||||
> > if x > 5: # control statement
|
||||
> > print(x)
|
||||
> > ```
|
||||
>
|
||||
> > [!important] Statement vs Expression
|
||||
> > - An **expression** evaluates to a value.
|
||||
> > - A **statement** does something — it doesn’t always return a value.
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[expressions]]
|
||||
> > [[functions]]
|
||||
> > [[conditionals]]
|
||||
> > [[pythonSyntaxOverview]]
|
|
@ -0,0 +1,37 @@
|
|||
---
|
||||
title: stringConcatenation
|
||||
tags:
|
||||
- python
|
||||
- concatenation
|
||||
aliases:
|
||||
- join-strings
|
||||
- strings +
|
||||
---
|
||||
|
||||
> [!note] String Concatenation
|
||||
> The `+` operator, when used with two strings, combines them into a single new string — this is called **string concatenation**.
|
||||
>
|
||||
> > [!example] Examples
|
||||
> > ```python
|
||||
> > 'Alice' + 'Bob' # AliceBob
|
||||
> > 'Alice' + 42 # TypeError: can only concatenate str (not 'int') to str
|
||||
> > 'Alice' + str(42) # Alice42
|
||||
> > 'Alice' * 5 # AliceAliceAliceAliceAlice
|
||||
> >
|
||||
> > name = "Al"
|
||||
> > print("It is good to meet you, " + name) # It is good to meet you, Al
|
||||
> > ```
|
||||
>
|
||||
> > [!important] Best Practices
|
||||
> > - Ensure both operands are strings before using `+`
|
||||
> > - Prefer `f-strings` or `format()` for cleaner code when mixing types
|
||||
> > ```python
|
||||
> > f"Alice{42}" # Alice42
|
||||
> > ```
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[typeConversions]]
|
||||
> > [[expressions]]
|
||||
> > [[len()]]
|
||||
> > [[The print() Function]]
|
||||
> > [[pythonSyntaxOverview]]
|
42
20-29 Areas/20 Programming/20.01 Python/typeConversions.md
Normal file
42
20-29 Areas/20 Programming/20.01 Python/typeConversions.md
Normal file
|
@ -0,0 +1,42 @@
|
|||
---
|
||||
title: typeConversions
|
||||
tags:
|
||||
- python
|
||||
- casting
|
||||
- conversions
|
||||
- data-types
|
||||
aliases:
|
||||
- convert types
|
||||
- type casting
|
||||
- coercion
|
||||
---
|
||||
|
||||
> [!note] Type Conversion in Python
|
||||
> You can convert values between built-in types using constructor functions:
|
||||
> - `int()`, `float()`, `str()`, `bool()`, `list()`, `tuple()`, `dict()`
|
||||
> - Useful for input handling, type flexibility, or data normalization.
|
||||
>
|
||||
> > [!example] Numeric Conversions
|
||||
> > ```python
|
||||
> > float(1 + 2) # 3.0
|
||||
> > int(6 / 2) # 3
|
||||
> > int("123") # 123
|
||||
> > str(123) # '123'
|
||||
> > bool(0) # False
|
||||
> > bool("text") # True
|
||||
> > ```
|
||||
>
|
||||
> > [!tip] Common Pitfall
|
||||
> > Converting `float` to `int` **truncates** the decimal:
|
||||
> > ```python
|
||||
> > int(4.99) # 4
|
||||
> > ```
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[dataTypes]]
|
||||
> > [[numericTypes]]
|
||||
> > [[mixedTypeArithmetic]]
|
||||
> > [[expressions]]
|
||||
> > [[input()]]
|
||||
> > [[stringConcatenation]]
|
||||
> > [[pythonSyntaxOverview]]
|
|
@ -0,0 +1,50 @@
|
|||
---
|
||||
title: variableNamingBestPractices
|
||||
tags:
|
||||
- python
|
||||
- variables
|
||||
- syntax
|
||||
- naming
|
||||
- constants
|
||||
aliases:
|
||||
- naming conventions
|
||||
- snake_case
|
||||
- ALL_CAPS
|
||||
- variable reassignment
|
||||
---
|
||||
|
||||
> [!important] Naming Best Practices
|
||||
> - Use **snake_case** for variables: `user_name`, `email_count`
|
||||
> - Use **ALL_CAPS** for constants: `MAX_CONNECTIONS = 10`
|
||||
> - Python doesn't enforce constants, but the convention is **SCREAMING_SNAKE_CASE**
|
||||
> - Define constants at the **module level** for global visibility
|
||||
>
|
||||
> - Avoid unintentional reassignment. Python allows variable re-binding to different types:
|
||||
> ```python
|
||||
> my_var = 1
|
||||
> my_var = "Now I'm a string"
|
||||
> ```
|
||||
>
|
||||
> - Variables don’t auto-update in expressions. You must reassign explicitly:
|
||||
> ```python
|
||||
> bacon = bacon + 1
|
||||
> # or
|
||||
> bacon += 1
|
||||
> ```
|
||||
>
|
||||
> > [!example] Reassignment Example
|
||||
> > ```python
|
||||
> > first_variable = 1
|
||||
> > first_variable = 2
|
||||
> > print(type(first_variable)) # <class 'int'>
|
||||
> > print(first_variable) # 2
|
||||
> >
|
||||
> > first_variable = "I'm a string now! I used to be an integer."
|
||||
> > print(type(first_variable)) # <class 'str'>
|
||||
> > print(first_variable)
|
||||
> > ```
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[variableNamingRules]]
|
||||
> > [[typeConversions]]
|
||||
> > [[pythonSyntaxOverview]]
|
|
@ -0,0 +1,33 @@
|
|||
---
|
||||
title: variableNamingRules
|
||||
tags:
|
||||
- python
|
||||
- variables
|
||||
- syntax
|
||||
aliases:
|
||||
- variable names
|
||||
- naming rules
|
||||
- valid variables
|
||||
---
|
||||
|
||||
> [!note] Naming Rules for Python Variables
|
||||
> Variable names must follow these rules:
|
||||
> 1. Must be a single word (no spaces)
|
||||
> 2. Can contain letters, numbers, and underscores
|
||||
> 3. Cannot start with a number
|
||||
> 4. Cannot include special characters
|
||||
>
|
||||
> > [!example] Valid vs ❌ Invalid Variable Names
|
||||
> > | **Valid Variable Names** | **Invalid Variable Names** |
|
||||
> > |--------------------------|----------------------------------------|
|
||||
> > | current_balance | current-balance *(no hyphens)* |
|
||||
> > | currentBalance | current balance *(no spaces)* |
|
||||
> > | account4 | 4account *(starts with number)* |
|
||||
> > | 42 | 42 *(starts with number)* |
|
||||
> > | TOTAL_SUM | TOTAL_$UM *(no special characters)* |
|
||||
> > | hello | 'hello' *(no quotes allowed)* |
|
||||
>
|
||||
> > [!info] Related Notes
|
||||
> > [[variableNamingBestPractices]]
|
||||
> > [[comments]]
|
||||
> > [[pythonSyntaxOverview]]
|
Loading…
Add table
Add a link
Reference in a new issue