Set Deduplication: Creation and Common Operations of Python Sets
Python sets are efficient tools for handling unordered, non - duplicate data, with core applications in deduplication and set operations. Creation methods include directly defining with `{}` (note that an empty set must use `set()`, as `{}` is a dictionary) or converting iterable objects like lists using the `set()` function. Common operations include: adding elements with `add()`, removing with `remove()` (which raises an error if the element does not exist) or `discard()` (for safe deletion), and `pop()` for randomly deleting elements. Set operations are rich, such as intersection (`&`/`intersection()`), union (`|`/`union()`), and difference (`-`/`difference()`). Key characteristics: unordered (cannot be indexed), elements must be immutable types (such as numbers, strings, tuples), and cannot contain lists or dictionaries. In practice, deduplicating a list can be directly done with `list(set(duplicate_list))` (order is random); since Python 3.7+, combining with a list comprehension `[x for x in my_list if not (x in seen or seen.add(x))]` can maintain the order. Mastering set creation, operations, characteristics, and deduplication methods enables efficient resolution of data deduplication and set operation problems.
Read MoreTuples vs. Lists: How to Use Python's Immutable Data Types and What's the Difference?
In Python, lists and tuples are commonly used data containers, with their core difference lying in mutability. Lists are defined using `[]` and are mutable (supporting additions, deletions, and modifications), making them suitable for dynamic data (such as updated student grades or to-do items). Tuples are defined using `()` and are immutable (their overall structure cannot be changed, except for mutable elements within them), making them ideal for static data (such as fixed dates or configuration information). When creating them, note that a single-element tuple must include a comma (e.g., `(10,)`; otherwise, it is treated as a regular variable). Lists support modifying elements and adding/removing operations, while tuples cannot be directly modified. However, mutable elements within a tuple (such as lists) can still be modified. Lists are flexible but prone to accidental modifications, while tuples are safer and can be used as dictionary keys. In summary, a list is like a "flexible shopping list," and a tuple is like a "fixed contract"—choose based on whether the data needs to be modified.
Read MoreList Comprehensions: A Concise Python Technique for Creating Lists (Beginner-Friendly)
This article introduces Python list comprehensions as a concise method for creating lists, which replaces the traditional for loop combined with append in one line of code, making it more efficient and concise. The basic syntax is `[expression for variable in iterable]`, for example, generating squares of numbers from 1 to 10: `[i**2 for i in range(1,11)]`. Screening conditions can be added using `if`, such as filtering even numbers: `[i for i in range(1,11) if i%2==0]`. The expression supports flexible operations such as string processing (e.g., `name.upper()`) and function calls (e.g., `abs(num)`). It should be noted that list comprehensions use `[]` to generate complete lists, which consume memory; generator expressions use `()` to create lazy sequences, saving memory. The core advantages are concise code and high readability. It is recommended to practice rewriting traditional loop codes, such as generating cubes and filtering negative numbers.
Read MorePython Input and Output: A Practical Tutorial on print() and input() Functions
This article introduces basic input and output operations in Python, with the core being the `print()` and `input()` functions. The `print()` function is used to output content, supporting text, numbers, variables, or expressions. It allows customizing the separator (e.g., using `-` to separate elements) via the `sep` parameter and controlling the ending (default is a newline; setting it to an empty string enables multi-line content to be printed on the same line) through the `end` parameter. The `input()` function retrieves user input and returns it as a string, which needs to be converted to numeric types (e.g., `int()`/`float()`) for numerical operations. For multiple inputs, the `split()` method can be used to separate values by spaces or commas, etc. Taking a "Personal Information Collection Program" as an example, the article demonstrates combining these functions: obtaining name, age, and height, outputting formatted information, and calculating next year's age and height. The summary emphasizes that `print()` enables flexible output, `input()` requires type conversion, `f-strings` facilitate convenient variable and expression concatenation, and proficiency can be achieved through more practice.
Read MoreAvoid Mistakes! A Detailed Explanation of Python Indentation Rules — Why Are Spaces So Important?
Python's indentation rules are a core syntactic feature, using spaces or tabs to distinguish code blocks instead of curly braces, aiming to enhance code readability and standardization. Core rules: uniformly use 4 spaces (PEP 8 specification), prohibit mixing spaces and tabs within the same code block, and indentation amount must be consistent within the same code block. Common errors include "unindentation" (e.g., not indenting the code block after an if statement) and "inconsistent indentation" (e.g., some code in a loop indented 2 spaces while others 4), both of which trigger IndentationError. Empty code blocks require the `pass` placeholder (e.g., temporary if blocks with unfilled logic). To avoid errors: use an editor's auto-indentation (e.g., VS Code, PyCharm), enforce the 4-space standard, and check indentation consistency by selecting all code after writing. Indentation essentially defines logical structure through spaces; developing this habit results in cleaner, more logically clear code.
Read MoreOne-Line Python Comments: Correct Ways to Write Single-Line and Multi-Line Comments
Python comments are the "instruction manuals" for code, aiding understanding and review. Single-line comments start with `#`, and the content after `#` is ignored. They can be placed after a code line or on a separate line. Note that `#` does not affect other lines, and it should not be written inside strings. Multi-line comments are implemented using three single quotes `'''` or double quotes `"""`, which are essentially strings. If used inside a function, they serve as docstrings (documentation strings) and can be viewed with `help()`. It is important to avoid using comments to hide code, avoid redundancy (comments should explain "why" rather than "what"), and do not assign variables to multi-line comments. By mastering the syntax of single-line `#` and multi-line triple quotes, you can write clear comments.
Read MoreFunction Definition and Call: How to Create Your First Function in Python?
Functions are a core tool for code reuse in Python, designed to solve the problem of repetitive code. By "packaging" functional modules, they save time and ensure consistency. The definition syntax uses the `def` keyword, including the function name, parameters (to receive external data), an indented function body, and `return` (to return a result, defaulting to `None`). When calling, parameters must be passed (either positional or keyword arguments), and the return value should be received. Key points to note include indentation requirements, parameter quantity matching, and unique function names. Mastering the basics of functions (parameters, return values) is crucial for advanced usage, as they allow breaking down complex logic and improving code simplicity and maintainability.
Read MorePython Loops Fundamentals: Differences and Application Scenarios between for and while Loops
This article introduces two basic Python loop structures: for and while, which are used to reduce repetitive code and handle repeated tasks. The for loop iterates over iterable objects (such as lists, strings, range, etc.). For example, printing numbers 1-5 or calculating the average score of a list. It is implemented with the syntax "for variable in iterable object", and the number of iterations is determined by the length of the sequence. It is suitable for scenarios where the traversal object is clearly known. The while loop is based on conditional judgment and controlled by "while condition". For example, calculating the sum of 1-10 or verifying user input. The condition must be modified within the loop to avoid infinite loops. It is suitable for scenarios where the loop is controlled by conditions. Core difference: for traverses a fixed sequence, while controls the number of iterations through conditions. Note should be taken of avoiding infinite loops in while and errors when for traverses non-iterable objects.
Read MoreLearning Python from Scratch: How to Use if-else Conditional Statements? A Practical Guide with Examples
The if-else conditional statement in Python is used to perform different operations based on conditions, addressing complex scenario judgment requirements (such as determining if a number is positive or negative, or whether a score is passing, etc.). Key syntax notes include adding a colon after the condition, using indentation to denote code blocks, and condition expressions that include comparison operators like >, <, and ==. Scenarios are categorized into three types: 1. Single condition execution uses if (e.g., checking if a number is greater than 5); 2. Binary choice uses if-else (e.g., determining if a score is passing); 3. Multi-condition sequential judgment uses if-elif-else (e.g., grade classification, where the first satisfied condition takes precedence). Key points to avoid: missing colons, indentation errors, improper condition order (e.g., checking lower scores first may prevent higher scores from being recognized), and using == instead of = for comparison operators. Once mastered, it enables flexible implementation of conditional judgment and is a core tool for Python's logical control.
Read MoreBeginner's Guide: Python Dictionaries - Key-Value Pairs and Iteration Techniques
This article introduces Python Dictionaries, which store data as key-value pairs. Keys are unique and immutable types (e.g., strings, numbers), while values can be of any type, similar to an address book. Creation: Use `{}` with key-value pairs like `{"name": "Xiaoming", "age": 18}`. Access: Directly use `dict[key]` (raises an error if the key does not exist); the `get()` method is recommended for safety (returns None or a custom value by default). Modification/Addition: Assign a value; if the key exists, its value is updated; if not, a new key-value pair is added. Deletion: Use `del dict[key]` or `dict.pop(key)`. Iteration: Three methods: `for key in dict` (iterates over keys), `for value in dict.values()` (iterates over values), and `for key, value in dict.items()` (iterates over key-value pairs). Common techniques: Use `in` to check key existence, `len()` to get the length, and `update()` to merge dictionaries (overwriting duplicate keys). Dictionaries are flexible and efficient, ideal for storing relational data. Mastering core operations enables proficient application.
Read MoreMastering Python Lists with Ease: Creation, Indexing, and Common Operations
Python lists are ordered and mutable data containers denoted by `[]`, where elements can be of mixed types (e.g., numbers, strings) and support dynamic modification. They are created simply by enclosing elements with `[]`, such as `[1, "a", True]` or an empty list `[]`. Indexing starts at 0, with `-1` representing the last element; out-of-bounds access raises `IndexError`. The slicing syntax `[start:end:step]` includes the start index but excludes the end index, with a default step size of 1. Negative step sizes allow reverse element extraction. Core operations include: adding elements with `append()` (to the end) and `insert()` (at a specified position); removing elements with `remove()` (by value), `pop()` (by index), and `del` (by position or list deletion); modifying elements via direct index assignment; checking length with `len()`, and existence with `in`. Concatenation uses `+` or `extend()`, and repetition uses `*`. Sorting is done with `sort()` (in-place ascending) or `sorted()` (returns a new list); reversing uses `reverse()` (in-place) or `reversed()` (iterator). Mastering list creation, indexing/slicing, and basic operations (addition, deletion, modification, querying, etc.) is essential for data processing.
Read MoreMust-Know for Beginners: A Detailed Explanation of Python Data Types (Integers, Strings, Booleans)
This article introduces Python's basic data types, using the analogy of "data boxes" with labels to help understand the operations of different data. There are three core types: 1. **Integer (int)**: Handles numbers (positive/negative/0), supporting addition, subtraction, multiplication, division, modulo operation (%), and integer division (//). It can be converted using int() (e.g., converting a string to an integer) and has no size limit. 2. **String (str)**: Text enclosed in quotes (single/double quotes, requiring matching pairs), supporting concatenation (+), length calculation (len()), and indexing (0-based). It can be converted using str() (e.g., converting an integer to a string). 3. **Boolean (bool)**: Only True/False, used for logical judgments, and supports the not operator for negation (e.g., in conditional statements). These three types are fundamental to programming. Subsequent learning will involve complex types like lists and dictionaries, making basic data types a crucial prerequisite.
Read MorePython for Beginners: Variable Basics — Definition, Assignment, and Usage
A variable is a "container" for storing data in programming, used to flexibly store and modify information (such as age, weight) and avoid repeatedly writing specific numerical values. In Python, variables are defined directly using "variable name = value" (e.g., age = 18), without type declaration; the assignment determines the type (dynamic typing). Variable naming must follow rules: it can only contain letters, numbers, and underscores, cannot start with a number, cannot use keywords (e.g., if), and is case-sensitive (age ≠ Age). When using, variables are printed by name (print(age)) or involved in calculations (e.g., x + y). The variable type changes with the last assignment (e.g., after a = 10, a = "Python"). Precautions: A variable must be assigned before use; variable names should be meaningful (e.g., student_count instead of sc); avoid repeated assignment that overwrites the original value. Variables are a core tool in Python; mastering their definition, naming, and usage enables efficient data processing.
Read MoreQuick Directory Tree Creation: A Guide to Using the Ubuntu `tree` Command
Tree is a tool for visualizing directory structures in Ubuntu, which can intuitively display file hierarchies and is suitable for understanding project organization. To install it, first execute `sudo apt update`, then use `sudo apt install tree`. Basic usage: Simply enter `tree` to view the current directory's tree structure. Common parameters: - `-d` only shows directories; - `-L N` (where N is a number) controls the display depth (e.g., `tree -L 2`); - `-f` shows full paths; - `-F` distinguishes file types (directories are appended with `/`); - `-a` shows hidden files; - `-h` displays file sizes (in K/M/G). Advanced usage: Output to a file (`tree > dir.txt`) or combine with `find` to view system directories (e.g., `find /usr/share | tree -L 1`). By combining parameters, you can flexibly control the output and improve file management efficiency.
Read MoreUbuntu System Information: Viewing Hardware Configuration with lscpu/lspci
In the Ubuntu system, understanding hardware configuration is fundamental for operations, and you can quickly obtain information using the `lscpu` and `lspci` commands. `lscpu` focuses on CPU and memory: executing it directly reveals details such as CPU architecture, number of logical/physical cores, model, cache, and total memory capacity. For example, "Model name" shows the CPU model, "CPU(s)" indicates the number of threads, and "Memory" displays the memory size. `lspci` is used to list PCI devices (such as graphics cards and network cards). Commonly used parameters include `-v` (detailed information), `-t` (tree structure), and `-nn` (hardware ID). The output includes device type, manufacturer, and model. For instance, "01:00.0 VGA compatible controller: NVIDIA Corporation..." can identify the graphics card. Practical tips: Redirect output to a file to save configurations, use `lspci -vnn | grep -i vga` to filter graphics card information, and `lspci -t` to display the device connection structure. These two commands help quickly troubleshoot hardware issues or confirm compatibility.
Read MoreSystem Resource Monitoring: Is Ubuntu's htop Command Better Than top?
In Ubuntu, top and htop are commonly used system resource monitoring tools. The classic top tool has a monochrome interface with compact information and requires memorizing shortcuts (e.g., P/M for sorting), lacking mouse operations. Its memory units are default in Kb, which is not intuitive, making it suitable for users familiar with commands. htop, an enhanced version of top, needs prior installation on Ubuntu (`sudo apt install htop`). It features color display, tabular layout, mouse support, intuitive memory units, and process tree visualization, making it easier for newcomers to use. Comparison shows htop is more beginner-friendly: clear color visuals allow header-click sorting, while operations like F5 for process trees and F6 for sorting are simple. Top suits scenarios requiring complex monitoring for users proficient in Linux commands. In summary, htop is recommended for beginners as it enables more intuitive and efficient system monitoring.
Read MoreUbuntu Text Processing: Using the cat Command to View File Contents
The `cat` command is a fundamental text processing tool in the Ubuntu system, derived from "concatenate". Its core function is to view and merge file contents. The basic syntax `cat filename` can display a file's content (e.g., viewing `test.txt`). Common options enhance its functionality: `-n` shows line numbers for all lines (including empty ones), `-b` only numbers non-empty lines, and `-s` merges consecutive empty lines. For multi-file processing, you can view multiple files simultaneously (e.g., `cat file1 file2`) or redirect the merged output to a new file using `>` (e.g., `cat a.txt b.txt > new.txt`). Important notes: If the file does not exist, an error will occur, so verify the path. If permissions are insufficient, use `sudo`. The `>` redirection overwrites the target file; it is recommended to back up or use `>>` for appending instead. Despite its simplicity, `cat` is highly practical. By practicing basic operations (e.g., testing different options and merging multiple files), you can quickly master its flexible applications.
Read MoreAdvanced Permission Management: Risks and Use Cases of `chmod 777` on Ubuntu
In the Ubuntu system, `chmod 777` is a command to modify file/directory permissions and should be used with caution. Its meaning is to grant full permissions to the owner, the group it belongs to, and other users through the numeric mode `777` (corresponding to `rwx`, i.e., read, write, and execute permissions). `777` is considered a high-risk permission due to multiple risks: any user can arbitrarily modify or delete files or directories. If applied to a web server directory, it is vulnerable to uploading malicious scripts. In development environments or old systems, misconfiguration or legacy setups can easily lead to permission abuse, violating security compliance. Although temporary use may occur in teaching, testing, or development debugging, it is not recommended. Secure alternatives include: `755` (owner with `rwx`, group and others with `rx`), setting correct owners/groups (e.g., `770` for internal group users only), or using ACL tools for precise permission control. In summary, the risks of `777` permissions far outweigh the benefits. It should be avoided unless the system is absolutely secure and users are completely trustworthy. It is recommended to use more secure permission settings instead.
Read MoreDifference between Ubuntu apt-get and apt: Which one should beginners use?
In the Ubuntu system, both `apt` and `apt-get` are used for package management, but they differ in design goals and user-friendliness for beginners. `apt-get` is an early tool with comprehensive functionality but complex parameters (requiring subcommands like `apt-get install`), making it suitable for experienced users. `apt`, on the other hand, is a newer version introduced after Ubuntu 16.04. It consolidates commonly used features into more concise commands (e.g., `apt install`), automatically handles dependencies, and focuses on beginner-friendly scenarios. The core differences lie in `apt`'s intuitive commands and more intelligent dependency handling, making it the preferred choice for newcomers. Essential `apt` commands for beginners include: `sudo apt update` (updating package sources), `sudo apt install <package-name>` (installing software), `sudo apt search <keyword>` (searching for packages), `sudo apt upgrade` (upgrading packages), and `sudo apt purge <package-name>` (completely uninstalling software). In summary, beginners are recommended to directly use `apt`, as it can cover 90% of daily usage scenarios.
Read MoreTerminal Efficiency: Ubuntu Command Line History Management
Managing Ubuntu command line history effectively can significantly improve operational efficiency. The core methods are as follows: **Viewing and Searching**: Use the basic `history` command to display numbered historical commands. For quick searching, use `Ctrl+R` for reverse search (keyword matching, press Enter to execute, Ctrl+G to exit), or `history | grep "keyword"` for filtering. **Modifying and Deleting**: Use `fc` to modify commands, e.g., `fc -e number` to open an editor for correction, or `fc -s number parameter` to modify parameters and execute. To delete, use `history -c` to clear the session, `history -d number` to delete a specific command, or directly `rm ~/.bash_history` to permanently clear. **Customization and Optimization**: Edit `~/.bashrc` to set `HISTSIZE`/`HISTFILESIZE` to control the number of commands, `HISTCONTROL=ignoredups` to ignore duplicates, `HISTTIMEFORMAT` to add timestamps, and `HISTIGNORE` to shield sensitive commands. **Practical Tips**: Use `Ctrl+P/N` to navigate through history, and `!number` to execute historical commands. Proper management of history can greatly enhance command reuse efficiency.
Read MoreUbuntu touch Command: Quickly Create Empty Files
In the Ubuntu system, the `touch` command is a practical utility for creating empty files. Its core function is to quickly generate empty files. If the target file already exists, it only updates its access and modification timestamps without altering the content. Basic usage includes: creating a single file (e.g., `touch test.txt`), creating multiple files in bulk (separated by spaces, e.g., `touch file1.txt file2.txt`), and specifying a path to create a file (e.g., `touch ~/Documents/note.txt`). When using it, note that if the directory in the target path does not exist, you need to first create the multi-level directory using `mkdir -p`. If permission is insufficient, you can use `sudo` to elevate privileges (e.g., `sudo touch /root/test.txt`). If the file already exists, only the modification time will be updated, and the content remains unchanged. Summary: The `touch` command is simple and efficient, supporting multiple files and path specification. It is a "powerful tool" for creating empty files and updating timestamps. Just ensure attention to permissions and path validity when using it.
Read MoreMust - Read for Beginners: Ubuntu Software Uninstallation (remove vs purge)
In Ubuntu, the common commands for uninstalling software are `apt remove` and `apt purge`, which often confuse new users due to their similar appearance. Both require `sudo` privileges. `remove` only uninstalls the software package while retaining configuration files (e.g., settings), making it suitable for reinstallation when you want to preserve previous settings. In contrast, `purge` completely removes the software package, its configuration files, and associated dependencies, which is ideal for thorough cleanup to avoid residual interference. Residual configuration files may cause conflicts between old settings and new software versions after reinstallation. You can check if a package is completely uninstalled using `dpkg -l | grep 包名` or `dpkg -s 包名`. If unsure, start with `remove` first; if residual configurations affect functionality, use `purge` to perform a complete removal. Summary: `remove` is a lightweight option that preserves settings, while `purge` performs a thorough deletion of configurations. Choose the appropriate command based on your needs.
Read MoreUbuntu netstat Command: View Network Connection Status
In Ubuntu, `netstat` is a core network management tool used to view critical network data such as connections and routing tables. If not pre-installed, it can be installed by executing `sudo apt update && sudo apt install net-tools`. The basic syntax is `netstat [options]`, with commonly used parameters and their functions: - `-a` shows all connections (including TCP/UDP); - `-t`/`-u` filter TCP/UDP protocols respectively; - `-n` displays IP/port in numeric format (no DNS resolution); - `-l` only shows listening connections; - `-p` requires sudo privileges to display process IDs and names; - `-r` views the routing table. Typical application scenarios include: - Checking listening ports with `sudo netstat -tuln` (combination of `-tuln`: TCP/UDP listening, numeric format); - Troubleshooting port occupancy (e.g., port 80) with `sudo netstat -tulnp | grep 80`; - Viewing established TCP connections with `netstat -tan | grep ESTABLISHED`. Mastering core commands and parameter combinations (e.g., listening ports, port occupancy, routing tables) combined with tools like `grep` enables efficient network issue diagnosis.
Read MoreSystem Maintenance: Clearing Cached Files with Ubuntu apt clean
In the Ubuntu system, packages downloaded by the `apt` tool are temporarily stored in the cache directory `/var/cache/apt/archives/`. Long-term accumulation of these packages can occupy disk space and affect system speed. Cleaning the cache can improve efficiency, and the `apt clean` command is recommended for this purpose. APT cache is used to accelerate repeated installations and is stored in the specified directory. The problems of long-term non-cleaning include occupying space and containing useless old version packages. To use `apt clean`, open the terminal (Ctrl+Alt+T), execute `sudo apt clean`, and enter the administrator password. After cleaning, it will not affect the installed software. Other related commands: `autoclean` only cleans up old version packages (retains new versions); `autoremove` deletes packages that are no longer dependent (not for cleaning cache). Precautions: Clean regularly (e.g., monthly), and you can check the cache size with `du -sh /var/cache/apt/archives/`. Combining with `autoclean` or `autoremove` can enable fine-grained management of the cache and keep the system clean.
Read MoreUbuntu sudo Command: The Correct Way to Perform Privilege Escalation
In Ubuntu, "sudo" is an abbreviation for "superuser do," allowing ordinary users to temporarily gain root privileges to execute administrative commands, such as using `sudo apt install` to elevate permissions when installing software. Its necessity lies in avoiding the high risks of directly using the root account (such as system crashes caused by accidental operations). It enables secure privilege elevation through temporary permissions, hiding the root password, and supporting multi-user collaboration. Basic usage: `sudo [command]`, for example, installing software (`sudo apt install [package name]`), system updates (`sudo apt update/upgrade`), and modifying configurations (`sudo nano /etc/...`). Common options: `sudo -i` switches to the root shell, and `sudo -u [username] [command]` executes commands as another user. Password-free configuration: Edit the sudoers file using `visudo` and add `your_username ALL=(ALL:ALL) NOPASSWD: ALL` (suitable for personal environments, use cautiously in public environments). Notes: If you forget your password, you can reset it with `su -`; avoid dangerous operations (e.g., `rm -rf /`); if a command fails, check for spelling errors or permission requirements. Summary: Sudo is a secure privilege escalation tool. Correct usage (considering scenarios, options, and rules) can prevent system issues.
Read More