python核心编程一书笔记之第一篇

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#env 是一个命令用来寻找系统中的python解释器。第二条解释使用utf-8编码

在类unix系统中允许python为了防止出现找不到命令经常会加上一段申明,在python3以下的版本需要加上编码申明来实行兼容:

在python中的第一个案例:

1 print 'Hello World!'
2 #你好这个世界 嘿嘿!
View Code

print 这个命令用于输出,常用来查看变量里面的内容,或者输出一些内容显示给用户。

变量是会变的量所以叫做变量。变量可以帮助程序员记忆一些东西,所以一些变量名都是代表着一些内容。变量的起名规则:只能由数字,字母,下划线组成,其中数字不能开头,变量名最好可以给你一种见名起义的感觉,变量名不要起太长不方便记忆。

在python中_下划线可以起到重复上一条命令的作用例:

1 >>> 1 == 1
2 True
3 >>> _
4 True
5 >>> 
View Code

在python中三种常用的字符串格式操作符%s,%d,%f   %s代表字符串,%d代表整数,%f代表浮点型。例:

1 name1 = 'alex'
2 name2 = 'eric'
3 name3 = 'seven'
4 print 'name1:%s,name2:%s,name3%s' %('name1','name2','name3')#可加引号,也可不加,python3中必须加。

用户的输入可以用raw_input('')等待用户输入内容,如果等待用户输入的是密码可以这么做例:

1 import getpass
2 name = raw_input('You name:')
3 pwd = getpass.getpass('You pwd:')

如何将用户输入的字符串转换成整形例:

name1 = int(raw_input('Guess the number:'))

查询帮助信息help(内建函数)即可查帮助信息。

注释:帮助理解用的,有申明注释,代码注释例:

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-    #申明注释
3 # 可做当行注释
4 '''
5 '''   #多行注释,可用三个单引号或者三个双引号不可混用。

算术操作符+ - * / // % **

前四种在python中与正常算术运算符一样,可正常加减,但不可字符串+,-整形, *对字符串使用的话可以成倍出现,除法中如果不带小数点会只留整数部分,//取整数部分,%取余,**用来幂运算

还有一些= , ==  ,  !=  <=, >=  第一个辅助运算,将右面的值赋给左边的值,这种运算都是先算右边再算左边。  ==比较    !=不等于.后面两个是大于等于喝小于等于。

还有一些特殊的布尔运算返回值为True  和False,和逻辑运算符返回值not, and, or   not表示条件相反,and表示并且,or表示或。

字符串: 是  表示""引号和字符之类内容的集合,两个引号必须相同不可左边一个单引号,右边一个双引号,两个字符串直接如果用+就表示两个字符串内容和成一个字符串可以叫做连接运算,如果用*表示重复字符串,在字符串中还有一些索引的用法注:索引从0开始的[:3]的话只会输出3这个代表位前面的元素例:

 1 >>> pystr = 'python'
 2 >>> iscool = 'is cool'
 3 >>> pystr[0]
 4 'p'
 5 >>> pystr[2:5]
 6 'tho'
 7 >>> iscool[:2]
 8 'is'
 9 >>> iscool[3:]
10 'cool'
11 >>> iscool[-1]
12 'l'
13 >>> pystr + iscool
14 'pythonis cool'
15 >>> pystr + ' ' + iscool
16 'python is cool'
17 >>> pystr * 2
18 'pythonpython'
19 >>> '-'*20
20 '--------------------'
21 >>> pystr = '''python
22    ... is cool '''
23 >>> pystr
24 'python\n   ... is cool '
25 >>> print pystr
26 python
27    ... is cool 
28 >>> iscool[-3]
29 'o'
30 >>> iscool[-3:]
31 'ool'
View Code

列表和元祖:可以保存python中任意类型任意数量的对象。列表用[]包裹里面的元素个数和元素值可以改变。元祖用()包裹里面的元素个数和元素值不可改变,可以当成可读,但元祖本身可以改变。列表和元祖都可以用切片来操作得到子集([][:])和字符串操作方法一样。不过它得到的子集不是单个的字符,而可能是一组字符,下面是一些列表和元祖的操作例:

 

 1 >>> alist = [1,'s',3,4]
 2 >>> alist
 3 [1, 's', 3, 4]
 4 >>> alist[0]
 5 1
 6 >>> alist[2:]
 7 [3, 4]
 8 >>> alist[:3]
 9 [1, 's', 3]
10 >>> alist[1]=2
11 >>> alist
12 [1, 2, 3, 4]
13 >>> #元祖的切片操作
14 >>> atuple = ('robots',77,93,'try')
15 >>> atuple
16 ('robots', 77, 93, 'try')
17 >>> atuple[:3]
18 ('robots', 77, 93)
19 >>> atuple[1] = 5
20 
21 Traceback (most recent call last):
22   File "<pyshell#11>", line 1, in <module>
23     atuple[1] = 5
24 TypeError: 'tuple' object does not support item assignment
25 >>> 
View Code

 

字典:由键值对组成(key,value)字典用{}包裹里面的元素可以修改操作例:

 1 >>> adict = {'host':'earth'}
 2 >>> adict['port']=80
 3 >>> adict
 4 {'host': 'earth', 'port': 80}
 5 >>> adict.keys()
 6 ['host', 'port']
 7 >>> adict['host']
 8 'earth'
 9 >>> adict['earth']
10 
11 Traceback (most recent call last):
12   File "<pyshell#5>", line 1, in <module>
13     adict['earth']
14 KeyError: 'earth'
15 
16 >>> for key in adict:
17     print key,adict[key]
18     
19 host earth
20 port 80
21 >>> 
View Code

代码块的缩进和对齐:一级一级的对应,同级的缩进应该保持一致。

if语句:在if语句中判断条件是否符合,符合就往下执行,否则不执行例:


 1 if True:
 2     print 'hello'
 3 if 1 == 0:
 4     print 'no'
 5 
 6 hello
 7 
 8 if 1 == 0:
 9     print 'no'
10 else:
11     print 'yes'
12 
13 
14 yes
View Code

 

 

while循环:当条件满足时,会一直执行代码,直到条件不满足跳出。注意要保持缩进一致例

1 counter = 0
2 while counter < 3:
3     print 'loop #%d' %(counter)
4     counter += 1
5 
6 loop #0
7 loop #1
8 loop #2
View Code

for循环:需要连接一个变量往下执行例:

 

 1 #-*- coding:utf-8 -*-
 2 print 'I like to use the Internet for:'
 3 for item in ['e-mail','net-surfing','homework','chat']:
 4     print item
 5 
 6 I like to use the Internet for:
 7 e-mail
 8 net-surfing
 9 homework
10 chat
11 
12 #如果想让上面的例子执行在同一行只需要在print语句最后添加一个逗号
13 
14 
15 print 'I like to use the Internet for:'
16 for item in ['e-mail','net-surfing','homework','chat']:
17     print item,
18 I like to use the Internet for:
19 e-mail net-surfing homework chat
View Code

 

还有一种类似计数循环:

 

posted @ 2017-06-20 00:09  Mr.暖阳  阅读(516)  评论(0编辑  收藏  举报