Python基础

来源:互联网 发布:联合电子汽车 知乎 编辑:程序博客网 时间:2024/06/05 23:56
# coding=gbkfrom pip._vendor.pyparsing import Eachmovies = ["The Holy Grail","The Life of Brain","The Meaning of Life"]print(movies[0])print(movies)movies.pop()print(movies)movies.append("The append")  #追加一个元素print(movies)movies.extend(["The extend1","The extend2"]) #追加一个数组print(movies)movies.remove("The extend1") #移除print(movies)print(len(movies)) #长度movies.insert(2, "Insert into 2")print(movies)movies.insert(3, 5)print(movies)#循环for movie in movies:    print(movie)movies = ["The Holy Grail",["The Holy Grail",["The Holy Grail","The Life of Brain","The Meaning of Life"],"The Life of Brain","The Meaning of Life"],"The Life of Brain","The Meaning of Life"]def print_lol(the_list):#定义方法    for movie in the_list:        if isinstance(movie, list):            print_lol(movie)  #递归        else:            print(movie)print("-----------------")print_lol(movies)print('''sfsfsftew''') #快速换行print(not True) #falsea = "123";print(a)a=12345print(a)  #python中变量的类型可以改变print(10/3) #3.3333333333333335print(10//3)#3  板除print(len("中文")) #2  字符数print(len("中文".encode(encoding='utf_8', errors='strict'))) #6 字节数'''hello , chg , you have 100000000000 money'''print("hello , %s , you have %d money" %('chg' , 100000000000))

sec

# coding=gbk'''Created on 2017年8月12日@author: Administrator'''from builtins import int'''a = input("input your age")age = int(a) #input得到的是str类型,需要类型转换if age> 17:    print("90后")else:    print("00后")''' print(range(10))   #range(0, 10) print(list(range(10))) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 生成序列sum=0 ;for x in range(101):    sum = sum+xprint(sum)#5050L = ['Bart', 'Lisa', 'Adam']for name in L:    print('hello , ' +name)'''hello , Barthello , Lisahello , Adam'''dic = {'first':100 , 'second':90 , 'thrid':80} #python中的mapprint(dic['first'])    #100dic['four'] = 66;print(dic) #{'thrid': 80, 'second': 90, 'four': 66, 'first': 100} ps:无序print(dic.get('first' , 888)); #100 找到返回对应的valueprint(dic.get('five' , 555)); #555  找不到返回指定的values=set([1,2,2,3,3,4])print(s) #{1, 2, 3, 4}s.add(5)print(s) #{1, 2, 3, 4, 5}s.remove(2)print(s) #{1, 3, 4, 5}

thr

#coding: gbk'''Created on 2017年8月12日@author: Administrator'''print(hex(16)) #0x10 10进制转16进制def my_abs(n):    if n>0:        print(n)    else:        print(-n)my_abs(66);   #66my_abs(-88);  #88     def my_abs2(n):    if n>0:        return n;    else:        return -n;print(my_abs2(66));  #66print(my_abs2(-88)); #88 def my_power(x,n):    result = 1;    while n>0:        n=n-1;        result = result * x;    return result;   print(my_power(5,2)) #25print(my_power(5,3)) #125def login(name , sex, age=18, city='chongqing' , ):    print(name)    print(sex)    print(age)    print(city)login('optics', 1, 22, 'jinan')'''optics122jinan'''print('--------')login('optics', 0)    #不传参则使用默认值'''optics018chongqing'''def calc(*number):    sum = 0 ;    for x in number:        sum = sum+x*x    return sumprint(calc(1,2))  #5print(calc(1,2,3))#14nums=[1,2,3,4]#print(calc(nums)) 报错print(calc(*nums)) #30 '''关键字参数 : 扩展函数的功能'''def person(name , age , **other):    print(name , age , other)person('optics', 22 , comefrom='cq',job='coder')#optics 22 {'job': 'coder', 'comefrom': 'cq'}othermsg = {'comefrom':'china' , 'job':'java'}person('chg', 22 , **othermsg) #chg 22 {'comefrom': 'china', 'job': 'java'}'''*表示变长的参数 list or tuple**表示关键字参数 dict'''def f1(a,b,c=0 , *args ,**other):    print(a,b,c,args,other)f1(1,2) #1 2 0 () {}f1(1,2,3)#1 2 3 () {}f1(1,2,3,name='chg')#1 2 3 () {'name': 'chg'}f1(1,2,3,4,5,name='chg')#1 2 3 (4, 5) {'name': 'chg'}
原创粉丝点击