先安装Python运行环境:
下载python2.7,配置python环境变量,在命令行输入pathon,出现版本信息即安装成功。
python语法特点:
#coding=utf-8 #注释中中文出错的解决办法
print 'hello world'
if True:
print 'hello' #注意空格,多行注释用''' '''
_name='peter' #变量名合法
age=24
#say='let's it go' 会出错的!引号是成对出现,把单引号改为双引号就行,这也是Python出现单引号的原因
say='let\'s it go' #可以
print name
print type(24) #输出类型,结果是<type 'int'>
print 2**3 #2的3次方
print id(name) #获取内存地址
print 3.0/2 #结果是1.5
print 3.0//2 #结果是1.0 ,这是整数除法
a=int(raw_input('please input num1:')) #用户输入数值
b=int(raw_input("please input num2:"))
print a-b
#数据类型:字符串,元祖,列表,字典
str1='abcdefg' #字符串操作
str2='1234567'
print str1[0]
print str1[ :4]
print str1[::2] #从开头到结尾,两步一取,结果aceg
print len(str1)
print str1+str2
print str1*5
print "#"*40
print "c" in str1
print max(str2)
print cmp(str1,str2)
t1=("chenchen",12,"male") #元祖tuple
print t1[0]
t2=()
t3=(1,)
print type(t1)
print type(t2)
print type(t3)
name,age,sex=t1
print t1
a,b,c=(1,2,3)
print a
listdemo=[] #列表list
print type(listdemo)
listdemo=["chenchen",12,"male"]
t=("chenchen",12,"male")
print t[0]+"\n"+listdemo[0]
print listdemo[0:]
print t[0:2]
listdemo[1]=13 #元祖不可改值,序列可以
listdemo.append("student")
#listdemo.remove('student')
#listdemo.remove(listdemo[3])
del(listdemo[3])
print listdemo
dic={'name':'chenchen','age':24,'sex':'male'} #字典
#print dic['name']
for k in dic:
print k
for k in dic:
print dic[k]
dic['tel']='15951813036' #插入顺序不固定,字典是哈希类型
print dic.pop('tel') #弹出并删除
#dic.clear() #清空里面
#del(dic) #删除字典
print dic.get('ah','error') #取不到,输出error
print dic.keys()
print dic.values()
#print dic
def func(): #if-else
return 1
if func():
print 'ok!'
"""x=int(raw_input("please input number:"))
if x>3:
print 'x>3'
elif x<3:
print 'x<3'
else:
print 'x=3'"""
if 1<2 and 2<3: #and,or,not
print 'ok'
if 1<2 or 2>3:
print "ok"
if not 1>2:
print 'ok'
print "#"*40
"""str='abcd' #for
for x in str:
print x,'hello'
for x in [0,1,2]:
print str[x],'hello'
for x in range(len(str)):
print str[x],'python'"""
s=0
for x in range(1,101):
s=s+x
print s
s="abcd"
t=(1,'as','x33',3)
l=[1,'as','x33',3]
for i in s: #字符串,元祖,列表都可以这样遍历
print i
for x in range(0,len(s)): #字符串,元祖,列表都可以这样遍历
print s[x]
d={'name':'chenchen','age':24,'sex':'male'}#遍历字典
for k,v in d.items(): #d.item()把字典变为元祖
print k,v
for i in range(10):
print i
if 2==i:
pass
if 3==i:
print 'hello 333333333'
continue #加不加没区别
if 6==i:
break
else:
print 'ending'#正常结束就显示ending
print "#"*40
"""x='' #while
while x!='q':
print 'hello'
x=raw_input("please input 'q' exit:")
else:
print 'ending'#正常结束就显示ending
"""
def func(a=4,b=3): #函数
print a,b
func(3)
func(88,38)
func()
func(b=3)
def func1():
a=100
global p
p=200
print a,p
func1() #这句必须有,使得func1运行,里面定义全局变量p
print p
def func2(x,y):
print x,y
t10=("name","age")
func2(t10,1) #元祖是个整体,只是一个值
func2(*t10) #*代表是个元祖
d={'name':'chen','age':24}
#func2(**d)#**代表是字典,但传字典键值要求与形参键值一致,否则出错
func2(d['name'],d['age'])
def func3(a,*b): #b是元祖,参数除第一个值,其余放进元祖
print a,b
func3(1,2,4,5,67,8) #输出是1 (2,4,5,67,8)
def func4(a,*b,**c): #c是字典
print a,b,c
func4(1,2,3,4,5,6,7,x=3,y=4) #输出是1 (2,3,4,5,6,7) {'x':3,'y':4}
g=lambda x,y:x*y #lambda匿名函数
print g(2,3)
l=range(1,11) #reduce函数与filter函数
def func5(x,y):
return x*y
print reduce(func5,l) #10!
print filter(lambda x:x%2==0,l) #1~10中偶数
list2=range(1,11)
print reduce((lambda x,y:x*y),list2) #10!,lambda省去不需要的函数名
'''a=int(raw_input("please input :"))#简单计算器
b=raw_input("please input :")
c=int(raw_input("please input :"))
if b=='+':
print (lambda x,y:x+y)(a,c)
elif b=='-':
print (lambda x,y:x-y)(a,c)
elif b=='*':
print (lambda x,y:x*y)(a,c)
elif b=='\\':
print (lambda x,y:x/y)(a,c)
else:
pass'''
'''dic1={'+':lambda x,y:x+y,'-':lambda x,y:x-y,'*':lambda x,y:x*y,'/':lambda x,y:x/y}#简单计算器
a=int(raw_input("please input :"))
b=raw_input("please input :")
c=int(raw_input("please input :"))
print dic1[b](a,c)
print "#"*40'''
'''print round(112.124) #返回浮点数 #内置函数
print callable(func2)#测试函数是否可调用,前提是存在
print type(3)
print isinstance(list2,list)#测试是否是列表
print cmp('he','he')
print range(10)'''
s='h,el,lo'
print s.capitalize() #首字母大写
print s.replace('hello','good')
list3=s.split(',') #切割1次
print list3[0:2]
name=['milo','zou','tom']
age=[20,21,22]
tel=['110','120']
print zip(name,age,tel) #比较zip与map的区别
print map(None,name,age,tel)
age1=[1,2,3]
age2=[7,8,9]
s=lambda age1,age2:age1*age2
print map(s,age1,age2) #map第一个参数是函数名