美文网首页
2018-11-19

2018-11-19

作者: Karl_2c80 | 来源:发表于2018-11-20 16:27 被阅读0次

0) 安装ipython

IPython 7.0+ supports Python 3.5 and above.
When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
Python 3.3 and 3.4 were supported up to IPython 6.x.

  • 根据本地python版本安装相应版本的ipython
pip install ipython=888   #指定不存在的版本以查看可用版本
pip install ipython=5.8.0

1) python文件类型

1.1) 源码(.py):vim 1.py

#!/usr/bin/python
print 'hello world'

python 1.py
./1.py

1.2) 字节代码(.pyc):vim 2.py

#!/usr/bin/pytthon
import py_compile
py_compile.compile('1.py')

python 2.py
python 1.pyc

1.3) 优化代码(.pyo)

python -O -m py_compile 1.py
python 1.pyo

2) python变量

  • python下变量是对一个数据的引用
  • python中给变量赋值,即变量指向内存中的另一块区域
  • 变量不能使用关键字
  • 变量不能以数字开头

2.2) 定义赋值

a = 123
id(a)
a = 456
id(a)

2.3) 运算符

2.3.1) 赋值运算符

x = 2
str = 'abc'
type(x)
type(str)

x += 2
x

x -= 1
x

x *= 8
x

x /= 5
x

x %= 3
x

2.3.2) 算数运算符

3 + 4
'a' + 'b'
3 - 4
3 * 4
4 / 3
4.0 / 3
4.0 // 3  #取整
4 % 3
2**3

2.3.3) 关系运算符

1 > 2
1 <= 2
1 == 2
1 != 2

2.3.4) 逻辑运算符

1 == 2 and 2 > 1
1 == 2 or 2 > 1
not 1 == 2

2.4.5) 优先级

  • 越往下优先级越高
  • 一行中,越往右优先级越高
    逻辑:or
    逻辑:and
    逻辑:not
    成员测试:in, not in
    同一性测试:is, is not
    比较:<, <=, >, >=, !=, ==
    按位或:|
    按位异或:^
    按位与:&
    移位:<<, >>
    加减法:+, -
    乘除法取余:, /, %
    正负号:+x, -x
    按位翻转:~x
    指数:
    *

2.4.6) input与raw_input

help(input)
input("Please input: ")   #分别输入数字和字母测试下,默认数值
raw_input("Please input: ")   #分别输入数字和字母测试下,默认字符串
  • vim 3.py
#!/usr/bin/python

num1 = input("Please input number1: ")
num2 = input("Please input number2: ")

print "%s + %s = %s" % (num1, num2, num1+num2)   #格式化字符串 %s
print "%s - %s = %s" % (num1, num2, num1+num2)
print "%s * %s = %s" % (num1, num2, num1+num2)
print "%s / %s = %s" % (num1, num2, num1+num2)

3) python数据类型

  • 数值
  • 字符串
  • 元组
  • 列表
  • 字典

3.1) 数值类型

  • 整型
  • 长整型
  • 浮点型
  • 复数型
a = 456
type(a)   #int范围:-2**31 --- 2**31
b = 100l   #long长整型:l或L,大小写都行
type(b)

type(3e+7)   #float浮点型:科学计数法也是浮点数

c = 23j
type(c)   #complex复数型

3.2) 字符串类型:string

  • str = 'this is a string'
  • str = "this is a string"
  • str = '''this is a string'''

python中,单引号和双引号没区别,三重引号(docstring)既能定义字符串也能用作注释

a = "hello\nworld"
print a

a= ''' hello
world'''
print a

#可通过索引和切片来操作字符串(序列)
a = 'abcde'

a[0]
a[-1]   #最后一个字符

a[0]+a[1]   #取前两个字符,通过使用a[:2]
a[0:2]
a[:2]
a[1:]
a[:]
a[:-1]
a[::2]   #步长step
a[-4:-2]
a[-2:-4:-1]   #-1表示从右到左

3.3) 序列的基本操作

  1. +: 连接2个序列
  2. *: 重复序列元素
  3. in: 判断元素是否在序列中
  4. len(): 求序列长度
  5. max(): 返回最大值
  6. min(): 返回最小值
  7. cmp(x, y): 比较2个序列

string字符串、tuple元组、list列表,都是序列

In [1]: a = 'abcde'

In [2]: max(a)
Out[2]: 'e'

In [3]: min(a)
Out[3]: 'a'

In [4]: len(a)
Out[4]: 5

In [5]: b = [1, '2', (3), ("4",), ["a", 6, 'b', '7']] 

In [6]: max(b)
Out[6]: ('4',)

In [7]: min(b)
Out[7]: 1

In [8]: len(b)
Out[8]: 5

In [9]: cmp(a, b)
Out[9]: 1

In [10]: cmp(a, 'bcde')
Out[10]: -1

In [11]: cmp(a, 'ABCDE')
Out[11]: 1

3.4) 元组类型:tuple

元组和字符串是不可变的序列

In [18]: a
Out[18]: 'abcde'

In [19]: t = (a, 'b', 'c')

In [20]: t
Out[20]: ('abcde', 'b', 'c')

In [21]: first, second, third = t

In [22]: first
Out[22]: 'abcde'

In [23]: second
Out[23]: 'b'

In [24]: third
Out[24]: 'c'

In [25]: t.count("b")
Out[25]: 1

In [26]: t.count('c')
Out[26]: 1

In [27]: t.count('a')
Out[27]: 0

In [28]: t.count(a)
Out[28]: 1

In [29]: t.count('ab')
Out[29]: 0

3.5) 列表类型:list

  • 取值:切片和索引
  • 添加:list.append()
  • 删除:list.remove(list[]) 或 del list[index]
  • 修改:list[] = x
  • 查找:var in list
In [35]: list1 = ['b', (1,), 3]

In [36]: list1.append([])

In [37]: list1
Out[37]: ['b', (1,), 3, []]

In [38]: list1[3].append('abc123')

In [39]: list1
Out[39]: ['b', (1,), 3, ['abc123']]

In [41]: range(5)
Out[41]: [0, 1, 2, 3, 4]

In [42]: list1.extend(range(5))

In [43]: list1
Out[43]: ['b', (1,), 3, ['abc123'], 0, 1, 2, 3, 4]

In [44]: a
Out[44]: 'abcde'

In [45]: list1.extend(a)

In [46]: list1
Out[46]: ['b', (1,), 3, ['abc123'], 0, 1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e']

3.6) 字典类型:dict

turn to 2018-11-21

相关文章

网友评论

      本文标题:2018-11-19

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