1、raw_input 是输入
>>> raw_input('please input your name:')
please input your name:Jessie
'Jessie'
解析:raw_input,都是对输入的内容作字符处理的,可以这样
>>> age = int (raw_input('please input your age:'))
----------------------------------------------------------------
str1 = raw_input('Enter a string:')
str2 = raw_input('Enter another string:')
print('str1 is:'+str1 + '.str2 is:' + str2)
print 'str1 is:%s.str2 is:%s.'%(str1,str2)
print('str1 is:{}.str2 is:{}.'.format(str1,str2))
# raw_input和input两个函数均能接收 字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。
# 而对于 input() ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。
# raw_input() 将所有输入作为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float );
# input() 可接受合法的 python 表达式,举例:input( 1 + 3 ) 会返回 int 型的 4
2、print
>>> print 'Information of ',name,':\nage:',age,'\nsex:',sex,'\njob:',job
Information of Jessie :
age: 30
sex: woman
job: engineer
或者可以这样,多行输出
>>> print '''\tname:%s
... \tage:%s
... \tjob:%s'''%(name,age,job)
name:Jessie
age:30
job:engineer
解析:%s用来引用字符的;%d用来引用数字。例如:
>>> print 'your age is:%d'%age
your age is:23
-------两个%之间不需要用,隔开。如print ‘your age is :%d’,%age,这个是错误的
3、if...else....
name = raw_input('Please input your name:')
age = int(raw_input('Please input your age:'))
job = raw_input('Please input your job:')
print '----------------\n'
if age < 28:
print "Congruatulations,you can still have the holiday at May 4th!"
elif age < 40:
print "You can have a holiday cause of your beautiful daughter"
else:
print "Your need to go back to work,don't even think about it!"
print '''\tYour name is:%s
\tYour age is:%s
\tYour job is:%s'''%(name,age,job)
4、# -*- coding:utf-8 -*-
如果要在python2的py文件里面写中文,则必须要添加一行声明文件编码的注释,否则python2会默认使用ASCII编码。
5、实现函数参数的多个不定参数
*nums,代表没有用关键字指明的参数列表,当作tuple来对待
**words代表的是有用关键字指明的参数列表,当作dictionary来对待
def print_paras(fpara,*nums,**words):
print("fpara:" + str(fpara))
print("nums:" + str(nums))
print("words:" + str(words))
print_paras('hello',1,2,3,4543,43534.444,'world',wordsd = 'python')
#输出结果
fpara:hello
nums:(1, 2, 3, 4543, 43534.444, 'world')
words:{'wordsd': 'python'}
#但是如果指明混乱,没有按先未指明后指明,则会报错
print_paras('hello',1,2,wordsd = 'python',3,4543,43534.444,'world')
#输出结果
File "E:\Python\function.py", line 6
print_paras('hello',1,2,wordsd = 'python',3,4543,43534.444,'world')
SyntaxError: non-keyword arg after keyword arg
6、通过for来遍历一个dictionary并输出
a_dict = {'Tom':'111','Jessie':'222','Cathy':'333'}
for ele in a_dict:
print(ele)
print(a_dict[ele])
或者:
for key,elem in a_dict.items():
print(key + ':' + elem)
7、#写入文件
somesentence = '''\
If you want a more confortable life,
must work harder.
Try more style.'''
f = open('E:\Python\sentence.txt','w')
f.write(somesentence)
f.close
8、#读取文件
f = open('E:\Python\sentence.txt')
while True:
line = f.readline()
if len(line) == 0:
break
print(line)
f.close
9、异常处理
try:
f = open('E:\Python\sentence1.txt')
s = f.readline()
i = int(s.strip())
except OSError as err:
#如果是系统错误的话,将执行这个except并且输出错误,例如没有这个文件
print('OSError:{}'.format(err))
except ValueError:
#如果是内容类型错误将执行这个except,例如读取的内容是字符型却需要强制转换为int就会报valueError
print('Could not convert date to an integer.')
声明:s为字符串,rm为要删除的字符序列
s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符
s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字符
s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的字符
注意:
1. 当rm为空时,默认删除空白符(包括'\n', '\r', '\t', ' ')
10、#GUI的使用
from Tkinter import *
import tkSimpleDialog as dl
import tkMessageBox as mb
root = Tk()
w = Label(root,text = 'Label Title')
w.pack()
mb.showinfo('Welcome','Welcome Message')
guess = dl.askinteger('Number','Enter a number:')
output = 'This is output message.'
mb.showinfo('output:',output)