整型、浮點型、布爾型內容略python
name='python' age=27 # 打印「我是python,今年27歲了」 new_str='我是'+name+',今年'+str(age)+'歲了' print(new_str) # python2的作法 new2='我是%s,今年%d歲了'%(name,age) print(new2) # python3的作法 new3='我是{},今年{}歲了'.format(name,age) print(new3) # 當大括號比較多的時候,還能夠以下作法 new4='我是{name},今年{age}歲了'.format( name='aaa', age='bbb' ) print(new4) # python3.6以後 new5=f'我是{name},今年{age}歲了' print(new5)
1.一、列表的建立數據結構
a=[1,2,3] b=[1,'abc',2.0,['a','b','c']] print(a) print(b) print(a[0],a[1],a[2],sep='-') c=b[1:3] s='abcdef' print(c,type(c)) print(s[1:3],s[-1],type(s))
1.二、列表的基本操做app
# 1、獲取列表的基本信息 list1=[9,1,-4,3,7,11,3] print('list1的長度=',len(list1)) print('list1的最大值=',max(list1)) print('list1的最小值=',min(list1)) print('list1中3這個元素出現了{}次'.format(list1.count(3))) # 2、列表的改變 list2=['a','c','d'] # 列表結尾添加新元素 list2.append('e') print('list2=',list2) # 列表元素之間添加新元素 list2.insert(1,'b') print('list2=',list2) # 刪除列表元素 list2.remove('b') print('list2=',list2) # 修改列表元素 list2[0]='A' print('list2=',list2) ''' 注意這裏要與字符串進行區分 a='123' a[0]='a' 報錯 a='abc' 只能從新賦值 ''' # 列表翻轉 list3=[1,2,3] list3.reverse() print('list3=',list3) # 列表排序 list4=[9,1,-4,3,7,11,3] list4.sort() #升序 # list4.sort(reverse=True) #降序 print('list4=',list4)
1.三、列表解析dom
a=[1,2,3,4,5,6,7,8] b=[item*item for item in a] print(b) c=[item for item in a if item%2==0] print(c)
2.一、元組的建立及訪問函數
# 元組的建立 a=(1,2,3) b=1, print(a,type(a)) print(b,type(b)) # 元組的訪問 print(a[1]) print(a[1:]) print(a[-1])
2.二、元組的方法spa
# 獲取元組的基本信息 tuple1=[9,1,-4,3,7,11,3] print('tuple1的長度=',len(tuple1)) print('tuple1的最大值=',max(tuple1)) print('tuple1的最小值=',min(tuple1)) print('tuple1中3這個元素出現了{}次'.format(tuple1.count(3))) print(tuple1.index(7)) #查詢元素7的下標
3.一、字典的建立及訪問3d
# 字典建立 a={ 1:'a', 2:'b', '3':'c' } print(a) e=dict(a=1,b=2,c='a') print(e) # 字典的訪問 print(e['c']) # 字典的增長、修改 e['d']=123 e['c']=3 print(e)
3.二、字典的基本操做code
d={ 'Name':'Jack', 'Age':9, 'Grade':5, } print(d.get('name')) # 避免傳入字典沒有的key時報錯 print(d.keys()) print(d.values()) print(d.items()) c=d.pop('Name') print(c) print(d) d.clear() print(d)
a={'a','b','c'} t='d' in a print(t) s=[1,2,3,2,4,5,2] s1=set(s) print(s1,type(s1),list(s1)) a='1234512' s2=set(a) print(s2) s3={1,2,3,4} s3.add(5) print(s3) s3.remove(5) print(s3)
a=int(input('請輸入一個數字:')) if a>0: print('這是個正數') elif a==0: print('這是零') else: print('這是負數')
2.一、while循環orm
a=5 while a>0: print(a) a=a-1 print('結束')
2.二、for循環對象
a='12345' b=[1,2,3,4] c=('a','b','c','d') d={1,2,3,5,9} for i in d: # d能夠換成a、b、c print(i) print('------------------------------') e={1:'a',2:'b',3:'c'} for a,b in e.items(): print('{}:{}'.format(a,b)) print('------------------------------') for i in range(1,5): print(i)
2.三、break和continue
for i in range(1,5): print(i) if i%3==0: break print('------------------------------') for i in range(6): # 0~5 if i%2==0: continue print(i)
2.四、小運用
import random a=random.randint(0,100) # 產生【0,100】之間的整數(包括0和100) while True: num=int(input('請輸入0-100之間的整數:')) if num==a: print('猜對了') break elif num>a: print('猜大了,請從新猜') else: print('猜小了,請從新猜')
def sum(a,b): return a+b print(sum(5,6)) print(sum([1,2],[3,4])) def max(a): if not a: return None max_value=a[0] for i in a: if i>max_value: max_value=i return max_value a=[1,4,5,2,3,8,9] print(max(a))
基本常識略,以全局變量舉例
z=1 print(z) def demo(a): global z z=z+a print(z) demo(10) print(z)
def add(*args): result=0 for i in args: result+=i return result print(add(1,2,10,20))
33
def demo(n): if n==1: return n return n*demo(n-1) print(demo(5))
120
class People: def __init__(self,name,age): self.name=name self.age=age def say(self): print('Hi,my name is {},and I am {} years old.'.format(self.name,self.age)) some=People('Jack',20) print(some.name) print(some.age) some.say()
class Animal: def eat(self): print('Animal is eating') class Bird(Animal): def sing(self): print('Bird is sing') class Dog(Animal): def eat(self): print('Dog is eating') a=Animal() a.eat() b=Bird() b.sing() d=Dog() d.eat()
def test(x,y): return x+2*y f=lambda x,y:x+2*y print(test(1,2)) print(f(1,2)) def demo(x,y,f): return f(x,y) print(demo(1,2,lambda x,y:x+2*y)) def add(n): return lambda x:n+x # 高階函數:返回一個函數 s=add(40) # 生成s這個函數 print(s(5)) print(s(-10))
a=[1,2,3,4] for item in map(lambda x:x*x,a): print(item) s=[item for item in map(lambda x:x*x,a)] print(s) from functools import reduce ss=reduce(lambda x,y:x+y,a) print(ss) sss=[item for item in filter(lambda x:x%2==1,a)] print(sss) s4=[item for item in a if item%2==1] # 列表解析一樣能實現相同功能 print(s4)