2018-12-26python编程注意点

2018-12-26python编程注意点

欢迎使用 小书匠(xiaoshujiang)编辑器,您可以通过设置里的修改模板来改变新建文章的内容。

数组

premise,conclusion in confidence:
分别表示横纵坐标
conclusion in confidence:
表示横纵坐标

if

多个注意对齐,如果后面的else不知归属哪个if,可以使用elif表示对齐上面的if

continiu 和 break

continiu表示跳出整个for
break表示跳过一句

用户输入

#-- coding: UTF-8 --
raw_input("按下 enter 键退出,其他任意键显示...\n")

以上代码中 ,\n 实现换行。一旦用户按下 enter(回车) 键退出,其它键显示。

print

print "4 - c 的值为:", c

打印数据结构

from pprint import pprint
#pprint模块,用于打印 Python 数据结构. 输出格式比较整齐, 便于阅读
pprint(list(support.items()))
enter description here

key,value,item

key 数组下标
value 值
item值和下标

多行语句

Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠()来实现多行语句,例如:

total = item_one +
item_two +
item_three

在 [], {}, 或 () 中的多行语句,不需要使用反斜杠(),例如:

total = ['item_one', 'item_two', 'item_three',
'item_four', 'item_five']

网络图

enter description here

标准数据类型

Python有五个标准的数据类型:
Numbers(数字)
String(字符串)
List(列表)
Tuple(元组)
Dictionary(字典)

多个变量赋值

a = b = c = 1
a, b, c = 1, 2, "john"

列表、元组、字典

列表[]
元祖()
字典{} 字典由索引 key 和它对应的值 value 组成。

列表与元组的更新

元组是不允许更新的。而列表是允许更新的:

实例(Python 2.0+)

#!/usr/bin/python
#-- coding: UTF-8 --
tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )
list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]
tuple2 = 1000 # 元组中是非法应用
list2 = 1000 # 列表中是合法应用

数据类型可以转换

运算符

算数运算符

a=10,b=20:
** a**b 为10的20次方, 输出结果 100000000000000000000
// 取整除 - 返回商的整数部分(向下取整)
9//2=4
-9//2=-5

Python成员运算符

in
not in

可运用于一系列的成员,包括字符串,列表或元组。

Python身份运算符

is
is not

is 与 == 区别:
is 引用对象是否为同一个, == 值是否相等。

猜你喜欢

转载自www.cnblogs.com/rubyJA/p/10183013.html