python-2020-6-1笔记

    技术2024-10-21  47

    程序=数据结构+算法

    数据结构是存储和使用数据的方式

    算法是解决问题的步骤

    解决一个问题的时候,分析问题,设计算法,编写程序,调试和出结果

    变量:可以改变的词

    常量:不可以改变的词

    变量类型:不同类型的变量存储不同类型的值,Python是弱语言类型,不需要提前声明变量

    举个栗子:

    a=1

    1是在内存中保存的

    a是个指针,指针的是1在内存中的地址,所以使用a的时候,可以访问到内存的1

    a=1

    ype(a)

    >>> t<class 'int'>

    id(a)

    >>> t140735413081760

    id(a)

    >>> t140735413081760

    A is 1如果为true的话,要求a和1在内存中的地址是一样的

    a is 1

    >>> <stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?

    True

    dir(__builtins__)#两个下划线,查看自带的类型,内置类型,就是内部提供的函数

    查看数据类型:

    整数int:a=1  type(a)

    小数float:b=2.0  type(b)

    字符串str:  C=”123”  type(c)

    复数complex:d=12+4j  type(d)

    列表list:e=[12,3,[23,”s”]]  type(e)

    元组tuple:  f=(12,”sdf”)  type(f)

    字典dict:  h={12:34,”we”:45}  type(h)

    集合set:g=set(“asdf”)  type(g)    s=set(["as",1,2,1,2])--》>>> type(s)---》<class 'set'>--》 s--》

    {'as', 2, 1}可以排重

     f=frozenset({2,3})

    >>> type(f)

    <class 'frozenset'>#不可以改变集合的值

     

    布尔型bool:  i=([1,2,”d”])   type(i)

    Pow(2,3)表示2的3次方,或者写成2**3

    A%b表示取余

    import math

    math.floor(1/2)#向下取余

    >>> 0

    math.ceil(1/2)#向上取余

    >>> 1

    round(3.8)

    >>> 4#四舍五入

    round(2.345,2)#四舍五入,保留2位小数

    >>> 2.35

    将变量恢复即不想使用这个变量

     list=1

     list

    >>>1

    del list

     list

    >>><class 'list'>

    S=”中国” python 3:unicode,python2等价于bytes

    S=u”中国” Python2,Python3,:Unicode

    Python3:s.encode后,变为bytes 等价于Python2的str类型

    Python2:s.encode变为Unicode类型,等价于Python3的str类型

     

    判断数据类型

    Isinstance(a,int)

    Isinstance(True,bool)

    Isinstance(23.0 float)

    ....

    数据类型转化

    整数->小数:float(23)

    小数->整数:  int(23.45)

    整数->字符串: str(123)

    小数->字符串: str(123.455)

    字符串->整数: int(“123)

    字符串->小数:float(“123”)

    输入函数:s=input()  #不管输入是什么,都是存储str判断bool的真假

    单一值:

    [],{},(),””,’’,False,””””””,’’’’’’除了这些基本都是真,具体的逻辑都要根据实际判断

    Eg:bool(0)  bool([])...都是假的

    遍历思想

    list(range(0,12))-->会打印0-11的数字12个数字

    range(0,12)-->会打印[0,11]

     

     #会打印出0,2,4,6,8

    for i in range(0,10,2)

    print(i)

    i=5#会打印出5,6,7,8,9

    while  i <10:

    print(i)

    i+=1  #表示在原i的基础上加1

    Processed: 0.009, SQL: 9