Python_输入与输出

/ Python / 没有评论 / 791浏览

一个最简单的输出

>>> print ('hello word')

打印计算器效果

>>> print (100 +200)
300

打印的优雅一点呢?

#输入
>>> name = input()
123
>>> print name
123
>>> name
123

再优雅一点

>>> name = input("please enter your name:")     #这里的双引号,单引号都可以
please enter your name:123
>>> print name
123
>>> name 
123
>>> name = input('please enter your name:')
please enter your name:1234
>>> print name
1234
>>> name
1234

字符串转义与换行

#要想输出 I'm "ok" ! ,要在I'm 之间的" ' " 前面加上""转义,ok的双引号也需要转义

>>> print('I\'m \"ok\" !')
I'm "ok" !

转义字符

  1. 换行示范
>>> print ('I\'m learning \nPython')
I'm learning 
Python
  1. 制表符示范
>>> print ('\t\t\t\t\t\t00000\t\t\t\t\t\t\t000')
						00000							000
>>> print('\t\t\t000\n\t\t000\t')
			000
		000	
>>> print(r'\t\t\t000\n\t\t000\t')
\t\t\t000\n\t\t000\t
>>> print('''line111
... line222
... line333''')
line111
line222
line333

布尔值

示范("<" ">" "=")等于号特殊,注意

>>> 3<2
False
>>> 3>2
True
>>> 3==2
False

或 or 的示范

>>> print (3>2 or 2>3)
True
>>> print (3>2 or 4>3)
True
>>> print (1>2 or 2>3)
False

与 and 的示范

>>> print (1<2 and 2<3)
True
>>> print (1<2 and 4<3)
False
>>> print (1==2 and 4<3)
False

not 非的示范

>>> print (not 1==2 and 2==2)
True
>>> print (not 1==1)
False

函数input()

函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便使用

示范:

[root@pa1 lijinghua]#python input.py 
tell me your name:lijinghua
your name lijinghua
[root@pa1 lijinghua]#cat input.py
#!/usr/local/python
name = input("tell me your name:")
print("your name "+name)

注意: 本人在Python2.7下,会出现一些问题

使用int()来获取数值输入

使用函数input()时,Python将用户输入解读为字符串,在做一些数值比较时,将会出错,不能将字符串和数值做比较 需要用int()函数先转化为数值

示范:

[root@pa1 lijinghua]#python input2.py
print enter your age:23
She/He is adult
[root@pa1 lijinghua]#cat input2.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
age = input('print enter your age:')
#print (name)
if int(age) >17:
	print (r'She/He is adult')
elif int(age) >= 6:
	print (r'She/He is teenager')
else:
	print (r'She/He is kid' )