主要内容:环境配置、基本数据类型、基本序列类型
参考:B站python光速入门、python官方文档
python release for windows vscode anconda 环境配置建议使用anconda vscode教程网上百度很多
>>> bi=True ##首字母大写否则会报错
>>> b1=false
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'false' is not defined
>>> b1=2>3
>>> print(b1)
False
>>> 3+3
6
>>> 7/2
3.5
>>> 7//2 ####整除
3
>>> 7%2 ####求余数
1
>>> s='abc'
>>> s
'abc'
>>> s="""abc
... def""" #####三重双引号或者三个单引号可以直接回车换行,一边用来写大量的注释
>>> s
'abc\ndef'
>>> s='abc\ndef' ######单引号需要加入换行符
>>> s
'abc\ndef'
>>> len(s) ###字符串的长度
7
>>> s="哈哈123"
>>> s
'哈哈123'
>>> len(s) ###python 里面可以直接识别中文检测到共有5个字符
5
>>> s1="123"
>>> t=s+s1 #####字符串拼接,可以直接相加
>>> t
'哈哈123123'
>>> t=t+12 #####不能直接与int数值相加
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> t=t+str(12) ###用str将整数转换为字符串
>>> t
'哈哈12312312'
>>> isinstance(t,str) ###用来判断是不是某种类型
True
可以使用Python中内置的函数对变量类型进行转换
int()
:将一个数值或字符串转换成整数,可以指定进制float()
:将一个字符串转换成浮点数。str()
:将指定的对象转换成字符串形式,可以指定编码chr()
:将整数转换成该编码对应的字符串(一个字符)ord()
:将字符串(一个字符)转换成对应的编码(整数)
list(列表)、turple、range 类似于c语言的数组
>>> x=[1,2,3]###list可以添加元素
>>> type(x)
<class 'list'>
>>> x.append(4) ###对list进行添加变量
>>> x
[1, 2, 3, 4]
>>> x.append(2)
>>> x
[1, 2, 3, 4, 2]
>>> x[1] is x[4] ###发现这两个完全等同
True
>>> t
['a', 'd', 'c']
>>> s=[1,2,3]
>>> s+t ###列表相加
[1, 2, 3, 'a', 'd', 'c']
>>> x=s+t
>>> x
[1, 2, 3, 'a', 'd', 'c']
>>> x[3]="xtg" ###列表修改列表里面的对象进行修改,python内部对于原来的对象会进行释放
>>> x
[1, 2, 3, 'xtg', 'd', 'c']
>>> x*2 #####列表乘以一个数,对列表每个元素的增加
[1, 2, 3, 'xtg', 'd', 'c', 1, 2, 3, 'xtg', 'd', 'c']
>>> x=[ ] ####设置一个空列表
>>> x
[]
>>> x.append(1) ###可以往里面添加元素
>>> x
[1]
>>> x=[[ ]]*3 #####这样创建的三个空元素指向的是同一个空列表
>>> x
[[], [], []]
>>> x[1].append(1)### 因为指向的同一个空列表,定义就会是一样的
>>> x
[[1], [1], [1]]
>>> x=[[],[],[]] ####这样创建的列表是三个空元素对应不用的空列表
>>> x
[[], [], []]
>>> x[1].append(1)#### 添加一个对另外的不影响
>>> x
[[], [1], []]
#########切片
>>> x=[1,2,3]
[1,2,3]
>>> y=x[1:3] ####不包括三并且这样取出来的列表是新的,与原来的不搭边,就是说改变y并不会改变x.
>>> y
[2, 3]
>>> x=(1,2,3)
>>> x
(1, 2, 3)
>>> type(x)
<class 'tuple'>
##### 列表可以转换为元组
>>> x=[1,2,3]
>>> x
[1, 2, 3]
>>> x=tuple(x)
>>> x
(1, 2, 3)
>>> type(x)
<class 'tuple'>
>>> x1=[1,2]
>>> x2=[2,3]
>>> x3=[4,5]
>>> x=(x1,x2,x3)###创建元组x
>>> x
([1, 2], [2, 3], [4, 5])
>>> x1=["a","v"] ###x1相当于重新创建了列表,不会改变x
>>> x
([1, 2], [2, 3], [4, 5])
>>> x2.append(4) ###但是如果在原来x2的列表上增加一个元素,x也会增加,因为x引用了x2
>>> x
([1, 2], [2, 3, 4], [4, 5])
字符串创建可以用 单引号', 双引号" " ,三引号""" """
>>> s="this is an apple"
>>> s
'this is an apple'
>>> 'i'm sam' #####如果字符串里面用到单引号需要加转义符\,不然无法识别
File "<stdin>", line 1
'i'm sam'
^
SyntaxError: invalid syntax
>>> 'i\'m sam'
"i'm sam
###########\反斜扛不想被当作转义字符可以加个前缀r,或者加两个\\
>>> s=r'd:\dir\t.txt'
>>> pattern = '%s is %d years old.'
>>> pattern
'%s is %d years old.'
>>> name ='tom'
>>> age =12
>>> msg = pattern %(name,age) ###### 百分号加元组的类型可以作为一个格式字符串模式是(str % tuple)
>>> msg
'tom is 12 years old.'
>>> pattern = '%10s is %d years old.' #####占十个占位符
>>> msg =pattern %(name,age)
>>> msg
' tom is 12 years old.'
{:数据类型}.format() format括号里面是需要添加的参数
>>> msg='{:d}+{:d}={:d}'.format(1,12,1+12)
>>> msg
'1+12=13'
###冒号前面可以指定要显示第几个参数
>>> msg='{1:d}+{0:d}={2:d}'.format(1,12,1+12)
>>> msg
'12+1=13'
>>> name,age
('tom', 12)
>>> f'{name} is {age} years old'
'tom is 12 years old'
字符串对象是只读的,不能改变的,如果改变一定是创建了新的对象
>>> str.capitalize('trr') ####首字母大写
'Trr'
>>> s = 'hello, world!'
>>> print(s.title()) ##获得字符串每个单词首字母大写的拷贝
Hello, World!
>>> print(s.upper()) #### 获得字符串变大写后的拷贝
HELLO, WORLD!
>>> print(s.find('or')) # 从字符串中查找子串所在位置,如果差找不到则返回-1
8
>>> print(s.find('t'))
-1
>>> s
'hello, world!'
>>> s.replace('hello','hi')###替换字符串
'hi, world!'
>>> text='1,2,3,4,5'
>>> text
'1,2,3,4,5'
>>> text.split(",") ####分隔符分开,比如在读取文本的时候经常用到
['1', '2', '3', '4', '5']
>>> text[2]
'2'
>>> text[2]='c' ########字符串是只读的,不能重写,不能用取下标的方式进行修改
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> text[2:7] ###如果想要取下标的方式进行修改,可以将字符串修改为列表
'2,3,4'
>>> lt=list(text)
>>> lt
['1', ',', '2', ',', '3', ',', '4', ',', '5']
>>> lt[0]='a'
>>> lt
['a', ',', '2', ',', '3', ',', '4', ',', '5']
>>> s='' ####如果想将列表重新变回字符串,比较麻烦,可以先创建一个空的字符串,一次添加,可以用循环。
>>> s += lt[0]
>>> s
'a'
>>> s += lt[1]
>>> s
'a,'
>>> s += lt[2]
>>> s
'a,2'
if while for
score = int(input('hh input score: '))
if score >= 90 :
grade='A'
elif score>=80:
grade='B'
elif score>=70:
grade='C'
else:
grade='D'
print(f'your grade is {grade}.') ###f字符串
i=1
sum=0
while i<= 100:
sum +=i
i +=1
print(sum)
scores=[90,80,20,40,33,24,80]
for scores in scores:
if scores >= 60:
print("pass")
else:
print("nopass")
####################################
$ python python/python02/python02.py
pass
pass
nopass
nopass
nopass
nopass
pass
for i in range(10):
print(i)
$ python python/python02/python02.py
0
1
2
3
4
5
6
7
8
9
###########可以取出下标,那么对于一个列表,就可以用range用下表的方式取出全部的值
score=[10,20,34,84,92,100]
for i in range(len(score)):
print(score[i])
$ python python/python02/python02.py
10
20
34
84
92
100
########怎么既能得到下标,又能得到值
score=[10,20,34,84,92,100]
for score in enumerate(score):
print(score)
$ python python/python02/python02.py
(0, 10)
(1, 20)
(2, 34)
(3, 84)
(4, 92)
(5, 100)
############元组用值代替
>>> x=(1,2)
>>> idx,score=x
score=[10,20,34,84,92,100]
for idx,value in enumerate(score):
print(idx,value)
$ python python/python02/python02.py
0 10
1 20
2 34
3 84
4 92
5 100
#########想要输出两个列表的内容zip封装两个列表
score=[10,20,34,84,92,100]
names=['sam','tom','hhh','type','mary','lili']
for score,names in zip(score,names):
print(score,names)
$ python python/python02/python02.py
10 sam
20 tom
34 hhh
84 type
92 mary
100 lili