重点:
-
Python中循环都要加冒号:,不同于C++
-
Python中的缩进有意义,不同于C++
#####################################################################
Head:
#可以不写下面两句 #!/usr/bin/python相当于写死了python路径; #!/usr/bin/env python会去环境设置寻找python目录,推荐这种写法s
Variance:
#python #语句结尾可以不要加分号; name="abcde" name2="ABCDE" print(name,name2) #函数中自动包括空格
//C++ string name="abcde";
string name2="ABCDE";
print(name," ",name2); //函数中要输入空格
Interaction:
name = input(" Input your name:") #input()函数默认输入为字符串,若要转其他格式: age = int( input(...) ) #type()函数显示变量类型 print(type(age), type(str(age)))
Code Comment:
#单行注释用井字符号:#... #多行注释用三个单引号:'''...''' ,也可作为string赋值 info = ''' ------- info of ABC ------ Name:ABC Age:23 ''' print(info) #多行string中指向变量 var1="ABCDE" var2=55 #用%方式, %s表示指向string变量,%d表示指向double变量 info = ''' ------- info of %s ------ Name:%s Age:%d ''' %(var1,var1,var2) print("info=",info) #用.format()设置新变量方式 info2 = ''' ------- info of {_name} ------ Name:{_name} Age:{_age} '''.format(_name=var1, _age=var2) print("info2=",info2) #用.format()无新变量方式 info3 = ''' ------- info of {0} ------ Name:{0} Age:{1} '''.format(var1,var2) print("info3=",info3)
Loop:
#python中循环都要加冒号:,不同于C++ #python中的缩进有意义,不同于C++ #continue,break用法与C++相同 count=0 #while while count<3: #要加冒号: print(count) count+=1 #python中没有count++这个语法 else: #要加冒号: print("OK") #for for i in range(3): print(i) else: print(i,"OK")