[Summary] Python and C language, Java language and other basic syntax differences

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

[Summary] Python and C language, Java language and other basic syntax differences

This article introduces the main difference between Python and other languages ​​(such as C, Java, etc.), and allow the reader to quickly understand and master the basic syntax of Python, quickly learning from other languages ​​over to Python in the past.

1, sentence structure

Python is a program ends do not end with a semicolon ";", compared to most other languages ​​concise many

2, program output

Python special print functions provided in the output () function, the following statement:

print(*Object, sep = ' ', end = '\n', file =  sys.stdout, flush = False)

Python be directly output string, numeric, a variable (including numeric, boolean, list of tuples, dictionaries, etc.), can be formatted output, the output does not wrap, a multi-value output and other operations.

  • Output Format For example:
print("The length of %s is %d" % (s, n))
  • Output does not wrap Example:
for x in range(0, 3)
     print(x, end = '、')

3 comments

Python annotated with the symbol "#" at the beginning

For example:

# 这是一行注释

Python has a unique way of comment, it is to use the documentation string. Document string bag, modules, classes or functions in the first statement, using a triple double quotes tissue, since contents of the package may be automatically extracted by a members _doc_ object, and is used pydoc.

4, code style

Most languages ​​use curly braces to indicate blocks of code statements, but in Python, using indentation to represent a block of statements and Python indentation on the very strict requirements, do not indent the first line of the source file

5, variable

Python is not supported in the variable increment (+), decrement (-) operator.
In Python -n to n, ++ n also n.

6, the digital type

The Python There are three types of numbers:

  1. int int
  2. float float
  3. complex complex types

7, Boolean type

Python3, the following are False:

  • None
  • False
  • 0
  • 0.0
  • 0.0 + 0.0j
  • “”
  • []
  • ()
  • {}
  • _bool_ _len or user-defined class instances implemented () _ Method

In addition to the above, are True.

8, the string

Python has no notion of the characters, although only a character also belong to the string type.

Python quoted string is contained, it may be a single quotes may be double-quotation marks, or three marks.

String having indexing rule, a first predetermined index of 0, and so on.

You can use the index to find, the value of the operation:

For example:

str[0]
str[0:6]
str[6:]
str[:6]

* + Can also be used or the string is connected and repeat

For example:

str1 + str2
str3 = str1 * 3

9, the sequence

  1. Tuple: with () is defined, the elements can not be modified.
demo_tuple = (1,2,3,4) # 这是一个元组
  1. List: with [] define the number of elements, the value can be freely modified.
demo_list = [1,2,3,4] # 这是一个列表

10, dictionary

Map data type, such as a shaped key: value of the key components. You may store different types of elements, defined by {}.

demo_dict = {"name":"张三","age":21}  # 这是一个字典

11, a set of

Python, is an unordered set, the data set will not be repeated.

Role is as follows:

  1. Deduplication: the list to become a collection, will take the initiative to go heavy.

  2. Relationship test: test whether there are two sets of data cross (&), difference (-), and (|), XOR (^) relationship.

Python utility in braces {}, or set () function to create the collection.

NOTE: To create an empty set, use must be set () function, {} creates an empty dictionary.

12, if the statement

  • if the condition does not require the use of parentheses contain the expression, but the use of a colon ":" indicates the end of a conditional expression
    for example:
if 表达式:
   执行语句
  • Multiple execution statement does not require the use of curly brackets, through indentation.

For example:

if 表达式:
   执行语句1
   执行语句2
   执行语句3

Else if other languages ... as in Python elif ...
For example:

if 表达式1:
   执行语句1
elif 表达式2:
   执行语句2
else 表达式3:
   执行语句3

Note: Python is if nesting by indentation achieved.

Judgment expression

No Python ternary operator, but a similar function can be achieved by determining expression.

为True时表达式 if 判定条件 else 为False时表达式

While loop

Python provides for a while ... else ... the cycle, while at the statement following else after the implementation of execution.

Note: while the nested loop through the code indentation is achieved.

For example:

while 条件表达式1:
    执行语句1
    执行语句2
    ...
    while 条件表达式2:
        执行语句3
        执行语句4
        ...

For loop

Python is used for circulating through the elements of the sequence (such as a string, a tuple, a list, a set of dictionaries or the like) or other objects iterations.

Format is as follows:

for 临时变量 in 可迭代对象 :
   执行语句1
   执行语句2
   ...

For example;

# 遍历字典
demo_dict = {"name":"张三","age":21}
for key in demo_dict :
    print(key + "的值是:" + dict[key])

Article link

Python in loop on what skills it?
[Summary] cycle analysis techniques in Python

Guess you like

Origin blog.csdn.net/weixin_43876206/article/details/93380958