码迷,mamicode.com
首页 > 编程语言 > 详细

进击的Python【第二章】:Python基础(二)

时间:2016-07-31 20:32:25      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:

Python基础(二)                                                             

本章内容

  1. 数据类型
  2. 数据运算
  3. 列表与元组的基本操作
  4. 字典的基本操作
  5. 字符编码与转码
  6. 模块初探
  7. 练习:购物车程序

 一、数据类型

  Python有五个标准的数据类型:

  • Numbers(数字)
  • String(字符串)
  • List(列表)
  • Tuple(元组)
  • Dictionary(字典)

  1. Number(数字)

  number类型用来专门存储数字数据,他们是不可改变的数据类型,这意味着改变数字数据类型会分配一个新的对象

  Python支持四种不同的数字类型:

  • int(有符号整型)
  • long(长整型[也可以代表八进制和十六进制])
  • float(浮点型)
  • complex(复数)

  实例

  一些数值类型的实例:

intlongfloatcomplex
10 51924361L 0.0 3.14j
100 -0x19323L 15.20 45.j
-786 0122L -21.9 9.322e-36j
080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j
-0490 535633629843L -90. -.6545+0J
-0x260 -052318172735L -32.54e100 3e+26J
0x69 -4721885298529L 70.2-E12 4.53e-7j
  • 长整型也可以使用小写"L",但是还是建议您使用大写"L",避免与数字"1"混淆。Python使用"L"来显示长整型。
  • Python还支持复数,复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型。在Python中虚数部分用的是j,是因为Python沿用了工程领域的规范,这块和咱们上学时用的i有所不同。

  2.string(字符串)

  在最新的Python 3版本中,字符串是以Unicode编码的,也就是说,Python的字符串支持多语言,例如:

1 >>> print(I am 成怡强)
2 I am 成怡强

  3.list(列表)

  List(列表) 是 Python 中使用最频繁的数据类型。

  列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(所谓嵌套)。

  列表用[ ]标识。是python最通用的复合数据类型。看这段代码就明白。

  列表中的值得分割也可以用到变量[头下标:尾下标],就可以截取相应的列表,从左到右索引默认0开始的,从右到左索引默认-1开始,下标可以为空表示取到头或尾。

  实例

1 name_list = [tom, lili, tim]
2 num_list = [1, 2, 3, 4]

 

  4.tuple(元组)

  元组是另一个数据类型,类似于List(列表)。

  元组用"()"标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。

  实例

1 tuple = ( abcd, 786 , 2.23, john, 70.2 )

  5.字典

  字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。

  两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。

  字典用"{ }"标识。字典由索引(key)和它对应的值value组成。

  实例

1 person = {"name": "mr.wu", age: 18}
2 3 person = dict({"name": "mr.wu", age: 18})

 

二、数据运算

简单的回答可以使用表达式4 + 5等于9,在这里4和5被称为操作数,+被称为操符。 Python语言支持操作者有以下几种类型。

  • 算术运算符

  • 比较(即关系)运算符

  • 赋值运算符

  • 逻辑运算符

  • 位运算符

  • 成员操作符

  • 标识操作符

  • Python运算符优先级

让我们逐一看看所有的运算符。

1.算术运算符

操作符描述符例子
+ 加法 - 对操作符的两侧增加值 a + b = 30
- 减法 - 减去从左侧操作数右侧操作数 a - b = -10
* 乘法 - 相乘的运算符两侧的值 a * b = 200
/ 除 - 由右侧操作数除以左侧操作数 b / a = 2
% 模 - 由右侧操作数和余返回除以左侧操作数 b % a = 0
** 指数- 执行对操作指数(幂)的计算 a**b = 10 的幂 20
// 地板除 - 操作数的除法,其中结果是将小数点后的位数被除去的商。 9//2 =  4 而 9.0//2.0 = 4.0

示例:

 1 #!/usr/bin/python
 2 
 3 a = 21
 4 b = 10
 5 c = 0
 6 
 7 c = a + b
 8 print "Line 1 - Value of c is ", c
 9 
