Java users quickly understand Python syntax (1)

Tip: The following is the main content of this article, and the following examples are for reference.

1, Python identifier quotation mark annotation

identifier .

  1. Python identifiers consist of letters, numbers, and underscores, which are case sensitive but cannot start with a number.
  2. Start with a single underline_Foo represents class properties that cannot be directly accessed and must be accessed through the interface provided by the class. It cannot be imported using from xxx import *.
  3. Starting with double underline__Foo represents the private member of a class, and __foo__, which starts and ends with double underscores, represents the identifier specific to special methods in Python, such as __init__() representing the constructor of a class.

Quotation marks
Python can use quotation marks ('), double quotation marks ("), triple quotation marks (' 'or') to represent strings, and the beginning and end of quotation marks must be of the same type.

Triple quotation marks can be composed of multiple lines, and write shortcut syntax for multi line text
The code is as follows: .

word = 'word'
sentence = "sentence."
paragraph = """this is a paragraph. 
 multiple statements"""

annotation .

Single line comments in Python start with #
Multiple line comments in Python use three single quotes or three double quotes;

2, Python variable types

Python has five standard data types:

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

1. String.

Python string value order .

  1. Indexing from left to right, starting from 0 by default
  2. Index from right to left, starting with -1 by default

You can use [header index: footer index] to truncate the corresponding string (the result includes header index, not footer index) .

2. Python List.

List marked with [].

3. Python tuples.

The tuple is identified by (). Internal elements are separated by commas. However, tuples cannot be reassigned, which is equivalent to a read-only list. You can use the del statement to delete the entire tuple.

4. Python Dictionary.

Dictionary usage; {}" Identification. A dictionary consists of an index (key) and its corresponding value. A list is an ordered collection of objects, a dictionary is an unordered collection of objects, the elements of a list are accessed through offsets, and the elements of a dictionary are accessed through keys.

3, * * and in Python arithmetic operators//

one (Power)**.

 10 * * 20 represents the 20th power of 10 

2// .

>>> 9//2
4     #returns the integer part of the quotient (rounded down) 
>>> -9//2
-5

Pass statement .

pass do nothing and use it as a placeholder statement 

4, Python conditional statement, loop statement

Python conditional statements .

Python specify any non-0 and non-empty values( null )value is true , 0 or null by false

Python While Loop Statement .

while judging conditions : #related to Java compared to judging conditions, there is no need for parentheses, curly braces, and an additional one is added":"。
 execute statement …… #the ones with the same space indentation below are all loop bodies 

Python for loop statements .

for iterating_var in sequence:
statements(s)

**Break in Python, continue**.

break breaking the minimum barrier for or while loop 
continue statement jumps out of this loop 

5, Python functions

Define function method: .

  • The function code block starts with the def keyword, followed by the function identifier name and parentheses.
  • Place the incoming parameters in parentheses.
  • The first line of the function statement can selectively use a document string - used to store the function description.
  • The function content starts with a colon and is indented, with the same indentation for statements in the same method body.
  • Return [expression] ends the function and returns a value to the caller. A return without an expression is equivalent to returning None.
def functionname( parameters ):
"function _document string"
function_body
return [expression]

The problem of parameter transfer in functions .

In Python, strings, tuples, and numbers are immutable objects, while lists, dicts, and so on are modifiable objects:

  1. Immutable type: Similar to value passing in Java;
  2. Variable type: Similar to reference passing in Java.

Essential parameters, keyword parameters, default parameters, variable length parameters.