Analyze the basic syntax of Python in simple terms
Share 10 posts every day and fly together!
1. List fragmentation:
2. The magic of the list:
(1) Compare size:
(2) List addition:
(3) When lists are multiplied, they become compound assignments:
(4) The usage of in/not in in the list:
(5) The concept of "copy" of list fragmentation:
Sharded copy syntax: list name[:];
The essence of sharded copy: open up a new memory space;
Note: The real copy requires the sharding method;
3. The difference between tuples and lists:
Answer: Tuples and lists are the largest The difference is that you can modify the elements in the list at will, and you can insert or delete an element at will, but this is not possible for tuples. Tuples are immutable (like strings), so you can’t expect to perform original operations on tuples. advanced operations such as sorting.
Note: Tuples can be defined without parentheses (list = 1,2,3,4)
4. Use string concatenation to update and delete:
5. Use join to separate String:
split() is exactly the opposite of join(). split() is used to split the string:
replace() method, as its name suggests, replaces the specified string:
6. String formatting:
(1) format () Function usage:
(2) Formatting operator: %
7, sequence:
(1) list(), tuple(), str(obj);
(2) Keyword parameters:
This is very similar to PHP
This is very interesting, prepared for lazy people
The asterisk * can actually be packaged and 'unpacked'. If you need to pass a list a into the collection parameter *params of the test parameter, then an error will occur when calling test(a). This You need to add an asterisk (*) in front of a to indicate that the actual parameters need to be "unpacked" before they can be used:
(5) Global variables:
But it is not ruled out that some people still insist on the eight-character principle of "accept humbly and never change" and still feel it is necessary to modify this global variable in the function, then you might as well use global Keywords to achieve the goal!
Note: Closures target internal functions, so they cannot be used directly externally. Adjust the internal function;
(8) lambda expression: Also known as anonymous function
(9) filter( ) and map():
filter() has two parameters. The first parameter can be a function or None. If it is a function, the second iterable data will be used. Each element in is calculated as a parameter of the function, and the returned True value is filtered out; if the first parameter is None, the True value in the second parameter is directly filtered out. Come, let's take a look directly. Example:
## (10) Recursion is "magical":
The concept of recursion is the category of algorithms. It does not originally belong to the grammatical content of the Python language. However, recursion is discussed in every programming language teaching series. That is because if you master the methods and techniques of recursion, you will find that this It’s a great programming idea!
##Having said so much, I haven’t talked about the concept of recursion in programming! Recursion, in principle, is the function call itself. A behavior, let’s look at a factorial example:
9. Dictionaries and collections:
(1) Create and access dictionaries:
is created by {}:
Create a dictionary through dict():
It is recommended to use {} to create a dictionary, which looks comfortable;
fromkeys()The method is used to create and return a new dictionary. It has two parameters: the first parameter is the key of the dictionary; the second parameter is optional and is passed in The value corresponding to the key. If not provided, the default is None. For example: -------------------------------------------------- -------------------------------------------------- ------------ key(), values() and items(): keys() is used to return the dictionary keys, values() is used to return all values in the dictionary, then items() of course returns all key-value pairs (i.e. items) in the dictionary, for example: ------------------------------------------------- -------------------------------------------------- ---------- get(), in and not in: The get() method provides a more relaxed way to access the dictionary item, when the key does not exist, the get() method does not report an error, but silently returns a None, indicating that nothing was found; if you want to return the specified value when the data cannot be found, you can use the second The default return value corresponding to the parameter setting; If you don’t know whether a key is in the dictionary, you can use the membership operator (in or not in) to judge; --------------------------------------- -------------------------------------------------- ------------------ clear() clears a specified dictionary: Some students may think that using Clear the dictionary by assigning the variable name to an empty dictionary. This has certain drawbacks. Let’s take a look at an example; --------- -------------------------------------------------- -------------------------------------------------- The copy() method is to copy the dictionary: -------------- -------------------------------------------------- ------------------------------------- pop() And the popitem() method: pop() pops up the corresponding value for a given key, and popitem() pops up an item. These two are easier: The setdefault() method is somewhat similar to the get() method, but setdefault() will automatically add it when it cannot find the corresponding key in the dictionary: update() method is used to update the dictionary: (In fact, there is a method above, but this one is more authentic) 10. Sets: In Python3, if you enclose a bunch of numbers in braces but do not reflect the mapping relationship, then Python will think that the bunch of things is a set. Since the elements in the set are unordered, they cannot be accessed using subscripts like sequences. But you can use iteration to read the data in the collection one by one: (add() and remove() methods) Sometimes you want the data in the collection to be stable, that is, like a tuple, elements in the collection cannot be added or deleted at will. Then we can define an immutable collection. The frozenset() function is used here. Yes, it freezes the elements: (2) Dictionary built-in method:
This example tells us that we cannot always take things for granted. Sometimes reality will give you a hard blow. The fromkeys() method does not The values "one", "two" and "three" will be assigned to keys 1, 2 and 3 respectively, because fromkeys() treats ("one", "two", "three") as one value.
------------------------------------------------ -------------------------------------------------- -----
The above is the detailed content of Analyze the basic syntax of Python in simple terms. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.
