In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Python basic grammar example analysis". In daily operation, I believe many people have doubts about Python basic grammar example analysis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Python basic grammar example analysis". Next, please follow the editor to study!
I. coding
Python source files are encoded in UTF-8 by default, which can be used to represent any character in the Unicode standard. Of course, you can also specify a different encoding for the source file, such as the following:
#-*-coding: cp-1252-*-
The above definition allows the use of character encodings in the Windows-1252 character set in the source file, and the corresponding languages are Bulgarian, Blanc, Macedonian, Russian, and Serbian.
II. Keywords
Keywords, also known as reserved words, cannot be used as variable names, function names, class names, and so on. As reserved words in the Python language, they have a specific function and cannot be used as any identifier name. We can view all the keywords for Python in the following ways:
View the results:
['False',' None', 'True',' and', 'as',' assert', 'break',' class', 'continue',' def', 'del',' elif', 'else',' finally', 'for',' from', 'global',' if', 'import',' in', 'is',' lambda', 'nonlocal',' not', 'or' 'pass', 'raise',' return', 'try',' while', 'with',' yield'] III. Identifier
The so-called identifier can be simply understood as the name of variable, class, function and so on. Therefore, the identifier cannot be duplicated with the Python keyword mentioned above. Identifiers are not randomly named, and their naming rules are as follows:
Identifiers must start with a letter or _, such as title, title66, _ title, etc.
The rest of the identifier can be a letter, _, or number
Identifiers cannot be keywords, such as for, import, and so on.
Identifiers are case sensitive.
Among them, the identifier beginning with the underscore _ has a special meaning, as follows:
Representatives starting with a single underscore _ cannot be imported with from xxx import *, such as _ module
Private members of a class that begin with a double underscore, such as _ _ member
Beginning and ending with a double underscore represent identifiers specific to special methods in python, such as _ _ init__ () represents the constructor of the class and is used by the interpreter.
It is also worth noting that in Python3, Chinese can be used as an identifier, and non-ASCII identifiers are also allowed.
IV. Notes
Annotations are intended to explain the program and facilitate programmers' understanding and future maintenance of the program. Comments serve as prompts only and do not execute comment statements.
There are two ways to annotate in Python:
One-line comments, using the symbol #. The usage is as follows:
# the first Python program print ("Life is too short, I'll learn Python!")
Multiline comments, using multiple double quotes "and". The usage is as follows:
"" author: AI bacteria function: the first Python program date: 2020-12-20 "" print ("Life is too short, I learn Python!") Fifth, indent
The most distinctive feature of Python is the use of indentation instead of curly braces {} to represent code blocks. In Python, the first four consecutive spaces of a sentence represent an indentation. By indenting to represent a block of code, the code is more concise. Let's briefly demonstrate the syntax of indentation:
If True:print ("You are right!") else:print ("Error!")
It should be noted that it must be 4 consecutive spaces, if you enter 2 or 3 spaces, the program will report an error.
Line wrapping of long sentences
We usually write a sentence one by one. But if the statement is long, we can use the backslash\ to wrap the long sentence, for example:
Str = "Life is short," +\ "I learn Python" +\ "!"
The running results are as follows:
If it is a long statement in [], {}, or (), you do not need to use a backslash\, you can wrap the line directly, for example:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"]
The running results are as follows:
At this point, the study of "Python basic Grammar example Analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.