phython学习笔记

在w3c上学习的笔记:

1.Python的代码块不使用大括号({})来控制类,函数以及其他逻辑判断。python最具特色的就是用缩进来写模块。

2.Python语句中一般以新行作为为语句的结束符。但是我们可以使用斜杠( \)将一行的语句分为多行显示。

例如:

 total = item_one + \ 
        item_two + \
        item_three

3.字符串用单引号双引号三引号表示,三引号可以中间内容换行,注释用#开头,无块注释。

4.函数或方法之间用空行表示,表示一个新函数或方法的开始。

5.Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程。

6.Python允许你同时为多个变量赋。例如:

a = b = c = 1 # 为a/b/c赋值1
a, b, c = 1, 2, "john" # 为a/b/c赋值1,2,john

7.数据类型里值得注意的是:

<1>列表——集合类的数据类型,用“[]”标识例如:

list = [ 'abcd', 768, 2.36, 33]

print list # 输出整个列表

print list[0] #    abcd

print list[1:3] # 输出 768和2.36

print list[2:] # 输出2.36后所有的内容

print list * 2 # 输出列表两次

<2>元祖—— 类似于list的列表,用“()”标识,不同于list的是不可以更新,只读,复制后不可改变。

<3>python元字典,列表是有序对象集合,字典是无序的对象集合。

区别在于:字典当中的元素是通过键来存取的。用“{}”标识。

例如:

test[2] = 'hello'

test1['name'] = charles

test2['id': 23, 'user': 'user01']

print test[2]

print test1['name']

print test2 #完整输出元字典

print test2.keys() # 输出所有键

print test2.values() # 输出所有值

7.运算符:

特殊的几个:幂运算:  **         取整除  //

逻辑运算:

if (a and b) :

   print "a and b"

if (a or b) :

   print "a or b"

成员运算:

if (a in list) :

   print "a in list"

if (a not in list) :

   print "a not in list"

身份运算符:is ,is not 判断两个标识符是否引自同一个对象,结果返回1

8.条件语句:

if :

elif :

9.循环语句:for   while   

while……else……

例如:

count = 0
while count < 5:
   print count, " is  less than 5"
   count = count + 1
else:
   print count, " is not less than 5"
无限循环可以使用 ctrl + C  结束




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章