10 c = a - b
11 print "Line 2 - Value of c is ", c 
12 
13 c = a * b
14 print "Line 3 - Value of c is ", c 
15 
16 c = a / b
17 print "Line 4 - Value of c is ", c 
18 
19 c = a % b
20 print "Line 5 - Value of c is ", c
21 
22 a = 2
23 b = 3
24 c = a**b 
25 print "Line 6 - Value of c is ", c
26 
27 a = 10
28 b = 5
29 c = a//b 
30 print "Line 7 - Value of c is ", c

 

结果:

1 Line 1 - Value of c is 31
2 Line 2 - Value of c is 11
3 Line 3 - Value of c is 210
4 Line 4 - Value of c is 2
5 Line 5 - Value of c is 1
6 Line 6 - Value of c is 8
7 Line 7 - Value of c is 2

 

2.比较(即关系)运算符

运算符描述示例
== 检查两个操作数的值是否相等,如果是,则条件变为真。 (a == b) 不为 true.
!= 检查两个操作数的值是否等相等,如果值不相等,则条件变为真。 (a != b) 为 true.
<> 检查两个操作数的值是否等相等,如果值不相等,则条件变为真。 (a <> b) 结果为true。这类似于!=运算符。
> 检查左操作数的值是否大于右操作数的值,如果是,则条件成立。 (a > b) 为  true.
< 检查左操作数的值是否小于右操作数的值,如果是,则条件成立。 (a < b) 为true.
>= 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立。 (a >= b) 不为 true.
<= 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立。 (a <= b) 为 true.

示例:

 1 #!/usr/bin/python
 2 
 3 a = 21
 4 b = 10
 5 c = 0
 6 
 7 if ( a == b ):
 8    print "Line 1 - a is equal to b"
 9 else:
10    print "Line 1 - a is not equal to b"
11 
12 if ( a != b ):
13    print "Line 2 - a is not equal to b"
14 else:
15    print "Line 2 - a is equal to b"
16 
17 if ( a <> b ):
18    print "Line 3 - a is not equal to b"
19 else:
20    print "Line 3 - a is equal to b"
21 
22 if ( a < b ):
23    print "Line 4 - a is less than b" 
24 else:
25    print "Line 4 - a is not less than b"
26 
27 if ( a > b ):
28    print "Line 5 - a is greater than b"
29 else:
30    print "Line 5 - a is not greater than b"
31 
32 a = 5;
33 b = 20;
34 if ( a <= b ):
35    print "Line 6 - a is either less than or equal to  b"
36 else:
37    print "Line 6 - a is neither less than nor equal to  b"
38 
39 if ( b >= a ):
40    print "Line 7 - b is either greater than  or equal to b"
41 else:
42    print "Line 7 - b is neither greater than  nor equal to b"

 

结果:

1 Line 1 - a is not equal to b
2 Line 2 - a is not equal to b
3 Line 3 - a is not equal to b
4 Line 4 - a is not less than b
5 Line 5 - a is greater than b
6 Line 6 - a is either less than or equal to b
7 Line 7 - b is either greater than or equal to b

 

3、赋值运算符

运算符描述示例
= 简单的赋值运算符,赋值从右侧操作数左侧操作数 c = a + b 类似于 a + b 到 c
+= 添加和赋值操作符,它增加了右操作数左操作数和结果赋给左操作数 c += a 类似于 c = c + a
-= 减和赋值操作符,它减去右边的操作数从左边操作数,并将结果赋给左操作数 c -= a 类似于 c = c - a
*= 乘法和赋值操作符,它乘以右边的操作数与左操作数,并将结果赋给左操作数 c *= a 类似于 c = c * a
/= 除和赋值操作符,它把左操作数与正确的操作数,并将结果赋给左操作数 c /= a 类似于 c = c / a
%= 模量和赋值操作符,它需要使用两个操作数模和结果赋给左操作数 c %= a 类似于 c = c % a
**= 指数和赋值运算符,执行指数(幂)计算操作符和赋值给左操作数 c **= a 类似于 c = c ** a
//= 地板除,并分配一个值,执行地板划分对操作和指定值到左操作数 c //= a 类似于 c = c // a

