YouTubeATP
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    The sources provide extensive coverage of Python syntactical elements, core concepts, and operational details. Below is a comprehensive breakdown of the syntactical detail covered, including the examples you provided and other relevant concepts. --- ### I. Syntax Fundamentals and Program Structure | Concept | Syntactical Details | | :--- | :--- | | **Comments** | Single-line comments begin with the hash symbol (`#`) [1, 2]. Multi-line comments, documentation strings (**Docstrings**), and multiline strings are enclosed in triple quotes (`'''` or `"""`) [1-4]. While triple quotes can function as comments, using `#` is preferred for clarity [4]. | | **Program Entry Point** | Programs often use a `main()` function defined with `def main():` [5]. The entry point executes the main logic after checking the environment using `if __name__ == "__main__": main()` [5, 6]. | | **Indentation** | Indentation is syntactically significant; the difference in indentation **disturbs Python's ability to translate the source code into bytecode** [7]. Indentation defines code blocks (e.g., function bodies, loops, conditional statements) [8, 9]. Consistent 4-space indentation is good practice [10]. | | **Statements per Line** | Good programming style recommends putting **no more than one statement on each line** [10, 11]. Multiple statements can be separated by a semicolon (`;`) [12, 13]. | | **Case Sensitivity** | Python is a **case-sensitive language**, meaning it treats lowercase and uppercase letters differently (e.g., variable names like `my_var` and `My_Var` are distinct) [14, 15]. | ### II. Data Types, Variables, and Typing | Concept | Syntactical Details | | :--- | :--- | | **Variable Naming** | Variable names (identifiers) must start with a letter (Unicode) or an underscore (`_`) but **cannot start with a digit** [16-18]. They can contain letters, digits, and underscores [17]. **Reserved words** (e.g., `if`, `for`, `True`, `None`) cannot be used as variable names [17, 19]. | | **Object Creation** | Objects are created when a value is first assigned to a variable using the assignment operator (`=`) [20, 21]. | | **Named Constants** | Python does not have special syntax for constants, but convention dictates using **ALL UPPERCASE LETTERS** (e.g., `PI`, `MAX_LIMIT`) [10, 22, 23]. | | **Type Hinting** | Used to specify the expected type of a variable or function parameter/return value using a colon (`:`) followed by the type name [24, 25]. For return types, an arrow (`->`) is used: `def max(num1: int, num2: int) -> int:` [25]. Type hinting aids readability but **does not enforce the type** at runtime [15]. | | **Literals** | Six basic literal types exist: **Integer** (`42`), **Floating Point** (`7.35`, `5.67e-2`), **Complex** (`1+2j`), **Boolean** (`True`, `False`), **String** (`"Hello"`, `'a'`), and **None** [26]. | | **Mutability** | Python objects are categorized as **Mutable** or **Immutable** [27, 28]. **Immutable types** include integers, floats, complex numbers, booleans, strings, and tuples [27, 28]. **Mutable types** include lists, dictionaries, and sets [27, 28]. | | **Type Conversion** | Explicit type conversion is performed using built-in functions: `int()`, `float()`, `str()`, and `bool()` [29-31]. Implicit type conversion occurs when an integer and a float are involved in a binary operation (the integer is converted to a float) [32]. | ### III. Python Operations and Advanced Semantics #### A. Arithmetic, Assignment, and Comparison | Concept | Syntactical Details | | :--- | :--- | | **Arithmetic Operators** | Include addition (`+`), subtraction/negation (`-`), multiplication (`*`), **float division (`/`, always returns float)**, **integer/floor division (`//`)**, exponentiation (`**`), and modulo (`%`) [33, 34]. | | **Precedence & Associativity** | Operators have strict precedence [35, 36]. `**` is right-to-left associative [37, 38]. `*`, `/`, `//`, `%`, `+`, `-` are left-to-right associative [35, 38]. | | **Assignment Operators** | Basic assignment (`=`) [39]. **Augmented assignment** simplifies operations: `+=`, `-=`, `*=`, `/=`, `//=`, `%=`, `**=` [40]. **No increment (`++`) or decrement (`--`) operators** exist [41]. | | **Simultaneous/Cascading Assignment** | **Simultaneous assignment** (`x, y = 3, 4`) performs variable swapping idiomatically (`x, y = y, x`) [39, 42, 43]. **Cascading assignment** (`i = j = k = 1`) assigns the same value to multiple variables [39, 44, 45]. In both, the right-hand expression is evaluated exactly once, and assignments proceed from left to right [45, 46]. | | **Relational Operators** | Operators (`<`, `<=`, `>`, `>=`, `==`, `!=`) compare values and return a Boolean result (`True` or `False`) [47]. Comparisons on sequences (like strings) use **lexicographical order** based on ASCII values [48-50]. | | **Chained Comparison** | Python supports chaining comparisons (e.g., `3 <= x <= 10`), which is equivalent to combining individual comparisons with `and` [51-53]. A key syntactical feature is that **each expression in the chain is evaluated exactly once** [54, 55]. | #### B. Identity, Logical Operations, and Caching | Concept | Syntactical Details | | :--- | :--- | | **Identity Operators** | `is` and `is not` check for **object equality**, meaning they check if two variables refer to the exact same object in memory [56-58]. This is different from `==`, which checks for value equality [57]. | | **Small Integer Caching** | Python employs an optimization technique where small integers (typically in the range of **-5 to 256**) are cached [56, 59, 60]. Variables assigned these values will often reference the same object, causing `a is b` to evaluate to `True` [56, 59]. Integers outside this range may or may not be cached, depending on the implementation [59, 61]. | | **Logical Operators** | The keywords `not`, `and`, and `or` perform logical negation, conjunction, and disjunction, respectively [62]. `not` has the highest precedence, followed by `and`, then `or` [36, 63]. | | **Short-Circuit Evaluation** | Also known as **Lazy Operations**, this technique halts the evaluation of a Boolean expression containing `and` or `or` operators as soon as the truth value is definitively determined (evaluation proceeds left-to-right) [64]. For example, in `p or q`, `q` is only executed if `p` is `False` [64]. | | **Membership Operators** | `in` and `not in` check if an item exists within a collection (sequence) [65]. | ### IV. Control Flow (Branching and Looping) | Concept | Syntactical Details | | :--- | :--- | | **Conditional Statements** | Implemented using `if`, `if..else`, and the **if-elif-else ladder** [9, 66, 67]. **Nested `if` statements** allow branching logic within existing `if` or `else` blocks [68]. | | **Conditional Expressions** | Also known as the **ternary operator**, the syntax is `<variable> = <expression 1> if <Boolean expression> else <expression 2>` [69]. **Only one of the expressions is evaluated and returned** based on the Boolean outcome [69]. | | **match-case Statement** | Introduced in Python 3.10 for **structural pattern matching** [70, 71]. Syntax uses `match <expression>:` followed by `case <value>:` blocks [70]. Multiple values can be matched using the **pipe operator** (`|`), e.g., `case 'A' | 'a':` [72]. The **wildcard case** (`case _:`), which serves as the default, must be placed at the end [70, 72, 73]. | | **Looping Statements** | Python provides the **`while` loop** (suitable for sentinel-controlled loops) and the **`for` loop** (suitable for counter-controlled loops or iterating over sequences) [74, 75]. | | **range() Function** | Used primarily in `for` loops, taking integer arguments: `range(<initial value>, <end value (exclusive)>, <step value>)` [76, 77]. | | **break and continue** | **`break`** causes the immediate termination of the innermost enclosing loop [78, 79]. **`continue`** skips the remainder of the current loop iteration and proceeds to the next iteration [79, 80]. | | **for/while-else Loop** | An optional `else` block can be appended to `for` or `while` loops [81, 82]. This `else` block only executes if the loop **completes normally** (i.e., the condition becomes false for a `while` loop, or all items are iterated over for a `for` loop), **without hitting a `break` statement** [81-83]. | | **Nested Loops** | Allows one loop to appear inside another [84]. If the same variable name is used for both inner and outer loop variables, the inner loop's assignment overwrites the outer variable's value [85]. | ### V. Sequences and Collections | Concept | Syntactical Details | | :--- | :--- | | **Indexing** | Elements in a sequence (lists, strings, ranges) are accessed using the **index operator (`[]`)** [10, 86]. Indexing is **0-based** (0 is the first element) [86]. **Negative indices** allow access relative to the end of the list (`-1` is the last element) [87]. Out-of-bounds access results in an `IndexError` [88]. | | **List Slicing (Retrieval)** | The slicing operator uses the syntax: `my_list[start : end : step]` [89, 90]. `start`, `end`, and `step` must be integers, and **`step` cannot be 0** [89, 91]. The slice includes `start` and excludes `end` [89]. Unlike indexing, slicing handles **out-of-range indices gracefully** without raising an `IndexError` [92, 93]. | | **List Slicing (Assignment)** | Used to replace or insert elements in a mutable sequence (list) [94, 95]. If `step` is 1 (default), the slice length does not need to match the replacement sequence length; elements are effectively removed and replaced [95, 96]. If `step` is not 1, the length of the slice (elements being replaced) **must exactly match** the length of the replacement sequence (otherwise, `ValueError` occurs) [97, 98]. | | **List Comprehensions** | Provides a concise method to create a list based on an existing sequence: `[<expression> for <variable> in <sequence> if <condition>]` [99, 100]. | | **String Splitting** | The `str.split(separator)` method is used to split a string into a list of substrings [101, 102]. If the `separator` is omitted, it defaults to any whitespace (spaces, tabs, newlines), and consecutive whitespaces are treated as a single delimiter [102]. | ### VI. Copying Semantics: Shallow vs. Deep Copy The distinction between shallow and deep copies is crucial when dealing with collections containing mutable objects. #### 1. Shallow Copy A shallow copy creates a **new object (the outer container)** but populates it with **references to the objects found in the original container** [103, 104]. * **Creation Methods:** Shallow copies can be created using the list constructor (`list(original_list)`), list comprehension (`[x for x in original_list]`), list concatenation (`[] + original_list`), or list splicing (`original_list[:]`) [105]. They can also be created using `copy.copy(original_list)` [104]. #### 2. Deep Copy A deep copy creates a new object and **recursively adds copies of all nested objects** found in the original [103]. * **Creation Method:** Must be created using `copy.deepcopy(original_list)` from the `copy` module [105, 106]. #### 3. Side Effects (Which types cause side effects?) Side effects in a shallow copy occur when the copied list contains objects that are **mutable types** [103, 107]. * If a shallow copy contains **immutable objects** (like integers, floats, strings, or tuples), modifying those elements in the copy actually replaces the *reference* in the copy with a new immutable object, leaving the original unaffected [108, 109]. * If a shallow copy contains **mutable objects** (like a nested list or dictionary), the copy holds a shared reference to that internal mutable object. Modifying the internal mutable object through the copy (e.g., `shallow_copied_list = 99`) **will affect the original list** because the nested object itself was not duplicated [103, 110]. * Therefore, **mutable types** (lists, dictionaries, sets) nested within a collection are the types that cause side effects when modified in a shallow copy [27, 103].

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully