003、python基本语法

 

1、变量

  a、用来标识数据

  b、语法: 变量名 = 值 (数据)

 

       变量名的规范:

  a、字母、下划线、数字组成  ;

  b、不能以数字开头 ;

  c、区分大小写 ;

  e、见名知意  ;

  f、不能是关键字 (内置的函数和方法不是关键字 ) ,用如下代码查看关键字: 

import keyword

print(keyword.kwlist)

关键字打印结果如下:

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 

'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',

'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] Process finished with exit code 0

 

 2、注释

class Demo():
    """
    这是多行注释
    """
    
    '''
    这也是多行注释
    '''
    
    # 这是单行注释, 选中行 按 ctrl + /
    pass

 

3、字符串  与  多行字符串  '''  ''''   、   """  """

address_01 = """
湖南省
邵阳市
大祥区
"""

address_02 = '''
湖南省
邵阳市
大祥区
'''

address_03 = "湖南省邵阳市" \
             "大祥区蔡锷乡"

address_04 = '湖南省邵阳市' \
             '大祥区蔡锷乡'

address_05 = '湖南省邵阳市大祥区蔡锷乡'
address_06 = "湖南省邵阳市大祥区蔡锷乡"


print('address_01: ' + address_01)
print('address_02: ' + address_02)
print('address_03: ' + address_03)
print('address_04: ' + address_04)
print('address_05: ' + address_05)
print('address_06: ' + address_06)
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo_04.py
address_01: 
湖南省
邵阳市
大祥区

address_02: 
湖南省
邵阳市
大祥区

address_03: 湖南省邵阳市大祥区蔡锷乡
address_04: 湖南省邵阳市大祥区蔡锷乡
address_05: 湖南省邵阳市大祥区蔡锷乡
address_06: 湖南省邵阳市大祥区蔡锷乡

Process finished with exit code 0
View Code

 

4、Python3 的六个标准数据类型 :

  • 不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组);
  • 可变数据(3 个):   List(列表)、Dictionary(字典)、Set(集合)。
# 不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组);
# 可变数据(3 个):   List(列表)、Dictionary(字典)、Set(集合)。

# python 小整数常量池:
#  特点:
#   1.整数范围: 范围[-5, 256]
#   2.它永远不会被GC机制回收, 只要定义的整数变量在 范围: -5 -- 256内,会被全局解释器重复使用, 257除外
#   3.只要在这个 -5 -- 256 范围内,创建同一区域代码块的变量的值如果是相等的,那么不会创建新对象,直接引用。
#    4.注意:上面对于python小整数对象池的使用仅仅是在命令行中执行可以,而在Pycharm或者保存为文件执行,结果是不一样的,
#           这是因为解释器做了一部分优化。下面使用pycharm,即使整数超过256,id() 也是相同的。

# 整数类型
a = 257
b = 257
print(id(a))
print(id(b))

# 浮点数类型
c = 3.14159
print(c)

# 字符串类型
str_demo = 'hello world'
print(str_demo)

# 元组类型 Tuple
t = (1,)        # 1个元素的元组
print(type(t))
print(t)
t01 = (1, 2, 3, 4)
print(t01)


# List 列表
list_temp = [1, 2, 3, 4]
print(list_temp)


# Dictionary(字典)
dic_temp = {'name':'sky', "age":25}
print(dic_temp)

# set(集合)去重功能
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)
a = set('abracadabra')
print(a)
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo_04.py
2998045334512
2998045334512
3.14159
hello world
<class 'tuple'>
(1,)
(1, 2, 3, 4)
[1, 2, 3, 4]
{'name': 'sky', 'age': 25}
{'orange', 'apple', 'pear', 'banana'}
{'b', 'd', 'c', 'a', 'r'}

Process finished with exit code 0
View Code

 

 

 

知识补充:

 1_a、python 中 _ 和 __  的区别:

  xx:      公有变量,所有对象都可以访问;
  _xx:      单前置下划线,私有化属性和方法,form 包名 import * 禁止导入,类对象和子类可以访问,使用  对象._变量名调用
  __xx:     双前置下划线,避免与子类中的属性命名冲突,无法在外部直接访问,应使用  对象._类名__变量名调用  ;
  __xx__:    双前后下划线,用于定义类的魔法属性/魔法方法,例如:__init__,__str____del__ 等;
          为什么叫魔法方法,因为不需要显式调用,看起来魔法一般;
  xx_:    单后置下划线,用于避免与python关键字的冲突。

 

1_b、在python的类中,没有真正的私有化,不管是方法还是属性,为了编程的需要,约定加了下划线 _ 的属性和方法, 不属于API,不应该在类的外面访问,也不会被  from M import *  导入。

参考学习: Python中 _ 和 __ 的含义

dir() 是 directory() 简写,目录的意思;

代码如下:

'''
在类A中定义了一个_method方法,按照约定是不能在类外面直接调用它的,
为了可以在外面使用_method方法,又定义了method方法,method方法调用_method方法。
请看代码演示:
'''


class A:
    def _method(self):
        print('约定为不在类的外面直接调用这个方法,但是也可以调用')

    def method(self):
        return self._method()


a = A()
print(dir(a))   # 打印对象a所有的方法和属性
a.method()
a._method()
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo.py
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_method', 'method']
约定为不在类的外面直接调用这个方法,但是也可以调用
约定为不在类的外面直接调用这个方法,但是也可以调用

Process finished with exit code 0
View Code

 

1_c、 __xx: 双前置下划线,避免与子类中的属性命名冲突,无法在外部直接访问,应使用对象._类名__变量名调用 ;

代码如下:

'''
在类A中,__method方法其实由于name mangling技术的原因,变成了_A__method,
所以在A中method方法返回的是_A__method,B作为A的子类,只重写了 __method 方法,
并没有重写method方法,所以调用B中的method方法时,调用的还是 _A__method 方法:
在A中没有__method方法,有的只是_A__method方法,也可以在外面直接调用,所以python中没有真正的私有化 ;

C中重写method方法,C 中的method方法会调用_B__method方法 ;
'''


class A:

    def __method(self):
        print('This is a method from class A')

    def method(self):
        return self.__method()


class B(A):
    def __method(self):
        print('This is a method from calss B')


class C(A):
    def __method(self):
        print('This is a method from calss C')

    def method(self):
        return self.__method()


a = A()
print(dir(a))
a.method()

b = B()
print(dir(b))
b.method()

# print(a.__method())     # 报错 AttributeError: 'A' object has no attribute '__method'
print(a._A__method())       # 对象._类名__变量名调用

c = C()
c.method()
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/gggg/demo_3.py
['_A__method', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'method']
This is a method from class A
['_A__method', '_B__method', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'method']
This is a method from class A
This is a method from class A
None
This is a method from calss C

Process finished with exit code 0
View Code

 

 

1_d、特殊成员和 常用魔法方法

参考学习视频  : https://www.bilibili.com/video/BV14v411B7pB?t=170

  __new__() 方法   和    __init__() 方法  、 __del__()  方法

  __new__方法接受的参数虽然也是和 __init__一样, 但 __init__ 是在类实例创建之后调用,而 __new__方法正是创建这个类实例的方法 。

       示例代码如下:

class Cat():
    """
    这是一个猫类
    """

    def __new__(cls, name, age, colour):
        print('__new__ 了一只猫')
        return super(Cat, cls).__new__(cls)

    def __init__(self, name, age, colour):
        self.name = name
        self.__age = age
        self._colour = colour
        print('__init__, 我是一只猫,我的名字叫:{0}'.format(self.name))

    def __del__(self):
        print('__del__,我被系统回收了')


#  __new__方法接受的参数虽然也是和__init__一样,但__init__是在类实例创建之后调用,而 __new__方法正是创建这个类实例的方法。
cat = Cat('Tom', 5, '黄色')
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo_03.py
__new__ 了一只猫
__init__, 我是一只猫,我的名字叫:Tom
__del__,我被系统回收了

Process finished with exit code 0
View Code

 

 

  __call__()方法

  call():  可以让类的实例具有类似于函数的行为; 使用方式:对象后面加括号,触发执行 。即:对象()或者类()

  示例代码如下:

class Cat():
    """
    这是一个猫类
    """
    def __call__(self, *args, **kwargs):
        print('__call__,我是一只猫,我被调用了。')


cat = Cat()
cat()           # 调用了 __call__ 方法
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo_03.py
__call__,我是一只猫,我被调用了。

Process finished with exit code 0
View Code

 


__str__() 和  __repr__()  方法

  如果找不__str__()  就会找到  __repr__()  方法。 __repr__()  是  __str__()  的备胎 ;

  示例代码如下:

class Cat():
    """
    这是一个猫类
    """

    def __init__(self, name, age, colour):
        self.name = name
        self.__age = age
        self._colour = colour
        print('__init__, 我是一只猫,我的名字叫:{0}'.format(self.name))

    # def __str__(self):
    #     return '__str__,' + '  ' +  self.name + '  ' + str(self.__age) + '  ' + self._colour

    #  repr():改变对象的字符串显示
    # 此方法是__str__()的备胎,如果找不__str__()就会找到 __repr__()方法。
    # repr()方法默认调用__repr__()方法
    def __repr__(self):
        return '__repr__, 我是__repr__魔法方法 。'

    def __del__(self):
        print('__del__,我被系统回收了')


cat = Cat('Tom', 5, '黄色')
print(cat)          # 如果找不__str__()就会找到 __repr__()方法。
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo_03.py
__init__, 我是一只猫,我的名字叫:Tom
__repr__, 我是__repr__魔法方法 。
__del__,我被系统回收了

Process finished with exit code 0
View Code

 

python中的比较  is  和 ==

  is比较俩个对象的id值是否相同,是否指向同一个内存地址;
  ==比较的是俩个对象的内容是否相等,即内存地址可以不一样,内容一样就可以了;
  实列对象  ==  比较时 , ==  默认会调用对象的 __eq__() 方法,继承自 object 的  __eq__ 方法比较俩个对象的id 。

# is比较俩个对象的id值是否相同,是否指向同一个内存地址;
# ==比较的是俩个对象的内容是否相等,即内存地址可以不一样,内容一样就可以了;
# ==默认会调用对象的__eq__()方法,继承自object的__eq__方法比较俩个对象的id 。

lst1 = [1, 2, 3]
lst2 = [1, 2, 3]
print(id(lst1))
print(id(lst2))
print(lst1 == lst2)
print(lst1 is lst2)


class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # def __eq__(self, other):
    #     return self.__dict__ == other.__dict__


per1 = Person('lee', 10)
per2 = Person('lee', 10)
print(id(per1))
print(id(per2))

print(per1 == per2)
print(per1 is per2)
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_05.py
1719389587584
1719389588096
True
False
1719389810640
1719389612208
False
False

Process finished with exit code 0
View Code

 

    继承 object 的类重写 __eq__ 方法,实现两个对象比较

# is比较俩个对象的id值是否相同,是否指向同一个内存地址;
# ==比较的是俩个对象的内容是否相等,即内存地址可以不一样,内容一样就可以了;
# ==默认会调用对象的__eq__()方法,继承自object的__eq__方法比较俩个对象的id 。

lst1 = [1, 2, 3]
lst2 = [1, 2, 3]
print(lst1 == lst2)
print(lst1 is lst2)


class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    #  重写__eq__ 方法
    def __eq__(self, other):
        return self.__dict__ == other.__dict__


per1 = Person('lee', 10)
per2 = Person('lee', 10)

print(per1.__dict__)
print(per2.__dict__)

print(id(per1))
print(id(per2))

print(per1 == per2)
print(per1 is per2)

# 字典 is 和 ==
a = {'name': 'lee', 'age': 10}
b = {'name': 'lee', 'age': 10}
print(a is b)
print(a == b)
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo_04.py
True
False
{'name': 'lee', 'age': 10}
{'name': 'lee', 'age': 10}
2094091431888
2094091233456
True
False
False
True

Process finished with exit code 0
View Code

 

 

补充知识:

  关于变量名

  1、用关键字作变量名,解释器会直接报错,所以不能用 ;
  2、用内置函数名作变量名,解释器不会报错,但是变量名后面跟了对象,就变成了对象调用,然而这个对象已经不是本身的内置函数了,所以会报不可调用的错误;
  3、如果有些地方实在要用到内置函数名或者关键字,在后面加 _   即可 ;

  用内置函数名str 做变量名  在前, 示例代码:

str = 'bbb'
print(str)

a = 3.14
print(str(a))
View Code

  执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py
bbb
Traceback (most recent call last):
  File "D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py", line 170, in <module>
    print(str(a))
TypeError: 'str' object is not callable

Process finished with exit code 1
View Code

 

  用内置函数名str 做变量名  在后, 示例代码:

a = 3.14
print(str(a))

str = 'bbb'
print(str)
View Code

  执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py
3.14
bbb

Process finished with exit code 0
View Code

 

  变量名 str 加 后下划线  _ ,  str_

str_ = 'bbb'
print(str_)

a = 3.14
print(str(a))
View Code

  执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py
bbb
3.14

Process finished with exit code 0
View Code

 

posted @ 2021-07-25 00:27  空-山-新-雨  阅读(93)  评论(0编辑  收藏  举报