Python——8.基础语法

1.关键字

In [21]: import keyword                                                                                                             
In [22]: keyword.iskeyword                                                                                                             
Out[22]: <function frozenset.__contains__>
In [23]: keyword.kwlist                                                                                                                
Out[23]: 
['False',
 'None',
 'True',
 'and',
...............

2.注释

单行注释#,多行注释可以用多个 # 号、 ‘’’ 和 “”"。
空语句pass
语句换行
total = item_one +
item_two +
item_three
[], {}, 或 () 中的多行语句换行不需要反斜杠。

3.变量声明

赋值即使用,赋值以后该变量才会被创建。变量没有类型. 分配内存后才有。

4.数据类型

Python3 中有六个标准的数据类型:
不可变数据:Number、String、Tuple。
可变数据:List、Set、Dictionary。
Number:int、bool、float、complex (如complex(1,2))

内置的 type() 函数和 isinstance() 可以用来查询变量所指的对象类型。

5.type()

In [4]: help(type)
Help on class type in module builtins:
class type(object)
 |  type(object_or_name, bases, dict)
 |  type(object) -> the object's type
 |  type(name, bases, dict) -> a new type
 |  
 |  Methods defined here:
 |  
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
................................

6.isinstance()

In [3]: help(isinstance)                                                                                                               
Help on built-in function isinstance in module builtins:

isinstance(obj, class_or_tuple, /)
    Return whether an object is an instance of a class or of a subclass thereof.
    
    A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
    check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
    or ...`` etc.

7.del语句

 In [18]: class A: 
    ...:      
    ...:     pass                                                                                                      
In [19]: a=A()                                                                                                                         
In [20]: del a      

————Blueicex 2020/2/22 19:11 [email protected]

发布了118 篇原创文章 · 获赞 1 · 访问量 4507

猜你喜欢

转载自blog.csdn.net/blueicex2017/article/details/104447913
今日推荐