一、打开已创建项目
进入InsCode,打开工作台,在IDE中编辑。
二、运行解释器
首先打开终端,然后调整一下终端界面,查看python的版本,输入
python -V
会显示目前已安装的python版本号,可以看到版本为3.9.16。然后通过python3.9
就可以运行解释器。
按照提示,使用quit()
函数或者快捷键Ctrl D
组合退出运行。
输入’python -v’会输出输出很多关于模块导入的详细信息,这对于调试模块导入问题或理解Python的内部导入机制非常有用。
三、使用解释器
3.1当作计算器
>>> 2+2 #加法
4
>>> 50+5*6
80
>>> (50+5)*6
330
>>> 5/6 #除法
0.8333333333333334
>>> 12/6 #总是返回浮点数
2.0
>>> 12.6/6
2.1
>>> 12.6//6 #使用//进行取整运算
2.0
>>> 12.6%6 #使用%进行取余运算
0.5999999999999996
>>> 18.3 % 3
0.3000000000000007
>>> 5 ** 2 #计算a的b次方,使用**
25
>>> 5 ** 4
625
>>> 5 ** -1
0.2
>>> 5 ** -2
0.04
加入变量定义:
>>> tax = 12.5 / 100 #使用等号赋值,赋值后,下一个交互提示符的位置不显示任何结果
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _ #交互模式下,上次输出的表达式会赋给内置变量 _
113.0625
>>> round(_, 2)
113.06
>>> _ #最好当作只读类型。显式赋值会创建同名局部变量,该变量会屏蔽内置变量。
113.06
>>> price
100.5
3.2操作文本
>>> 'doesn\'t' #使用转义符
"doesn't"
>>> "doesn't" #或者使用双引号
"doesn't"
>>> "\"Yes,\" they said."#使用双引号标示字符串,内嵌双引号时,需要转义,单引号同理
'"Yes," they said.'
>>> '"Yes," they said.' #不同符号内嵌时不必转义
'"Yes," they said.'
加入变量定义
>>> s = 'First line.\nSecond line.'
>>> s #原样输出
'First line.\nSecond line.'
>>> print(s) #转义输出
First line.
Second line.
换行输出
>>> print("""\ #输入"""...""" 或 '''...'''表示多行输入
... aaa
... bbb
... """)
aaa
bbb
>>> print('''...
... ...
... qqq
... ''')
...
...
qqq
>>> print('''
... ddd''')
ddd
拼接合并和切片
>>> 3 * 'un' + 'ium' #重复三次'un'然后拼接'ium'
'unununium'
>>> 'Py' 'thon' #相邻多个字符串自动合并
'Python'
>>> text = ('Put several strings within parentheses ' #定义时换行,输出一行
... 'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'
>>> prefix = 'Py'
>>> prefix + 'thon' #变量和字符串使用'+'拼接
'Python'
>>> _
'Python'
>>> word =_
>>> word[5] #字符串支持索引
'n'
>>> word[-1] #支持负数索引
'n'
>>> word[0:2] #切片
'Py'
>>> word[:2] #首位空缺从0开始
'Py'
>>> word[-2:] #末位空缺默认到结尾
'on'
>>> word[:2] + word[2:]#输出结果包含切片开始不包含切片结束。s[:i] + s[i:] 总是等于 s
'Python'
索引对应关系:
0 | P | 1 | y | 2 | t | 3 | h | 4 | o | 5 | n | 6 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
-6 | -5 | -4 | -3 | -2 | -1 |
>>> word[42] #单独使用索引,越界报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> word[4:42] #切片时自动处理索引
'on'
>>> word[2:] = 'py' #不能通过索引对字符串修改
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> 'J' + word[1:] #新建字符串可以拼接索引
'Jython'
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s) #计算字符串长度
34
3.3列表
>>> squares = [1, 4, 9, 16, 25]
>>> squares[-3:] #列表也有索引
[9, 16, 25]
>>> squares + [36, 49, 64, 81, 100] #支持拼接
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[2]=1 #可以通过索引赋值
>>> squares
[1, 4, 1, 16, 25]
>>> squares.append(36) #末尾添加,使用append函数
>>> squares
[1, 4, 1, 16, 25, 36]
>>> rgb = ["Red", "Green", "Blue"]
>>> rgba = rgb #将一个列表赋值给一个变量时,该变量将引用 现有的列表
>>> rgba
['Red', 'Green', 'Blue']
>>> rgba.append("Alph") #rgba添加数据时
>>> rgba
['Red', 'Green', 'Blue', 'Alph']
>>> rgb #rgb也能看到修改
['Red', 'Green', 'Blue', 'Alph']
>>> id(rgb) == id(rgba)
True
>>> correct_rgba = rgba[:] #切片操作返回包含请求元素的新列表,是浅拷贝
>>> correct_rgba[-1] = "Beta"
>>> correct_rgba
['Red', 'Green', 'Blue', 'Beta']
>>> rgba
['Red', 'Green', 'Blue', 'Alph']
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:5] = ['C', 'D', 'E'] #通过替换动态修改值
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> letters[2:5] = [] #移除
>>> letters
['a', 'b', 'f', 'g']
>>> letters[:] = [] #清空
>>> letters
[]
>>> len(letters) #取长
0
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n] #列表可嵌套
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'