python 语法笔记
来源:互联网 发布:客户生日提醒软件 编辑:程序博客网 时间:2024/05/29 07:12
- i=int(raw_input("i=
" )) - if
i== 0: -
print 'i=0' - elif
i== 1: -
print 'i=1' - else:
-
print 'wrong input'
注意这里的冒号
注意这里的强制转换int(),否则raw_input输入的是字符串类型。不能比较。
- i=0
- total=0
- while
i<</span>10: -
i+=1 -
total=total+i -
- print
'i is:' ,i,'totalis:' ,total
注意没有++和=+
- total=0
- for
i inrange( 0,11): -
total=total+i -
- print
total
结果是55.
注意range的最后一个元素不算的。0+1+2+3+4+5+6+7+8+9+10
while和for中都可以使用 break和continue
函数:
- def
printMax(a, b): -
if a > b: -
print a, 'ismaximum\n' -
else: -
print b, 'ismaximum\n'
- printMax(
3, 4) - x
= 5 - y
= 7 - printMax(x,
y)
注意def后面也有冒号
函数的默认参数:
- def
say(message, 1):times = -
print message * times - say(
'Hello' ) - say(
'World' , 5)
结果显示
>>>
Hello
WorldWorldWorldWorldWorl
文档字符串,它通常被简称为 docstrings 。DocStrings是一个重要的工具,由于它帮助你的程序文档更加简单易懂。
你可以使用__doc__(注意双下划线)调用printMax函数的文档字符串属性。
或者help(printMax)。记住按q退出help。
- def
printMax(x, y): '''Prints
the maximum of two numbers.
- The
two '''values must be integers. - if
x > y: -
print x, 'ismaximum' - else:
-
print y, 'ismaximum'
- printMax(
3, 5) - print
printMax.__doc__
显示
>>>
5 is maximum
Prints the maximum of two numbers.
模块的__name__
每个Python模块都有它的__name__,如果它是'__main__',这说明这个模块被用户单独运行,我们可以进行相应的恰当操作。
比如一个模块test.py
if __name__ == '__main__' :
else:
如果单独运行test.py,会显示
>>>
This program is being run by itself
如果在别的程序里面import test
会显示
>>> import test
I am being imported from another module
列表
- shoplist=['A','C','B','D']
- print
shoplist[ 0:4] - print
'I have' ,len(shoplist),'itemsto purchase.' - print
'These items ,are:' Notice the comma at end of the line - for
item inshoplist: -
print item, - print
'\nI also have to buy E.' - shoplist.append('E')
- print
'My shopping ,shoplistlist is now' - print
'I will sort my list now' - shoplist.sort()
- print
'Sorted shopping ,shoplistlist is' - print
'The first ,shoplist[0]item I will buy is' - olditem=shoplist[0]
- del
shoplist[ 0] - print
'I bought ,olditemthe' - print
'My shopping ,shoplistlist is now'
>>>
['A', 'C', 'B', 'D']
I have 4 items to purchase.
These items are: A C B D
I also have to buy E.
My shopping list is now ['A', 'C', 'B', 'D', 'E']
I will sort my list now
Sorted shopping list is ['A', 'B', 'C', 'D', 'E']
The first item I will buy is A
I bought the A
My shopping list is now ['B', 'C', 'D', 'E']
shoplist[0:4]
元组:元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变
- zoo=('A','B','C')
- print
'Number of ,len(zoo)animals in the zoo is' - new_zoo=('D','E',zoo)
- print
'Number of ,len(new_zoo)animals in the new zoo is' - print
'All animals ,new_zooin new zoo are' - print
'Animals brought ,new_zoo[2]from old zoo are' - print
'Last animal ,new_zoo[2][2]brought from old zoo is'
结果显示
>>>
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('D', 'E', ('A', 'B', 'C'))
Animals brought from old zoo are ('A', 'B', 'C')
Last animal brought from old zoo is C
给Perl程序员的注释列表之中的列表不会失去它的身份,即列表不会像Perl中那样被打散。同样元组中的元组,或列表中的元组,或元组中的列表等等都是如此。只要是Python,它们就只是使用另一个对象存储的对象
字典:
- ab={
'A' : 'a@intel.com', -
'B' : 'b@intel.com', -
'C' : 'c@intel.com', -
'D' : 'd@intel.com' -
} - print
"A's address is %s" �['A'] - #
Adding a key/value pair - ab['E']='e@intel.com'
- #
Deleting a key/value pair - del
ab[ 'D'] - print
'There are %d contacts in the address-book' %len(ab) - for
name,address inab.items(): -
print 'Contact %s at %s' %(name,address) - if
'E' in ab: #OR ab.has_key('Guido') -
print "E's address is %s" �['E']
结果显示
>>>
A's address is a@intel.com
There are 4 contacts in the address-book
Contact A at a@intel.com
Contact C at c@intel.com
Contact B at b@intel.com
Contact E at e@intel.com
E's address is e@intel.com
我们使用字典的items方法,来使用字典中的每个键/值对。这会返回一个元组的列表,其中每个元组都包含一对项目——键与对应的值。
参考,赋值:
只是如果你想要复制一个列表或者类似的序列或者其他复杂的对象(不是如整数那样的简单对象),那么你必须使用切片操作符来取得拷贝。如果你只是想要使用另一个变量名,两个名称都是参考同一个对象
- shoplist=['A','B','C','D']
-
- print
'Simple Assignment' - mylist=shoplist
- del
shoplist[ 0] - print
'shoplist is' ,shoplist - print
'mylist is' ,mylist -
- print
'Copy by making a full slice' - mylist=shoplist[:]
- del
mylist[ 0] - print
'shoplist is' ,shoplist - print
'mylist is' ,mylist
显示结果一目了然:
>>>
Simple Assignment
shoplist is ['B', 'C', 'D']
mylist is ['B', 'C', 'D']
Copy by making a full slice
shoplist is ['B', 'C', 'D']
mylist is ['C', 'D']
字符串
- name='Swaroop'
- if
name.startswith( 'Swa'): -
print 'Yes, the string starts with "Swa"' - if
'a' in name: -
print 'Yes, it contains the string "a"' - if
name.find( 'war')!=-1: -
print 'Yes, it contains the string "war"' -
- A='
* ' - mylist=['Brazil','Russia','India','China']
- print
A.join(mylist)
结果显示
>>>
Yes, the string starts with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil * Russia * India * China
1、filter(bool_func,seq):此函数的功能相当于过滤器。调用一个布尔函数bool_func来迭代遍历每个seq中的元素;返回一个使bool_seq返回值为true的元素的序列。
- >>>
filter( lambdax 2: x% == 0,[1,2,3,4,5]) - [2,
4]
2、map(func,seq1[,seq2...]):将函数func作用于给定序列的每个元素,并用一个列表来提供返回值;如果func为None,func表现为身份函数,返回一个含有每个序列中元素集合的n个元组的列表。
- >>>
map(lambda x : None,[1,2,3,4]) - [None,
None, None, None] - >>>
map(lambda x : x * 2,[1,2,3,4]) - [2,
4, 6, 8] - >>>
map(lambda x : x * 2,[1,2,3,4,[5,6,7]]) - [2,
4, 6, 8, [5, 6, 7, 5, 6, 7]] - >>>
map(lambda x : None,[1,2,3,4]) - [None,
None, None, None]
结果是[3,7,11]
如果碰到这样的两组序列:a =[1,2,3], b = [4,5],这里顺带比较一些map和zip
上面的处理,可以看到map是做最大的集合,而zip则是根据最小的集合。
3、reduce(func,seq[,init]):func为二元函数,将func作用于seq序列的元素,每次携带一对(先前的结果以及下一个序列的元素),连续的将现有的结果和下一个值作用在获得的随后的结果上,最后减少我们的序列为一个单一的返回值:如果初始值init给定,第一个比较会是init和第一个序列元素而不是序列的头两个元素。
- >>>
reduce(lambda x,y : x + y,[1,2,3,4]) - 10
- >>>
reduce(lambda x,y : x + y,[1,2,3,4],10) - 20
- python 语法笔记
- Python语法笔记
- python语法笔记
- python语法笔记
- Python基本语法笔记
- Python基础语法笔记
- Python 语法笔记
- python学习笔记-语法
- python基本语法笔记
- Python基本语法笔记
- Python基础语法笔记
- Python语法笔记
- Python语法笔记
- python语法笔记
- Python基本语法笔记
- 【Python语法笔记】列表
- 【Python语法笔记】字典
- 【Python语法笔记】集合
- argc和argv的定义和用法
- 再读C++ primer,提取重点
- systemverilog 的 OOP
- Makefile 笔记
- Primetime 的使用
- python 语法笔记
- python 常用模块
- python PIP 或者&…
- PYTHON 的 YAML 和PKL
- I2C 的开漏和上拉电阻
- Python 处理文件例程
- Git 的有用操作 (更新)
- PyQt学习笔记(7)——Qt Design…
- linux 将模块编译进内核