[root@localhost ~]# python Python 2.7.5 (default, Jun 24 2015, 00:41:19) [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print ‘hello,word‘ hello,word >>>exit()
在交互模式中 退出用exit()函数
输入输出:
输出用print函数 :该函数前面不能加空格
可以将所有的字符用逗号连接起来,生成一个字符串;
比如:
>>> print ‘lixing‘,‘wo aini‘ lixing wo aini >>>
print
会依次打印每个字符串,遇到逗号“,”会输出一个空格,因此,输出的字符串是这样拼起来的:
print 打印整数和计算 如下:
>>> print 100 100 >>> print 100+2 102 >>> print 100*100 10000 >>>
raw_input():从终端输入字符,如下:
>>> name=raw_input() lixing >>> name ‘lixing‘ >>> print name lixing >>>
name是一个字符变量;在py中变量不需要事先声明是字符型还是整形浮点型等。
编写一个脚本test.py
#!/usr/bin/env python name=raw_input(‘please enter your name:‘) print ‘hello,‘,name
执行这个脚本./test.py
[root@localhost lear_py]# ./test.py please enter your name:lixing hello, lixing [root@localhost lear_py]#
字符串
转义符 \ 不转义 r 如下:
>>> print ‘\\your\‘s py‘ \your‘s py >>> print r‘\\your\‘s py‘ \\your\‘s py >>>
字符编码
ord():将字符转化为对应的ascii编码
chr():将ascii吗转化为字符
u‘....‘: 用Unicode表示的字符串
>>> len(u‘abc‘) 3 >>> len(‘abc‘) 3 >>> ‘中国‘.decode(‘utf-8‘) u‘\u4e2d\u56fd‘ >>> u‘中国‘.encode(‘utf-8‘) ‘\xe4\xb8\xad\xe5\x9b\xbd‘ >>> u‘中国‘ u‘\u4e2d\u56fd‘ >>>
#!/usr/bin/env python # -*- coding: utf-8 -*- a=100 if a>=1000: print ‘值为:‘,a else: print ‘值为:‘,-a
原文:http://my.oschina.net/u/2420214/blog/507202