Chinaunix首页 | 论坛 | 博客
  • 博客访问: 219903
  • 博文数量: 96
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-14 11:43
文章分类

全部博文(96)

文章存档

2016年(41)

2015年(55)

我的朋友

分类: Python/Ruby

2015-12-10 11:16:27

1. 终端输入
input
raw_input

使用input和raw_input都可以读取控制台的输入,但是input和raw_input在处理数字时是有区别的

当输入为纯数字时:

input返回的是数值类型,如int,float
raw_inpout返回的是字符串类型,string类型

输入字符串为表达式

input会计算在字符串中的数字表达式,而raw_input不会。

如输入“57 + 3”:

input会得到整数60

raw_input会得到字符串”57 + 3”


ps:input 输入字符串的时候要就加引号,否则会程序崩溃。

2. 判断数据类型
type, types.IntType,types.StringType
  1. aaa = 0  
  2. print type(aaa)  
  3. if type(aaa) is types.IntType:  
  4.     print "the type of aaa is int"  
  5. if isinstance(aaa,int):  
  6.     print "the type of aaa is int"  
  7.   
  8. bbb = 'hello'  
  9. print type(bbb)  
  10. if type(bbb) is types.StringType:  
  11.     print "the type of bbb is string"  
  12. if isinstance(bbb,str):  
  13.     print "the type of bbb is string" 
3. python is ==
Python中的对象包含三要素:id、type、value
其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值
is判断的是a对象是否就是b对象,是通过id来判断的
==判断的是a对象的值是否和b对象的值相等,是通过value来判断的

4. python 没有++运算

5. python 字符串不支持变量内插

6. 注释
#单行注释'''多行注释多行注释多行注释'''

7.print 函数
print 会自动在行末加上回车,如果不需回车,只需在print语句的结尾添加一个逗号”,“,就可以改变它的行为。
for i in range(0,5): print i,

或直接使用下面的函数进行输出:

sys.stdout.write("输出的字串")
print可以直接输出list,dictionary
print可以格式化输出

8.cmp函数
cmp(x,y) 函数用于比较2个对象,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1。
阅读(429) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~