Python基础

# coding=gbk
from pip._vendor.pyparsing import Each
movies = ["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('''
sfsf
sf
tew

''') #快速换行
print(not True) #false


a = "123";
print(a)
a=12345
print(a)  #python中变量的类型可以改变

print(10/3) #3.3333333333333335
print(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+x
print(sum)#5050

L = ['Bart', 'Lisa', 'Adam']
for name in L:
    print('hello , ' +name)
'''
hello , Bart
hello , Lisa
hello , Adam
'''
dic = {'first':100 , 'second':90 , 'thrid':80} #python中的map
print(dic['first'])    #100
dic['four'] = 66;
print(dic) #{'thrid': 80, 'second': 90, 'four': 66, 'first': 100} ps:无序
print(dic.get('first' , 888)); #100 找到返回对应的value
print(dic.get('five' , 555)); #555  找不到返回指定的value


s=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);   #66
my_abs(-88);  #88     

def my_abs2(n):
    if n>0:
        return n;
    else:
        return -n;

print(my_abs2(66));  #66
print(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)) #25
print(my_power(5,3)) #125

def login(name , sex, age=18, city='chongqing' , ):
    print(name)
    print(sex)
    print(age)
    print(city)

login('optics', 1, 22, 'jinan')
'''
optics
1
22
jinan
'''
print('--------')
login('optics', 0)    #不传参则使用默认值
'''
optics
0
18
chongqing
'''
def calc(*number):
    sum = 0 ;
    for x in number:
        sum = sum+x*x
    return sum


print(calc(1,2))  #5
print(calc(1,2,3))#14

nums=[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'}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值