1. Python的解释器常见的有哪些?

2. Python的option有哪些(python -h)?

3. Python文件开头加上shebang/hashbang/sha-bang/she-bang/sh-bang

  1. #!/usr/bin/python
  2. #!/usr/bin/env python

4. 学习提供的learning.py文件。

learning.py

5. print()函数

  1. print()会依次打印它的参数,遇到一个“,”会自动的打印一个空格

6. Python大小写敏感

7. Python中不需要指定变量的类型。所谓python的数据类型指的是变量所指的内存中_对象_的类型

  1. number

    • int
    • float
    • bool
    • complex
  2. string

    • 对于string来说双引号(")和单引号(')没有区别。只不过单引号中的双引号不需要转义,反之亦然。
    • raw字符串用r''表示,内部的字符串不用转义。
    • 用('''...''')的格式可以表示多行的内容而不同显式的插入换行符\n
    • python3中字符串是用unicode编码的
    • ord()函数获取字符的整数表示,chr()把编码转化成对应的字符:ord('o'), chr(23566)
    • bytes类型的数据用带b前缀来表示:x = b'ABC'type(x) => <class 'bytes'>, len(x)=3
    • str和bytes类型之间的转化:str.encode('charset')=>bytes; bytes.decode('charset')=>str
    • len()函数在计算strbytes时的方法:计算str的字符数,计算bytes的字节数。
    • 输出时的格式化:使用%, %d, %f, %s, %x。'%05d-%s' %(3, "hello")
  3. list
    • 空的list:a = []; b = list()
    • 索引位置:左边从0开始;右边从-1开始
    • 常用操作:append(), insert(idx, elem); pop(idx)
  4. tuple
    • 空的tuple:a = (); a = tuple()
    • 含有一个元素的tuple:a = (10,); a = tuple([10])
    • tuple不可变,但是包含的元素可以变:a = ('a', 'b', ['A', 'B'])
  5. set
    • 空的set:a = set()
    • 创建一个set,需要提供一个list作为输入集合:a = set([10, 14])
    • set不能有重复的元素,所以可以用来去重:a = [1, 2, 3, 2, 4, 6, 2]; b = set(a); a = list(b)
    • set中的元素是无序的
    • set常用的操作:add(key), remove(key), s1 & s2, s1 | s2, s1 - s2
  6. dict
    • 空的dict:a = {}; a = dict()
    • 当dict中存在key:value对时,后面如果加入相同的key,那么会更新已存在的value
    • 使用d[key]去获得value时,如果key不存在那么将会报错。
      • 使用in语法判断key是不是存在
      • 使用d.get(key)方法,该方法还可以指定default值d.get(key, defvalue)
  7. list v.s. dict
速度 内存
list 查找和插入的时间随着元素的增加而增加 占用空间小,内存浪费少
dict 查找和插入的速度极快,不会随着key的增加而变慢 占用大量的内存,内存浪费多

8. Python中实际上并没有常量或是说没有机制可以保证所谓常量的值不变。例如PI=3.14159265359是一个常量,但是依然可以给PI重新赋值。

9. Python中的两种除法

  1. / 10 / 3 = 3.3333333333333335
  2. // 10 // 3 = 3

10. 条件判断:if...elif...else

11. 循环语句:for... in, while, for...in...else, while...else

- for...in...else和while...else中的else语句会在for/while正常执行完毕之后调用。如果循环由于break语句而跳出,那么else语句将不会执行。

12. 函数定义,参数以及递归

  1. 定义
    • def关键字
    • 没有return语句,函数返回的是None
    • return None,可以简写为return
    • 空函数的函数体使用pass作为占位符。pass还可以用到其他的语句中
    • 函数可以返回多个值,相当于返回的时一个tuple
  2. 参数
    • 位置参数
    • 默认参数
      • 必选参数在前,默认参数在后
      • 变化大的参数放在前面,变化小的参数放到后面,可以充分的利用默认参数的便利性
      • 默认参数可以使用参数名字来指定将value传给哪一个参数
      • 默认参数和可变对象&不可变对象。此处有坑。
    • 可变参数
      • 使用list/tuple实现,这样在调用的时候需要传入list/tuple
      • 使用*实现,这样在调用的时候就可以一个一个的传入参数。如果传入的是list/tuple那么需要使用*来解包,但是内部还是将所有的参数自动组装成为一个tuple
    • 关键字参数
      • 使用**实现,这样在调用的时候就可以传入任意多的含参数名的参数。在内部这些参数名和参数组成dict
    • 命名关键字参数
      • 命名关键字参数需要一个特殊分割符**之后的参数被命名为关键字参数:def person(name, age, *, city, job)
      • 如果已经定义了可变参数,后面的命名关键字参数就不需要再加*了:def person(name, age, *arg, city, job)
      • 命名关键字参数必须传入参数名
      • 命名关键字参数可以有default值
    • 参数组合
      • 必选参数,默认参数,可变参数,命名关键字参数和关键字参数可以按照以上顺序组合使用
  3. 递归
    • 有栈溢出的风险,可以通过尾递归来优化
    • Python的标准解释器并没有针对尾递归进行优化

In [52]:
##测试几种变量的类型
a, b, c, d, e = 1, 2.3, True, 4+5j, 10000000000000000000000000000000000000000000000
print(type(a), type(b), type(c), type(d), type(e))
f = 'Hello, Python!'
g = True
print(type(f), type(g))
h, i, j = [], set(), {}
print(type(h), type(i), type(j))
#所有的这些都是类(class)


<class 'int'> <class 'float'> <class 'bool'> <class 'complex'> <class 'int'>
<class 'str'> <class 'bool'>
<class 'list'> <class 'set'> <class 'dict'>

In [53]:
10/3, 10//3, 10 % 3


Out[53]:
(3.3333333333333335, 3, 1)

In [54]:
ord('o'), chr(23566)


Out[54]:
(111, '導')

In [55]:
x = b'ABC'
print(type(x))
print(len(x))


<class 'bytes'>
3

In [56]:
print('中文'.encode('utf-8'))
len('中文'.encode('utf-8'))


b'\xe4\xb8\xad\xe6\x96\x87'
Out[56]:
6

In [57]:
'%05d-%s' %(3, "hello")


Out[57]:
'00003-hello'

In [34]:
a = list()
a = tuple('a')
a = tuple(10)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-a13887db7fc7> in <module>()
      1 a = list()
      2 a = tuple('a')
----> 3 a = tuple(10)

TypeError: 'int' object is not iterable

In [37]:
a = set([10])
print(type(a))
print(a)


<class 'set'>
{10}

In [48]:
a = [1, 2, 3, 2, 4, 6, 2]
b = set(a)
a = list(b)
print(a)
print(b)
c = set([2, 3, 4, 5])
print(c)
print(b - c)
print(b | c)


[1, 2, 3, 4, 6]
{1, 2, 3, 4, 6}
{2, 3, 4, 5}
{1, 6}
{1, 2, 3, 4, 5, 6}

In [60]:
import math
def move(x, y, step, angle=0):
    nx = x + step * math.cos(angle)
    ny = y - step * math.sin(angle)
    return nx, ny
x, y = move(100, 100, 60, math.pi/6)
print('x = %f, y = %f' %(x, y))


x = 151.961524, y = 70.000000

In [3]:
def add_end(L = []):
    L.append("END")
    return L

print(add_end())
print(add_end())

#这里L要使用不可变对象
def add_end(L = None):
    if L is None:
        L = []
    L.append('END')
    return L
print(add_end())
print(add_end())


['END']
['END', 'END']
['END']
['END']

In [7]:
def calc(numbers):
    sum = 0
    for num in numbers:
        sum = sum + num
    return sum

print(calc([1, 2, 4, 5, 6]))

def calc(*numbers):
    sum = 0
    print(type(numbers))
    for num in numbers:
        sum = sum + num
    return sum

print(calc(1, 2, 3, 4, 5))
print(calc(*[1, 2, 3, 4, 5]))
print(calc([1, 2, 3, 4, 5]))


18
<class 'tuple'>
15
<class 'tuple'>
15
<class 'tuple'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-618179ba48e1> in <module>()
     16 print(calc(1, 2, 3, 4, 5))
     17 print(calc(*[1, 2, 3, 4, 5]))
---> 18 print(calc([1, 2, 3, 4, 5]))

<ipython-input-7-618179ba48e1> in calc(*numbers)
     11     print(type(numbers))
     12     for num in numbers:
---> 13         sum = sum + num
     14     return sum
     15 

TypeError: unsupported operand type(s) for +: 'int' and 'list'

In [62]:
def person(name, age, **kw):
    print('name: ', name, "age: ", age, "others: ", kw)

person('Longshan', 30, city='Beijing')


name:  Longshan age:  30 others:  {'city': 'Beijing'}

In [ ]: