Chinaunix首页 | 论坛 | 博客
  • 博客访问: 871218
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: BSD

2012-04-01 17:26:03

1、type 显示变量属性
2、关键字有31个:
and del from not while as elif global or with assert else if pass yield
break except import print class exec in raise continue finally
is return def for lambda try
3、string 不支持数学计算,eg:‘2’ - ‘1’ 是错误的
4、在string 中,如果对字符串使用了+操作符,那么字符串会执行concatenation操作,即字符串的拼接
eg:first=‘hello'   first+first='hellohello' first*2='hellohello'
5、注释采用#
6、类型转换函数
6-1:int可以接受任何数据并且将他们转换为integer,如果不可以的话会报错,如果int转换float类型的数据时,不会执行四舍五入,会强制舍去。
eg:int('32')->32
int('hello')->Traceback (most recent call last):
  File "", line 1, in
ValueError: invalid literal for int() with base 10: 'hello'
int(3.9999)->3
6-2、float会将数据转换为float类型
6-3、str会将数据转换为string类型
7、Math函数,需要导入进来,即import  math
7-1、math.pi->圆周率
7-2、函数定义:
eg:def print_lyrics():
print "hello"
print "world"
定义函数之后,以一个空行结束函数的定义(通过命令行)
7-3、在函数内部定义的变量 都是局部变量,只存在与该函数内部
7-4、如果一个函数返回值为void,并且将该值赋给其他变量的时候,那个该变量的值为None,None的类型为NoneType
7-5、len函数可以对字符串求长
7-6、在一个函数的参数中如果传递另外一个函数的话 ,那么参数的名称为该传递的函数的名称
7-7、for 语法:
eg: for i in range(4):
print 'hello'
7-8、docstring,对函数的描述,使用"""开始同时使用"""结束,一般用在函数声明的后面,比如
eg: def f1():
"""This is a desc for f1
"""
在命令行中使用help(f1)之后即可看到This is a desc...
8、bool值包含True和False,逻辑运算符包含and or 和not
9、if语法中在判断条件之后需要加“:”
eg:if x%3 == 0:
print 'hello'
elif x%3 == 1:
print 'world'
else:
print 'haha'

10、等待用户输入函数为raw_input()
11、string是一系列的单个字符串的拼接,可以使用字符数组去访问,string是不可变的,
eg:a='hello' a[:3]表示从0开始到第三个字符结束的字符串。即hel
eg:b='hello' a[3:]表示从末尾0开始到倒数第三减一个字符结束的字符串,即l0
a[:]表示该字符串,a[0:0]表示为空''。
string中的in方法表示前面的字符串是否存在与后面的字符串中,即'hello' in 'helloworld' ==>True

12、快速打开文件并遍历:
eg fin = open('file.txt'):
for line in fin:
print line


























阅读(833) | 评论(0) | 转发(0) |
0

上一篇:smarter-core-data

下一篇:PyCharm 2.5 序列号

给主人留下些什么吧!~~