示例:

 1 #!/usr/bin/python
 2 
 3 a = 21
 4 b = 10
 5 c = 0
 6 
 7 c = a + b
 8 print "Line 1 - Value of c is ", c
 9 
10 c += a
11 print "Line 2 - Value of c is ", c 
12 
13 c *= a
14 print "Line 3 - Value of c is ", c 
15 
16 c /= a 
17 print "Line 4 - Value of c is ", c 
18 
19 c  = 2
20 c %= a
21 print "Line 5 - Value of c is ", c
22 
23 c **= a
24 print "Line 6 - Value of c is ", c
25 
26 c //= a
27 print "Line 7 - Value of c is ", c

 

结果:

1 Line 1 - Value of c is 31
2 Line 2 - Value of c is 52
3 Line 3 - Value of c is 1092
4 Line 4 - Value of c is 52
5 Line 5 - Value of c is 2
6 Line 6 - Value of c is 2097152
7 Line 7 - Value of c is 99864

 

4.逻辑运算符

and 所谓逻辑与运算符。如果两个操作数都为真,则条件为真。 (a and b) 为 true.
or 所谓逻辑OR运算符。如果有两个操作数都为非零,则条件变为真。 (a or b) 为 true.
not 所谓逻辑非运算符。用反转操作数的逻辑状态。如果条件为true,则逻辑非运算符将为false。 not(a and b) 为 false.

示例:

 1 #!/usr/bin/python
 2 
 3 a = 10
 4 b = 20
 5 c = 0
 6 
 7 if ( a and b ):
 8    print "Line 1 - a and b are true"
 9 else:
10    print "Line 1 - Either a is not true or b is not true"
11 
12 if ( a or b ):
13    print "Line 2 - Either a is true or b is true or both are true"
14 else:
15    print "Line 2 - Neither a is true nor b is true"
16 
17 
18 a = 0
19 if ( a and b ):
20    print "Line 3 - a and b are true"
21 else:
22    print "Line 3 - Either a is not true or b is not true"
23 
24 if ( a or b ):
25    print "Line 4 - Either a is true or b is true or both are true"
26 else:
27    print "Line 4 - Neither a is true nor b is true"
28 
29 if not( a and b ):
30    print "Line 5 - Either a is not true or b is not true"
31 else:
32    print "Line 5 - a and b are true"

 

结果:

1 Line 1 - a and b are true
2 Line 2 - Either a is true or b is true or both are true
3 Line 3 - Either a is not true or b is not true
4 Line 4 - Either a is true or b is true or both are true
5 Line 5 - Either a is not true or b is not true

 

5.位运算符

运算符描述示例
& 二进制AND操作复制一位到一个结果数,如果存在两个操作数。 (a & b) = 12 即  0000 1100
| 二进制或复制操作了一个比特,如果它存在一个操作数中。 (a | b) = 61  即 0011 1101
^ 二进制异或运算符的副本,如果它被设置在一个操作数而不是两个比特。 (a ^ b) = 49  即 0011 0001
~ 二进制的补运算符是一元的,并有“翻转”位的效果。 (~a ) =  -61  即 1100 0011 以2的补码形式由于带符号二进制数。
<< 二进位向左移位运算符。左操作数的值左移由右操作数指定的位数。 a << 2 = 240 即 1111 0000
>> 二进位向右移位运算符。左操作数的值是由右操作数指定的位数向右移动。 a >> 2 = 15 即  0000 1111

示例:

 1 #!/usr/bin/python
 2 
 3 a = 60            # 60 = 0011 1100 
 4 b = 13            # 13 = 0000 1101 
 5 c = 0
 6 
 7 c = a & b;        # 12 = 0000 1100
 8 print "Line 1 - Value of c is ", c
 9 
10 c = a | b;        # 61 = 0011 1101 
11 print "Line 2 - Value of c is ", c
12 
13 c = a ^ b;        # 49 = 0011 0001
14 print "Line 3 - Value of c is ", c
15 
16 c = ~a;           # -61 = 1100 0011
17 print "Line 4 - Value of c is ", c
18 
19 c = a << 2;       # 240 = 1111 0000
20 print "Line 5 - Value of c is ", c
21 
22 c = a >> 2;       # 15 = 0000 1111
23 print "Line 6 - Value of c is ", c

 

