Python suitable for network siege lion learning-basic syntax (comments, methods and functions)

A certain network siege lion used topology to make a self-portrait and accidentally made himself handsome
Insert picture description here

1. Notes

1. Definition

Annotations can serve as remarks. In teamwork, the code you write may be called and maintained by others. In order to make it easier for others to understand the purpose and purpose of the code you wrote, it is very necessary to use comments in the code.

2. Examples

In Python, # is used as a comment symbol. In most cases, we use comments in script mode, but rarely in interactive mode.

#coding=utf-8
#生成一个整数列表,该列表为整数1~10的平方数的集合
test_list = [i**2 for  i in range (1,11) ]
print (teat_list)

Maybe you can’t understand this code, but through comments you can know that its function is to "generate a list of integers, which is a set of squares of integers 1 to 10", that is, 1, 4, 9, 16..., 100 .

If you use script mode to run python, and Chinese appears in the code, you must add "#coding=utf-8" at the beginning of the code, because the default encoding format is ASCII. If you do not modify the encoding format, python will not be able to Chinese is displayed correctly.
Insert picture description hereInsert picture description hereInsert picture description hereBecause the code behind # is commented, it will not be executed as part of the code, so using # can also "cover" the code we don't want to execute.

Two, methods and functions

Method (Method) and function (Function) are generally two interchangeable words.
Subtle differences:

  • Functions are independent functions and do not need to be associated with objects
  • The method is related to the object, it can be used without passing data and parameters

The type() function
Insert picture description here
method requires an object (variable or data) association. For example, uppper() is a method that converts the lowercase letters in the string into uppercase English letters.
Insert picture description here
Insert picture description hereverdor and a are variables, these two variables call the upper() method, and the return value, that is, all letters, are all capitalized.

In Python, each data type has its own default functions, methods and variables. To view the functions, methods and variables of a certain data type, use the dir() function.
Insert picture description here

  • " Contains ": built-in variable
  • "_Formatter_parser": private variable
  • Network engineers who are new to python only need to know these two.

Guess you like

Origin blog.csdn.net/weixin_44309905/article/details/114794066