4, python basic grammar

Introduction: This paper describes some basic python syntax, including the definition of definition identifiers, line breaks and indentation, quotes and comments, input and output variables.

First, the identifier


 1. Where we take the name, all identifiers.

2. In Python, identified by letters, underscore (_) and numbers, and can not begin with a number, for example:

readBook43     # valid identifier 
the Read # 9 # illegal identifier, which can not contain the # symbol 
3read      # illegal identifier, the identifier can not start with a number

3. In Python, the identifier is strictly case-sensitive, such as: apple and Apple are different identifiers.

4. The identifier may be a function name, class name, variable name, module name, project name.

The identifier can not use keywords , you can view all of the keywords python by keyword.kwlist.

import  keyword
print(keyword.kwlist)

   Results of the:

C:\software\python\python.exe D:/myworkspace/test/test/test.py
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def',Of the'', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Process finished with exit code 0

In addition, in order to regulate naming identifier, which has a convention wording:

1) See known name meaning: a meaningful name, try to make a quick glance to know what it means identifier, improving the readability of the code. E.g,

 Use teacher to teacher, said students use to represent the student, try to avoid using Pinyin.

2) According to the father of Python Gud recommended practices, when named as a variable in Python, a suggestion for the beginning of the class name in capital letters (such as CapWorld),

 Also called the camel nomenclature, the module name should be underlined lowercase mode (such as low_with_under).

 

Second, line breaks and indentation


 1.Python other languages ​​is the biggest difference, Python code blocks without using braces {} class controlled, functions, and other logic judgment. python most unique is to use indentation to write modules.

2. General The default indent is four spaces, but in fact the number of spaces to indent is variable, but all statements block must contain the same number of spaces , this must be strictly enforced.

3. Multi-line statement:Python statements generally a newline terminator statement. But we can use the slash (\) will line statement into multiple lines.

# Equal to = A "drftuyhjiko, lprtguyhjikm" 
A = " drftuyhjiko, lprtguyhj " \
     " IKM "

 

Third, quotes and comments


 1.Python quotes

  python may be used single quote ( '), double quote ( "), tris quotation marks (' '') or (" "") for strings, but must be quoted in pairs. Wherein three marks may be composed of lines,

Write multiple lines of text shorthand syntax, commonly used in the documentation string, the file in a specific location, is treated as a comment. (That is, we are talking about multi-line comments)

= A ' Read '       # use single quotes for string 
B = " Book "       # double quotation marks string 
C = '' ' Book ' ''    # tris single quotation marks string 
D = "" " Book " ""    # tris double quotes string representing 
E = "" " Student 
        Read 
        Book 
    " ""      # tris double quotation marks represent multi-line character string 

"" " 
But here is 
where the comment is 
" ""

2.Python comments

 . 1) Python employed in single-line comments beginning #. Shortcut keys: Ctrl + /

 2) Python multi-line comments three-quotation marks ( '' ') or ( "" ") enclose pairs

"" " 
But here is an 
" "" 
# this is a comment 2 
'' ' which is a comment 3 ' ''

 

Fourth, the input and output


 1.input input:

 obtaining a data input from the console inside the acquired data type is a string type.

2.print output:

  default output is print line feed (hereinafter i.e. content printed output has a line feed), separated by commas different data, in order to output a single line without line feed,

Can be written at the end of End = '' (here two single quotes)

the INPUT = book_name ( " Please enter the name of the book: " )   # After running from the console input content 
book_price = ' ¥ 38 ' 
Print (book_price)
 Print (book_name, End = '' )
 Print (book_price)

 operation result:

C: \ Software \ Python \ python.exe D: / myworkspace / the Test / the Test / test.py 
enter the name of the book: Hundred Years of Solitude 
¥ 38 
Hundred Years of Solitude ¥ 38 

Process Finished with Exit code 0

 

Five variables


 1. What is variable

  Variable value stored in memory . It will open up a space in memory to create variables. Based on the data type of the variable, the interpreter allocates specified memory, and decide what data

It can be stored in memory. Thus, the variable can specify different data types, these variables may be stored integer, decimal or character. View memory address: id ()

Question: If a = 1, a = 2, the final printed value?

Question 2: x = 1, y = 1 is a shared memory space or open up a new memory space?

1 = a 
a = 2
 Print (a)     # print result of 2, because the python code is executed from top to bottom, a first variable assignment 1, again assigned to a 2 

X = 1 
Y = 1
 Print (ID (X ))
 Print (the above mentioned id (y))     # operation has demonstrated the x and y share one memory space, because the python is an integer opened up a separate space from -5 to 256 stored in the cache, 
          # no matter how many variables created will assign it a value of 1, their memory addresses are the same, more about memory can study on their own Baidu

2. variable assignment

  The python variable assignment does not require type declaration. Each variable created in memory, both including identification information and data variables. Each variable must be assigned before use ,

This variable will be created after the variable assignment.

  Equal sign (=) is used to assign values to variables, the equal sign (=) operator is a variable name on the left, the right is the value stored in the variable. For example: name = "Michal" 

3. The name of the variable naming conventions

  Contain numbers, letters and underlined, you can not start with a number, all lowercase, see the name to know Italian.

Guess you like

Origin www.cnblogs.com/miki-peng/p/12222592.html