结果:

1 Line 1 - Value of c is 12
2 Line 2 - Value of c is 61
3 Line 3 - Value of c is 49
4 Line 4 - Value of c is -61
5 Line 5 - Value of c is 240
6 Line 6 - Value of c is 15

 

6.成员运算符

运算符描述实例
in 如果在指定的序列中找到值返回 True,否则返回 False。 x 在 y 序列中 , 如果 x 在 y 序列中返回 True。
not in 如果在指定的序列中没有找到值返回 True,否则返回 False。 x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。

示例:

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 
 4 a = 10
 5 b = 20
 6 list = [1, 2, 3, 4, 5 ];
 7 
 8 if ( a in list ):
 9    print "1 - 变量 a 在给定的列表中 list 中"
10 else:
11    print "1 - 变量 a 不在给定的列表中 list 中"
12 
13 if ( b not in list ):
14    print "2 - 变量 b 不在给定的列表中 list 中"
15 else:
16    print "2 - 变量 b 在给定的列表中 list 中"
17 
18 # 修改变量 a 的值
19 a = 2
20 if ( a in list ):
21    print "3 - 变量 a 在给定的列表中 list 中"
22 else:
23    print "3 - 变量 a 不在给定的列表中 list 中"

 

结果:

1 1 - 变量 a 不在给定的列表中 list 中
2 2 - 变量 b 不在给定的列表中 list 中
3 3 - 变量 a 在给定的列表中 list 中

 

 

7.标识操作符

运算符描述示例
is 计算结果为true,如果操作符两侧的变量指向相同的对象,否则为false。 x是y,这里结果是1,如果id(x)的值为id(y)。
is not 计算结果为false,如果两侧的变量操作符指向相同的对象,否则为true。 x不为y,这里结果不是1,当id(x)不等于id(y)。

示例:

 1 #!/usr/bin/python
 2 
 3 a = 20
 4 b = 20
 5 
 6 if ( a is b ):
 7    print "Line 1 - a and b have same identity"
 8 else:
 9    print "Line 1 - a and b do not have same identity"
10 
11 if ( id(a) == id(b) ):
12    print "Line 2 - a and b have same identity"
13 else:
14    print "Line 2 - a and b do not have same identity"
15 
16 b = 30
17 if ( a is b ):
18    print "Line 3 - a and b have same identity"
19 else:
20    print "Line 3 - a and b do not have same identity"
21 
22 if ( a is not b ):
23    print "Line 4 - a and b do not have same identity"
24 else:
25    print "Line 4 - a and b have same identity"

 

结果:

1 Line 1 - a and b have same identity
2 Line 2 - a and b have same identity
3 Line 3 - a and b do not have same identity
4 Line 4 - a and b do not have same identity

 

8.Python运算符优先级

  运算符优先级来确定条件的表达式中的分组。这会影响一个表达式如何计算。某些运算符的优先级高于其他;例如,乘法运算符的优先级比加法运算更高。

  例如x=7 + 3* 2;这里,x被赋值13,而不是20,因为运算符*的优先级比+更高,所以它首先乘以3 * 2,然后加7。

  这里,具有最高优先级运算符出现在表格上方,那些最低的显示在底部。在一个表达式,更高的优先级运算符将首先计算。

运算符描述
** 幂(指数)
~ + - 补码,一元加号和减号(方法名的最后两个+@和 - @)
* / % // 乘,除,取模和地板除
+ - 加法和减法
>> << 左,右按位转移
& 按位“与”
^ | 按位异或`‘和`定期‘或‘
<= < > >= 比较运算符
<> == != 等式运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 运算符标识
in not in 成员运算符
not or and 逻辑运算符

示例:

 1 #!/usr/bin/python
 2 
 3 a = 20
 4 b = 10
 5 c = 15
 6 d = 5
 7 e = 0
 8 
 9 e = (a + b) * c / d       #( 30 * 15 ) / 5
