Python的简单语法(一)
import sys a=3 b=4 c=5.66 d=8.0 e=complex(c,d) f=complex(float(a),float(b)) print("a is type:",type(a)) print("c is type:",type(c)) print("e is type:",type(e)) print(sys.float_info) age=3 name="sunliyuan" print("{0} was {1} years old".format(name, age)) '''保证所有的变量都是字符串''' print(name+"was"+ str(age) +" years old")
List结构列表:
#创建list number_list=[1,3,5,7,9] print("number_list:"+ str(number_list)) string_list=["abc","bbc","python"] #创建混合的元素列表 mixed_list=["python",".net",3,12] print("string_list"+str(string_list)) print("mixed_list"+ str(mixed_list)) #访问列表中的 second_num=number_list[1] third_string=string_list[2] fourth_mixed=mixed_list[3] print("second_num:{0} third_string:{1} fourth_mixed:{2}".format(second_num,third_string,fourth_mixed)) #更新列表中的元素 number_list[1]=30 print("number_list after: "+str(number_list)) #删除list 的元素 del number_list[1] print("number_list after del: "+ str(number_list)) #判断是否在元素内 print(len([1,2,3])) print([1,2,3]+[4,5,6]) print(['Hellow']* 4) print(3 in [1,2,3]) abcd_list=['a','b','c','d'] print(abcd_list[1]) #除右边数 print(abcd_list[-2]) #: 代表剩下的所有元素 print(abcd_list[1:])
换行:
print("what's your name? \n Tom")
Tuple:一旦创建不能修改
number_tuple=(1,3,5,7,9) print("number_list:"+ str(number_tuple)) string_tuple=("abc","bbc","python") mixed_tuple=("python",".net",3,12) print("number_list:"+ str(number_tuple)) print("string_list"+str(string_tuple)) print("mixed_list"+ str(mixed_tuple)) #Tuple(元组)一旦被创建就不可以更改 List可以 #删除 Tuple不可以删除其中的元素 但是可以删除整个Tuple del mixed_tuple #打印长度 print(len((1,2,3))) print((1,2,3)+(4,5,6)) print(('Hellow') * 4) print(3 in (1,2,3)) abcd_list=('a','b','c','d') print(abcd_list[1]) #除右边数 print(abcd_list[-2]) #: 代表剩下的所有元素 print(abcd_list[1:])
Tuple和List
#创建只包含一个元素的tuple a_tuple=(2,) #Tuple中的list mixed_tuple=(1,2,['a','b']) print("mixed_tuple: " +str(mixed_tuple)) #更改tuple中的list mixed_tuple[2][0]='c' mixed_tuple[2][1]='d' print("mixed_tuple after " +str(mixed_tuple)) #Tuple比List操作速度要快。如果定义了一个值得常量集,并且唯一要做的不断地遍历他,使用Tuple代替list Tuple比较安全(不能修改)