Python basic grammar (a): Notes and variables

Pictures with dimensions
On an article that the Python language features, where the official start learning Python. Python's basic grammar is not much, but want a detailed description is not able to finish the.
Here mainly talk about the comments and variables

Note

  Role comment: somewhere in the program to add some descriptive information, do not participate in the operation of the program.
  A comment in Python divided into two types:
    The first is a single-line comment

# 注释的内容,我是单行注释,由#开头,后面是注释的内容

    The second is a multi-line comments, composed of three pairs of double quotation marks, quotation marks if the English quotation marks. The Chinese did not use quotation marks

"""
注释的内容
我是多行注释
"""

  In fact, there are two small applications. It is seen as a way documentation comments, when you later write more code files, and then look back at your previous one code files do not know what to write. Perhaps you have to look for a moment they would understand. This time you can do this:

# TODO 注释内容

Moreover, this comment will be highlighted. Very convenient you find the code you want in a huge space in the code
  and the other is you write code written in more than one file, you do not know what use is a function of. This time you can do:

def yanshizhushi():
    """函数的用途"""

The advantage of this is that when your mouse to point this function will be displayed directly on your comments. Do not look inside the code to open the function

variable

  What is a variable?
  I understand it is a variable space. Like a house the same. We can use to live in, can be used as an office, it can be used for the Treasury. It can be used for what is called use. How do we find it in our house a lot of the house? In real life you may have names, community name, building number, floor, house number and so specific way to find this house. How to find our variable it in the computer? This comes on variable naming, we can give the variable a name. We use this name when he can be seen as a call to this variable.
  How to define a variable that does?

a=1
# 这就定义了一个变量a
print(a)
# 当我们调用a时就会输出 a 的值 1

定义变量:变量名 = 数值
调用变量:变量名

  In practice, however, we will not develop as a variable name with a. If so used, I feel your colleagues will love you.
  Named Variable pay attention to see to know the name of justice. That would see the variable name probably know what to do with it or where to use. And there are two variable naming convention:

  Underline nomenclature:

    Normally ordinary variable names or write function is used, the letters lowercase. If multiple words are words often used to underline split.

my_name = 余欢
  Large hump nomenclature:

    This will be used when writing the name of the class or write some constants.

MyName = 余欢

Conventional data is basically divided into these types:
  global variables : writing in a file, the entire file is valid.
  Local variables : a writing process. Effective range variable defining the start position to the end of the method ends.
  Private variables : write in class or function, a function or a class private variables, which can only be in use. You can not call outside.
  Public variables : Write in the class, all the functions belonging to this class can use this variable.
  Unique variables : Write a function in class, the other functions of this class can not be used.
  Class variables : write in class variable called class variables.
  Constant : is a variable after variable definition will not change is called a constant.

"""
# 变量称呼
全局变量
局部变量
公有变量
独有变量
私有变量
类变量

"""
CHANGLIANG = "常量"
quan_ju = "全局变量"

def han_shu():
    global quan_ju
    quan_ju = "全局变量" # 通过global 声明局部变量就变成了全局变量
    ju_bu = "局部变量"

class Lei():
    Lei_bianliang = "类变量"

    def __init__(self):
        self.__siyou = "公有变量,私有变量"
        self.duyou = "公有变量"

    def fangfa(self):
        self.bianlaing = "公有变量" # 只要带self的就是公有变量,不管是不是在init方法中


shili = Lei()
shili.bianliang = "独有变量" # 实例对象独有的叫做独有变量

Types of variables can be divided into two categories:
  Numeric : int Integer
      float type
      boolean bool
  non-numeric type : string str

num = 1
print(type(num)) # 这个时候num是整型 int
num = 1.1
print(type(num))# 这个时候num是浮点型 float
num = True
print(type(num))# 这个时候num是布尔型 bool
num = False
print(type(num))# 这个时候num是布尔型 bool
num = "1"
print(type(num))# 这个时候num是字符串 str

Of course, the type of the variable is converted

num = 1.1
print(type(num))# 这个时候是浮点型
int(num)# 转换为整型
print(type(num))
str(num)# 转换为字符串
print(type(num))

"""
布尔型只有两个:True和False
True是任何非0数字
False等于0
"""
print(True)# 输出为 1
print(False)# 输出为 0

Python identifier
  naming variables but also to meet naming identifiers.
  Identifier naming rules:
     1. Only numbers, letters, (_) underscore.
     2. Do not start with a number.
     3. not be a keyword.
     4. The case-sensitive.
  All Python keywords contain only lowercase letters.

and         exec        not
assert      finally     or
break       for         pass
class       from        print
continue    global      raise
def         if          return
del         import      try
elif        in          while
else        is          with
except      lambda      yield

  And to underline the beginning of the identifier is of special significance.
    _Foo begin single underline represent can not directly access the class attributes, accessed through the classes provided from xxx import * can not be introduced.
    In the private members of class representatives __foo beginning double underlined, is double-underlined start and end of the foo Representative Python in particular specific identification method, such as init constructor () represent the class.

Python same row can display multiple statements, method is to use a semicolon; separated, such as:

>>> print ('hello');print ('runoob');
hello
runoob
Python 保留字符
下面的列表显示了在Python中的保留字。这些保留字不能用作常数或变数,或任何其他标识符名称。

  Notes on Python and variables to finish here. Above represent only personal views. If chiefs have different views or understanding, please let us know.Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/xin-yuana/p/12098152.html