10 print "Value of (a + b) * c / d is ",  e
11 
12 e = ((a + b) * c) / d     # (30 * 15 ) / 5
13 print "Value of ((a + b) * c) / d is ",  e
14 
15 e = (a + b) * (c / d);    # (30) * (15/5)
16 print "Value of (a + b) * (c / d) is ",  e
17 
18 e = a + (b * c) / d;      #  20 + (150/5)
19 print "Value of a + (b * c) / d is ",  e

 

结果:

1 Value of (a + b) * c / d is 90
2 Value of ((a + b) * c) / d is 90
3 Value of (a + b) * (c / d) is 90
4 Value of a + (b * c) / d is 50

 

三、列表与元组的基本操作

列表:

1.基本列表操作

  

Python 表达式结果描述
len([1, 2, 3]) 3 长度
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] 串联
[‘Hi!‘] * 4 [‘Hi!‘, ‘Hi!‘, ‘Hi!‘, ‘Hi!‘] 重复
3 in [1, 2, 3] True 成员
for x in [1, 2, 3]: print x, 1 2 3 迭代

2.切片与索引

1 list1 = [physics, chemistry, 1997, 2000]
2 list2 = [1, 2, 3, 4, 5, 6, 7]
3 
4 print("list1[0]: ", list1[0])   #拿第一个元素
5 print("list2[1:5]: ", list2[1:5])   #顾头不顾尾,切片
6 print("list1[0]:", list1[::2])  #每两个步长取一个元素

 

 

结果:

1 list1[0]:  physics
2 list2[1:5]:  [2, 3, 4, 5]
3 list1[0]: [physics, 1997]

 

3.更新列表

1 #!/usr/bin/python
2 
3 list = [physics, chemistry, 1997, 2000];
4 
5 print "Value available at index 2 : "
6 print list[2];
7 list[2] = 2001;
8 print "New value available at index 2 : "
9 print list[2];

 

结果:

1 Value available at index 2 :
2 1997
3 New value available at index 2 :
4 200

 

4.删除列表中的元素

要删除列表的元素,可以使用del语句,如果知道哪些元素要删除;或如果你不知道那么使用remove()方法。下面是一个简单的例子:

1 #!/usr/bin/python
2 
3 list1 = [physics, chemistry, 1997, 2000];
4 
5 print list1;
6 del list1[2];
7 print "After deleting value at index 2 : "
8 print list1;

 

结果:

1 [physics, chemistry, 1997, 2000]
2 After deleting value at index 2 :
3 [physics, chemistry, 2000]

 

5.内置函数列表及方法

Python中包括下面的列表函数功能:

 技术分享

Python中包括下面的列表的方法:

技术分享

元组

  元组是不可变的Python对象序列。元组的序列就像列表。唯一的区别是,元组不能被改变,即元组是不可被修改。元组使用小括号,而列表使用方括号。

  元组的方法只有两个:

1 list.count(obj)
2 list.index(index, obj)

 

四、字典的基本操作

字典是另一种可变容器模型,且可存储任意类型对象。

字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:

1 dict = {Alice: 2341, Beth: 9102, Cecil: 3258}

也可以用上面的代码来创建一个字典。

键在一个字典中是唯一的,而值可能不是。字典的值可以是任何类型的,但键必须是不可变的数据类型,例如字符串,数字,或元组。

1.访问一个字典的值:

要访问字典元素,您可以使用熟悉的方括号一起的关键,获得它的值。下面是一个简单的例子:

1 #!/usr/bin/python
2 
3 dict = {Name: Zara, Age: 7, Class: First};
4 
5 print "dict[‘Name‘]: ", dict[Name];
6 print "dict[‘Age‘]: ", dict[Age];

 

结果:

1 dict[Name]:  Zara
2 dict[Age]:  7

 

2.更新字典:

可以通过添加一个新条目或项目(即一个键 - 值对),修改现有条目或删除。作为简单的例子,如下图所示在现有条目更新字词:

 1 #!/usr/bin/python
 2 
 3 dict = {Name: Zara, Age: 7, Class: First};
 4 
 5 dict[Age] = 8; # update existing entry
 6 dict[School] = "DPS School"; # Add new entry
 7 
 8 
 9 print "dict[‘Age‘]: ", dict[Age];
