美文网首页
Python学习笔记(一)

Python学习笔记(一)

作者: 一击必中 | 来源:发表于2021-06-25 22:33 被阅读0次

一、数值类型(int float)

  1. python 数字类型,只有两种,int 和 float

  2. type() 可直接得到相应的类型

type(1)
>>> type(1)
<class 'int'>
>>> type(-1)
<class 'int'>
>>> type(1.8888989)
<class 'float'>
>>> type(1+0.3242342)
<class 'float'>
  1. / 代表除 // 代表整除
>>> type(1/2)
<class 'float'>
>>> 1/2
0.5
>>> type(2//2)
<class 'int'>
>>> 2//2
1

二、进制(2进制、8进制、10进制、16进制)

  1. 二进制 0b , 8进制 0o ,16进制 0x ,10进制,直接写

    >>> 0b11
    3
    >>> 0o10
    8
    >>> 0x1F
    31
    >>> 10
    10
    
  2. 二进制转换bin(数),里面可以放任何进制。10进制int(数) 16进制 hex(数) 8进制 oct(数)

    >>> bin(0x122)
    '0b100100010'
    >>> bin(10)
    '0b1010'
    >>> bin(0o23)
    '0b10011'
    >>> int(0o2222)
    1170
    >>> int(0x1121)
    4385
    >>> hex(0xF89)
    '0xf89'
    >>> oct(78978)
    '0o232202'
    >>>  
    

三、布尔类型(bool)

  1. bool类型 True ,False ,注意要大写,他是数字类型。只要是非零都是True,0是False。任何数据类型,只要是非空类型,都是True

    >>> True
    True
    >>> False
    False
    >>> type(True)
    <class 'bool'>
    >>> type(False)
    <class 'bool'>
    >>> int(True)
    1
    >>> int(False)
    0
    >>> bool(2)
    True
    >>> bool(8.9)
    True
    >>> bool("sdfs")
    True
    >>> bool("")
    False
    >>> 
    

四、字符串(str)

  1. 单引号 '' 和双引号“” 区别,没啥区别,就是要求成对出现,和java一样

  2. 三引号(‘’‘),用于进行多行显示,双引号输入三个也是一样。他只是一个方便输入的方式,最终还是需要显示具体的结果。

    >>> '''
    sdfasdf
    asdfasdf
    asdfsadfsadf
    '''
    '\nsdfasdf\nasdfasdf\nasdfsadfsadf\n'
    >>> """hello\n"""
    'hello\n'
    

五、转义字符

  1. \n 换行 ' 单引号 \t 横向制表符 \r 回车

    >>> print('hello \\n world')
    hello \n world
    

六、原始字符串(r)

  1. 在输入前面,加上 r ,字符串是什么内容,就会是什么内容
>>> print(r'helleo \n world')
helleo \n world

七、字符串运算

  1. 字符串相加,拼接字符串

    >>> "helle"+"word"
    'helleword'
    
  2. 字符串相乘,将会重复字符串多次

    >>> "heller" * 3
    'hellerhellerheller'
    
  3. 字符串取值,可以直接取下标,如果加了 - ,将从后面开始取值,但是不支持数组越界情况

    >>> "hello"[0]
    'h'
    >>> "heller"[-1]
    'r'
    >>> "shello"[9]
    Traceback (most recent call last):
      File "<pyshell#56>", line 1, in <module>
        "shello"[9]
    IndexError: string index out of range
    
  4. 获取一组字符,可以使用范围[0:4],中间用 : 标识,如果后面的值是负数,将会反取值

    如果在数组取值的时候,不会越界~~~

    后面如果什么都不输入,则到最末尾~~

    如果从负数开始,后面什么都不输入,则取后几位~~~

    >>> "heller"[0:5]
    'helle'
    >>> "heller"[0:-1]
    'helle'
    >>> "hello"[6:20]
    ''
    >>> "hello"[2:200]
    'llo'
    >>> "hello java helsdf dsjfklsjdklfj sdjkfs dfjskldjf "[6:]
    'java helsdf dsjfklsjdklfj sdjkfs dfjskldjf '
    >>> "sdfhsdhfsdflksdlkfsdflsd"[-4:]
    'flsd'
    

相关文章

网友评论

      本文标题:Python学习笔记(一)

      本文链接:https://www.haomeiwen.com/subject/tzcayltx.html