Python基础
根据链接Tutorial/docs/L0/Python/ch2_python_intro.md at camp3 · InternLM/Tutorial (github.com)逐条学习python代码
1.Python基础语法
if True:
print('hello world!')
print('hello python!')
else:
print('goodbye python!')
print('goodbye world!')
hello world!
hello python!
python基本数据类型:
- 数字Number
- int整数
- float浮点数
- complex复数
- 布尔型bool
- 列表list
- 元组tuple
- 集合set
- 字典Dictionary
可以修改的是:列表list,集合set,字典Dictionary
不可以修改的是:数字Number,布尔型bool,元组tuple
#python中变量赋值的方式
a = 1
b = 2
print(a+b)
a,b = 1,2 #另一种单行赋值的方式
print(1,2)
#下面让我们来看看各种类型的数据长什么样
t_int = 1 #整数
t_float = 1.0 #浮点数
t_complex = 1.2j #复数
t_bool = True #布尔类型
t_list = [1,1,3,3,5,5] #列表
t_tuple = (1,1,3,3,5,5) #元组
t_set = {1,3,5} #集合
t_dict = {'day':18,'month':6,'year':2024} #字典
print(" t_int的类型是:",type(t_int),' t_int的值是: ',t_int)
print(" t_float的类型是:",type(t_float),' t_float的值是: ',t_float)
print(" t_complex的类型是:",type(t_complex),' t_complex的值是: ',t_complex)
print(" t_bool的类型是:",type(t_bool),' t_bool的值是: ',t_bool)
print(" t_list的类型是:",type(t_list),' t_list的值是: ',t_list)
print(" t_tuple的类型是:",type(t_tuple),' t_tuple的值是: ',t_tuple)
print(" t_set的类型是:",type(t_set),' t_set的值是: ',t_set)
print(" t_dict的类型是:",type(t_dict),' t_int的值是: ',t_dict)
3
1 2
t_int的类型是: <class 'int'> t_int的值是: 1
t_float的类型是: <class 'float'> t_float的值是: 1.0
t_complex的类型是: <class 'complex'> t_complex的值是: 1.2j
t_bool的类型是: <class 'bool'> t_bool的值是: True
t_list的类型是: <class 'list'> t_list的值是: [1, 1, 3, 3, 5, 5]
t_tuple的类型是: <class 'tuple'> t_tuple的值是: (1, 1, 3, 3, 5, 5)
t_set的类型是: <class 'set'> t_set的值是: {1, 3, 5}
t_dict的类型是: <class 'dict'> t_int的值是: {'day': 18, 'month': 6, 'year': 2024}
算数运算符
print("1+1 =",1+1)
print("2-1 =",2-1)
print("2*4 =",2*4)
print("2*4 =",2*4)
print("8//3 =",8//3)
print("8%5 =",8%5)
print("2**3 =",2**3)
1+1 = 2
2-1 = 1
2*4 = 8
2*4 = 8
8//3 = 2
8%5 = 3
2**3 = 8
比较运算符
a,b = 10,20
print("a>b =",a>b)
print("a<b =",a<b)
print("a>=b =",a>=b)
print("a<=b =",a<=b)
print("a==b =",a==b)
print("a!=b =",a!=b)
赋值运算符
a,b = 2,2
a += 1
b = b+1
print("a+=1,a=",a)
print("b=b+1,b=",b)
print("-"*20)
a,b = 2,2
a -= 1
b = b-1
print("a-=1,a=",a)
print("b=b-1,b=",b)
print("-"*20)
a,b = 2,2
a *= 2
b = b*2
print("a*=2,a=",a)
print("b=b*2,b=",b)
print("-"*20)
a,b = 2,2
a /= 2
b = b/2
print("a/=2,a=",a)
print("b=b/2,b=",b)
print("-"*20)
a,b = 2,2
a //= 2
b = b//2
print("a//=2,a=",a)
print("b=b//2,b=",b)
print("-"*20)
a,b = 2,2
a %= 3
b = b%3
print("a%=3,a=",a)
print("b=b%3,b=",b)
print("-"*20)
a,b = 2,2
a %= 3
b = b%3
print("a%=3,a=",a)
print("b=b%3,b=",b)
print("-"*20)
a,b = 2,2
a **= 3
b = b**3
print("a**=3,a=",a)
print("b=b**3,b=",b)
逻辑运算符
a,b = True,False
print("a and b =",a and b)
print("a or b =",a and b)
print("not b =",not a)
列表
alist = [1,2,3,4,5,6] #列表使用[]表示,每个元素之间用,隔开
print('alist :',alist)
blist = list("hello world!") #列表也可以使用list()函数将其他可迭代的对象转换为列表,比如字符串。字符串的结构我们会在字符串小节里具体介绍
print('blist :',blist)
print('elements: ',' '.join([str(i) for i in alist]))
print('正向索引: ',' '.join([str(i) for i in range(0,len(alist))]))
print('反向索引: ',' '.join([str(i) for i in range(-len(alist),0)]))#这里的len函数是用来获取列表长度的,后面会具体介绍
alist = [1,2,3]
blist = [4,5,6]
print('alist+blist: ',alist+blist)
print('alist*3: ',alist*3)
print('3 in alist: ',3 in alist)
print('4 in alist: ',4 in alist)
alist = [0,1,1,2,3,4,5,6,7,8]
print(len(alist))
print(max(alist))
print(min(alist))
alist.sort(key=None,reverse=False)
alist
元组
atuple = (1,2,3)
print(atuple[0])
print(atuple[1:3])#元组的索引与切片与列表一致
atuple = (1,2,3)
btuple = (4,5,6)
print('alist+blist: ',atuple+btuple)
print('alist*3: ',atuple*3)
print('3 in alist: ',3 in atuple)
print('4 in alist: ',4 in atuple)
atuple = (1,2,3,4)
print(len(atuple))
print(max(atuple))
print(min(atuple))
集合
#新建一个集合
aset = {1,2,3,4,5}
print('这是一个新集合: ',aset)
#新建一个空集合
aset = set()
print('这是一个空集合: ',aset)
#使用remove或discard删除集合中的指定的一个元素
bset = {3,4,5}
print('bset: ',bset)
bset.remove(3)
print('bset: ', bset)
bset.discard(4)
print('bset: ', bset)
字典
adict = {"list" : [1,2,3],'list2':[1,2,3],'interLM': 'LLM', 1:'summer camp',2:'internLM'}
#访问key为list的元素的值
print("adict['list'] :", adict['list'])
#修改key为list的元素的值
adict['list'] = [3]
print("adict['list'] = [3]后的adict: ",adict)
#删除字典中的元素
del adict['list2']
print(adict)
adict['list'] : [1, 2, 3]
adict['list'] = [3]后的adict: {'list': [3], 'list2': [1, 2, 3], 'interLM': 'LLM', 1: 'summer camp', 2: 'internLM'}
{'list': [3], 'interLM': 'LLM', 1: 'summer camp', 2: 'internLM'}
字符串
#字符串的方法实在太多,在在这里我们只取几个比较常用的方法作为示例。
text = "Success is not final, failure is not fatal: It is the courage to continue that counts."
#首先我们把这句话中所有的字母都转换为小写
text = text.lower()
print(text)
#再用replace把所有的标点符号去掉
text = text.replace(','," ").replace(':'," ").replace('.'," ")
print(text)
#再用split把这句话拆成词组成的列表
word_list = text.split(" ")
print(word_list)
#再用join函数用,把这个列表拼会一个字符串
print(','.join(word_list))
success is not final, failure is not fatal: it is the courage to continue that counts.
success is not final failure is not fatal it is the courage to continue that counts
['success', 'is', 'not', 'final', '', 'failure', 'is', 'not', 'fatal', '', 'it', 'is', 'the', 'courage', 'to', 'continue', 'that', 'counts', '']
success,is,not,final,,failure,is,not,fatal,,it,is,the,courage,to,continue,that,counts,