10 print "dict[‘School‘]: ", dict[School];

 

结果:

1 dict[Age]:  8
2 dict[School]:  DPS School

 

3.删除字典元素:

可以删除单个字典元素或清除字典中的全部内容。也可以删除整个字典在一个单一的操作。

要删除整个字典,只要用del语句。下面是一个简单的例子:

 1 #!/usr/bin/python
 2 
 3 dict = {Name: Zara, Age: 7, Class: First};
 4 
 5 del dict[Name]; # remove entry with key ‘Name‘
 6 dict.clear();     # remove all entries in dict
 7 del dict ;        # delete entire dictionary
 8 
 9 print "dict[‘Age‘]: ", dict[Age];
10 print "dict[‘School‘]: ", dict[School];

这将产生以下结果。注意引发异常,这是因为经过del dict删除,字典已经不存在了:

1 dict[Age]:
2 Traceback (most recent call last):
3   File "test.py", line 8, in <module>
4     print "dict[‘Age‘]: ", dict[Age];
5 TypeError: type object is unsubscriptable

 

4.字典的键的属性

字典值没有限制。它们可以是任意Python对象,无论是标准的对象或用户定义的对象。但是作为键,是不可以这样的。

要记住字典中的键的两个要点:

(一)不准一个键对应多个条目。这意味着不能有重复的键。当有重复的键,在分配过程中以最后分配的为准。下面是一个简单的例子:

1 #!/usr/bin/python
2 
3 dict = {Name: Zara, Age: 7, Name: Manni};
4 
5 print "dict[‘Name‘]: ", dict[Name];

当执行上面的代码,产生以下结果:

1 dict[Name]:  Manni

 

(二)键的值字必须是不可变的。这意味着可以使用字符串,数字或元组作为字典的键,但像[‘key‘]是不允许的。下面是一个简单的例子:

1 #!/usr/bin/python
2 
3 dict = {[Name]: Zara, Age: 7};
4 
5 print "dict[‘Name‘]: ", dict[Name];

 

结果:

1 Traceback (most recent call last):
2   File "test.py", line 3, in <module>
3     dict = {[Name]: Zara, Age: 7};
4 TypeError: list objects are unhashable

5.内置字典功能和方法

Python中包括以下字典功能:

技术分享 

Python中包括以下字典方法:

技术分享

 

五、字符编码与转码

Python2.X中默认的字符编码为Unicode

Python3.X中默认字符编码为Utf-8

下面举例说明:

 

Python3.X

msg = "我爱北京天安门"
#msg_gb2312 = msg.decode("utf-8").encode("gb2312")
msg_gb2312 = msg.encode("gb2312") #默认就是unicode,不用再decode,喜大普奔
gb2312_to_unicode = msg_gb2312.decode("gb2312")
gb2312_to_utf8 = msg_gb2312.decode("gb2312").encode("utf-8")

print(msg)
print(msg_gb2312)
print(gb2312_to_unicode)
print(gb2312_to_utf8)

结果:

utf-8
我爱北京天安门
b‘\xce\xd2\xb0\xae\xb1\xb1\xbe\xa9\xcc\xec\xb0\xb2\xc3\xc5‘
我爱北京天安门
b‘\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8‘

六、模块初探

一个模块可以在逻辑上组织Python代码。将相关的代码到一个模块中,使代码更容易理解和使用。模块是可以绑定和借鉴任意命名属性的Python对象。

简单地说,一个模块是由Python代码的文件。一个模块可以定义函数,类和变量。模块还可以包括可运行的代码。

sys模块

导入sys模块,调用argv方法

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
 
print(sys.argv)

输出结果:

E:\learn_python\day02>D:\python3.5\python.exe blog.py 1 2 3
[‘blog.py‘, ‘1‘, ‘2‘, ‘3‘]

os模块

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import os
 
os.system("df -h") #调用系统命令

 

进击的Python【第二章】:Python基础(二)

标签:

原文地址:http://www.cnblogs.com/yunweiqiang/p/5723086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!