python笔记

基础

  • 采用缩进(不用“;”隔开)

  • 空值:None

  • 注释
    # print absolute value of an integer:

  • Python的整数没有大小限制,浮点数也没有,但超出一定范围就直接表示为inf(无限大)

  • 动态语言:同一个变量可以反复赋值,而且可以是不同类型的变量

    1
    2
    3
    4
    a = 123 # a是整数
    print(a)
    a = 'ABC' # a变为字符串
    print(a)
  • bytes类型和str类型转化

1
2
3
4
5
6
7
8
9
10
11
>>> 'ABC'.encode('ascii')
b'ABC'
>>> '中文'.encode('utf-8')
b'\xe4\xb8\xad\xe6\x96\x87'
>>> b'ABC'.decode('ascii')
'ABC'
>>> b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')
'中文'
#如果只有一小部分无效的字节,可以传入errors='ignore'忽略错误的字节
>>> b'\xe4\xb8\xad\xff'.decode('utf-8', errors='ignore')
'中'
  • 布尔值
    1
    2
    3
    4
    >>> True
    True
    >>> False
    False
  • 与、或、非
    1
    2
    3
    4
    5
    6
    >>> True and True
    True
    >>> True or False
    True
    >>> not True
    False
  • 除法

无论整数、浮点数,/除法计算结果是浮点数

1
2
>>> 9 / 3
3.0

整数,//地板除计算结果是整数(只取结果的整数部分)
浮点数,//地板除计算结果是浮点数(只取结果的整数部分)

1
2
3
4
>>> 10 // 3
3
>>> 20.8 // 2
10.0

条件判断和循环

  • if语句
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    age = 3
    if age >= 18:
    print('your age is', age)
    print('adult')
    elif age >= 6:
    print('your age is', age)
    print('teenager')
    else:
    print('your age is', age)
    print('kid')

tuple

  • 一种有序列表:元组
  • tuple的每个元素,指向永远不变,但指向的list本身可变
  • 创建list
    1
    2
    3
    >>> classmates = ('Michael', 'Bob', 'Tracy')
    >>> classmates
    ('Michael', 'Bob', 'Tracy')
  • 获取元素的方法和list一样
  • 没有改变元素方法

list

  • 是内置的一种数据类型:列表
  • list里面的元素的数据类型可以不同

>>> L = ['Apple', 123, True, ['asp', 'php']]

  • 创建list
    1
    2
    3
    >>> classmates = ['Michael', 'Bob', 'Tracy']
    >>> classmates
    ['Michael', 'Bob', 'Tracy']
  • 获得list元素的个数
    1
    2
    >>> len(classmates)
    3
  • 用索引来访问list中元素
    1
    2
    3
    4
    >>> classmates[0]
    'Michael'
    >>> classmates[-1]
    'Tracy'
  • 添加、删除、替换元素
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #追加元素到末尾
    >>> classmates.append('Adam')
    >>> classmates
    ['Michael', 'Bob', 'Tracy', 'Adam']
    #把元素插入到指定的位置
    >>> classmates.insert(1, 'Jack')
    >>> classmates
    ['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
    #删除list末尾的元素
    >>> classmates.pop()
    'Adam'
    >>> classmates
    ['Michael', 'Jack', 'Bob', 'Tracy']
    #删除指定位置的元素
    >>> classmates.pop(1)
    'Jack'
    >>> classmates
    ['Michael', 'Bob', 'Tracy']
    #把某个元素替换成别的元素
    >>> classmates[1] = 'Sarah'
    >>> classmates
    ['Michael', 'Sarah', 'Tracy']

字符串

  • 字符串的输出

依次打印每个字符串,遇到逗号“,”会输出一个空格

1
2
>>> print('The quick brown fox', 'jumps over', 'the lazy dog')
The quick brown fox jumps over the lazy dog

输出格式化字符串:
%方法

1
2
3
4
>>> 'Hello, %s' % 'world'
'Hello, world'
>>> 'Hi, %s, you have $%d.' % ('Michael', 1000000)
'Hi, Michael, you have $1000000.'

format()方法

1
2
>>> 'Hello, {0}, 成绩提升了 {1:.1f}%'.format('小明', 17.125)
'Hello, 小明, 成绩提升了 17.1%'
  • 多行字符串

交互式命令行:

1
2
3
>>> print('''line1
... line2
... line3''')

.py文件:

1
2
3
print('''line1
line2
line3''')
  • 字符串不转义
    用r’’表示’’内部的字符串默认不转义
    1
    2
    3
    4
    >>> print('\\\t\\')
    \ \
    >>> print(r'\\\t\\')
    \\\t\\
  • 字符编码转换
    1
    2
    3
    4
    5
    6
    >>> ord('A')
    65
    >>> chr(25991)
    '文'
    >>> '\u4e2d\u6587'
    '中文'
  • 字符串的长度

len计算str类型字符数

1
2
3
4
>>> len('ABC')
3
>>> len('中文')
2

len也可计算bytes类型字节数

1
2
3
4
5
6
>>> len(b'ABC')
3
>>> len(b'\xe4\xb8\xad\xe6\x96\x87')
6
>>> len('中文'.encode('utf-8'))
6

输入输出

  • 输出
1
>>> print('hello, world')
  • 输入
1
2
3
4
>>> name = input()
Michael
>>> name
'Michael'
  • 文件示例
1
2
name = input('please enter your name: ')
print('hello,', name)
1
2
3
C:\Workspace> python hello.py
please enter your name: Michael
hello, Michael

文件运行

  • 命令行模式和交互模式

    • 命令行模式:

      1
      C:\>
    • 交互模式:
      命令行模式下输入python,进入交互模式
      输入exit(),退出交互模式

      1
      2
      3
      4
      5
      6
      C:\Users\cjq2061>python
      Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
      Type "help", "copyright", "credits" or "license" for more information.
      >>> exit()

      C:\>
  • 运行.py文件

    1
    C:\work>python xxx.py
作者

Hyeee

发布于

2019-04-20

更新于

2019-04-20

许可协议