- int()取整是只取整数,而不是四舍五入
What will be the value of x when the following statement is executed:
x = int(98.6)
-->>98
- 变量名命名不能以数字开头,只能是字母或下划线
Which of the following is a bad Python variable name?
Spam
SPAM23
_spam
23spam (×)
只能由字母或下划线开头,并只能由字母、数字、下划线(underscore)组成
不能由数字开头
- print(‘’, x)字符串和变量一起输出时候中间会自动有一个空格
name = input("Enter your name")
print("Hello", name)
-->>Hello Sarah
- 乘法运算忘记加*
#错误
pay = 40*r+(h-40)(r*1.5)
#正确
pay = 40*r+(h-40)*(r*1.5)
- time-and-a-half 表示按照正常工资的1.5倍支付
- .format()函数
print("Maximum is {}\nMinimum is {}".format(largest, smallest))
'''
-->>
Maximum is 10
Minimum is 2
'''
- .isdigit()函数
#该代码中没有使用try和except检查错误,在检验输入是否为数字的时候用到了isdigit()函数。
count = 0
total = 0
while True:
shuru = input('Enter a number: ')
if shuru.isdigit():
count = count + 1
total = total + int(shuru)
continue
if shuru == 'done':
break
else:
print("Invalid input")
print('Done!')
print("num: ", count)
print("sum: ", total)
ave = total / count
print("ave: ", ave)
- 空格也占位置
text = "X-DSPAM-Confidence: 0.8475"
p = text.find(':')
num = text[p+1:]
print(num)
#-->> 0.8475
n = float(num)
print(n)
#-->>0.8475
- 读取文档中每一行spilt,每一个行都会分别生成一个list,多行就是多个list
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
listofwords = [line.split() for line in fh]# list comprehension 每一行都生成了lsit,是a list of lists,所以注意下面采用两个for循环
for l in listofwords: # 遍历lists中的每一个list
for word in l:# 从list里面遍历每个词
if word not in lst:
lst.append(word)
lst.sort()
print(lst)
- 跳过空白行,防止blow up
# guardian pattern
if line == '':
continue
# 或者
if len(wds) < 1:
continue
# guardian in compound statement
if len(wds) < 3 #位置在前,这样先运行这个不通就运行or的内容了,这叫做short circuit evaluation
or wds[0] != 'from':
continue
- if语句中对None的逻辑判断只能用is
# 正确
if bigcount is None
# 错误
if bigcount = None
- 设置打开默认文件
if len(name) < 1:
name = "mbox-short.txt"
- sanity check
每写一段落代码,都可以先运行一下,确保这一block没问题 - 关于.sort()和sorted()
sort 是应用在 list 上的方法,属于列表的成员方法,sorted 可以对所有可迭代的对象进行排序操作。
list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
sort使用方法为ls.sort(),而sorted使用方法为sorted() - 采用list comprehension来简化代码
import re
file = open('regex_sum_1605686.txt')
s = 0
for line in file:
numbers = re.findall('[0-9]+', line)
for number in numbers: # 一行里面可能有多个数字,并且numbers返回的是list,需要遍历返回成string
s += int(number) # 将原先的str转换成int才能相加,int()里只能是str
print(s)
# list comprehension版本
print(sum([int(n) for n in re.findall('[0-9]+', open('regex_sum_1605686.txt').read())]))
- 求和或其他数学计算时,一定要检查是类型是否正确
tags = soup('span')
s = 0
count = 0
for tag in tags:
comments = tag.contents[0]
s += int(comments) #上一句返回的是list无法进行算术计算,要转换为int
count += 1
print('Count', count)
print('Sum', s)
- 爬取网页内容时,输出anchor tag、URL、子节点、属性的方法
# Retrieve all of the anchor tags
tags = soup('a') #寻找<a>...</a>
for tag in tags:
print 'TAG:',tag #输出<a>...</a>
print 'URL:',tag.get('href', None) #输出str
print 'Contents:',tag.contents[0] #将tag的子节点以list的方式输出
print 'Attrs:',tag.attrs #属性attribute,是一个字典,默认为空
- enumerate()函数
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
>>> seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
... print i, element
...
0 one
1 two
2 three
- 一个node的parent node对应的不是最终的parent node
which node is the parent node of node e?
<a>
<b>X</b>
<c>
<d>Y</d>
<e>Z</e>
</c>
</a>
==>c
- XML:枚举约束(enumeration constraint)
If you were building an XML Schema and wanted to limit the values allowed in an xs:string field to only those in a particular list, what XML tag would you use in your XML Schema definition?
==>xs:enumeration
'''
下面的例子定义了带有一个限定的名为 "car" 的元素。可接受的值只有:Audi, Golf, BMW:
代码示例:
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
'''
- XPath selector string
counts = tree.findall('.//count') # 使用.//前缀,从当前节点寻找所有的下方节点
# use an XPath selector string to look through the entire tree of XML for any tag named 'count'
参考阅读:
https://zhuanlan.zhihu.com/p/29436838
https://cloud.tencent.com/developer/article/1741898
- parse JSON时格外注意看是list还是dictionary
If the following JSON were parsed and put into the variable x, what Python code would extract “Leah Culver” from the JSON?
{
"users": [
{
"status": {
"text": "@jazzychad I just bought one .__.",
},
"location": "San Francisco, California",
"screen_name": "leahculver",
"name": "Leah Culver",
},
即x = {"users": [{"status": { "text": "@jazzychad I just bought one .__.",},"location": "San Francisco, California","screen_name": "leahculver","name": "Leah Culver",},
==>x["users"][0]["name"]
x#从字典x中["users"]#取出key为users的字典中的[0]#第一个list为几个字典["name"]#从中取出key为name的字典
- JSON读取文件一定要decode(),并且取目标数据时候要注意是否有parentnode,而且是” “双引号取标签
uh = urllib.request.urlopen(url).read().decode('utf-8') # 一定要decode
data = json.loads(uh)
for item in data["comments"]: #注意是否有parentnode
count = item["count"] # 得从"comments"里取出"count"
sum += int(count) # 一定要转换成整数
count += 1
- SELECT COUNT(*) FROM Users
SELECT COUNT(*) FROM Users
COUNT(*)表示 It counts the rows in the table Users
本文介绍了Python编程的一些关键概念,包括变量命名规范、输出操作、字符串格式化、列表操作、if语句、类型检查以及JSON解析。强调了.sort()与sorted()的区别、enumerate()函数的用途,还提到了XML的枚举约束和SQL的COUNT(*)函数应用。

3401

被折叠的 条评论
为什么被折叠?



