表达式Expression是运算符Operator和操作数Operand所构成的序列
>>> c = int('1') + 2
>>> a = 1
>>> b = 2
>>> c = 3
>>> a + b*c
7
>>> a or b and c
1
>>> a or (b and c)
1
>>> //and优先级高于or
>>> a = 1
>>> b = 2
>>> c = 3
>>> a or b and c //一般是从左向右的顺序来计算,即左结合
1
>>> (a or b )and c
3
>>> (a or b )and (c+1)
4
>>>
>>> a = 1
>>> b = 2
>>> c = a or b
>>> print(c)
1
>>> //对于赋值运算符,运算顺序是右结合
>>> a = 1
>>> b = 2
>>> c = 2
>>> not a or b + 2 == c
False
>>> (not a) or (b + 2 )== c
False
>>> (not a) or ((b + 2 )== c)
False
>>> // not > and > or
pycharm,vscode,sumlime 都是ide,属于集成开发环境
https://marketplace.visualstudio.com/vscode
下载插件
下载
官网下载:https://code.visualstudio.com/
汉化中文(官方下载默认为英文,英文好的小伙伴可直接跳过这步)
点击插件按钮搜索 Chinese, 在弹出的选项中选择第一个中文简体
然后右边会弹出如下图安装界面,接着点击 Install 安装
安装完毕后会有如下提示(主要提醒你安装完中文简体汉化包后一定要重启方可生效)
直接在官网上下载,千万不要在别的什么乱七八糟的网站上下载,导致下载一堆软件
修改字体:文件-首选项-设置
Editor:front-size
ctrl + 波浪线(1旁边的键)
在插件中搜索python
附属字有Linting, Debugging (multi-threaded, remote), Intellisense, Jupyter Notebooks, code formatting, refactoring, unit tests, snippets, and more.
利用这个插件进行智能感知,断点调试
下载插件vscode-icon
程序带有图标
一般默认形成txt文件,所以保存时必须修改类型为py文件
并保存在D盘下的python文件夹下面
点击文件-在终端中打开,为之后的调试作准备
print('hello python')
在调试框中,输入python 和一个字母U 再按tab键,就可以自动补全
点击打开文件夹,在左边框里可以得到之前建立的python文件夹,这样我们就可以在这个python文件夹下建立py文件
基本语法:每句句末不需要加分号
python依靠缩进区分代码块
alt + shift + a 可以多行注释
shift + / 可以单行注释
必须进行保存才可以进行调试,它是不会同时保存并运行的
ctrl + s 可以自动保存
c1.py的代码如下
""" print('hello python') """
# 多行注释
# print('hello')
# 单行注释
mood = False
if mood :
print('go to left')
print('back away')
else :
print('go to right')
重命名为c1.py
ctrl + c 然后 ctrl +v 进行粘贴出一个c2.py
c2.py 如下
a = 1
b = 2
c = 2
d = []
if a or b + 1 == c :
print('go to left')
# print('back away')
else :
print('go to right')
if d :
print('go to left')
# print('back away')
else :
print('go to right')
if else 经典用法是判断用户的密码是否对应
c3.py如下
account = 'qiyue'
password = '123456'
print('please input account')
user_account = input() # imput函数将接受到的用户账号赋值给变量
print('please input password')
user_password = input()在这里插入代码片
运行结果如下
PS D:\python> python .\c3.py
please input account
111
please input password
222
PS D:\python>
如此,我们用户账号为111,密码为222
下面模拟用户账号与密码的对应
account = 'qiyue'
password = '123456'
print('please input account')
user_account = input() # imput函数将接受到的用户账号赋值给变量
print('please input password')
user_password = input()
if account == user_account and password == user_password :
print('success')
else :
print('fail')在这里插入代码片
运行结果为:
PS D:\python> python .\c3.py
please input account
111
please input password
222
fail
PS D:\python> python .\c3.py
please input account
qiyue
please input password
123456
success
每一个python文件都是一个模块,如果提示你Missing Module docs,即缺失了模块文件,需要在文件开头附上一段注释文档用以解释说明本python文件的用途
可以通过pylint来修改文件格式,但是我的pylint好像装不上,所以我就没有调整格式
""" 一段小程序 """
account = 'qiyue'
password = '123456'
print('please input account')
user_account = input() # imput函数将接受到的用户账号赋值给变量
print('please input password')
user_password = input()
if account == user_account and password == user_password :
print('success')
else :
print('fail')
左边框Python文件夹下新建文件,c4.py
IDE内一个小技巧:Snippet,快速构建代码片段,例如打出if ,会跳出一些代码来让我们选择,直接按tab键,我们可以直接取代跳出的代码
# Snippet 片段
""" if code:
pass
else:
pass """
for target_list in expression_list:
pass
if condition:
pass
else:
pass #pass 称为占位语句,保证语法
if condition: # 代码嵌套
if condition:
pass
else:
pass
else:
f condition:
pass
else:
pass
新建c5.py,发现预期效果与实际效果不一致
a = input()
print('a is ' + a) # 打印出a 为多少
if a == 1 :
print('apple')
else :
if a == 2 :
print('orange')
else :
if a == 3 :
print('banana')
else :
print('shopping')
实际效果:
PS D:\python> python .\c5.py
1
a is 1
shopping
简化上述代码,c6.py
a = input()
print('a is ' + a) # 打印出a 为多少
if a == 1 :
print('apple')
elif a == 2 :
print('orange')
elif a == 3 :
print('banana')
else :
print('shopping')
c5.py出现问题在于,输入1时,实际输入了一个字符串,
a = input()
print (type(a))
print('a is ' + a) # 打印出a 为多少
if a == 1 :
print('apple')
else :
if a == 2 :
print('orange')
else :
if a == 3 :
print('banana')
else :
print('shopping')
实际结果
PS D:\python> python .\c5.py
1
<class 'str'>
a is 1
shopping
我们考虑转换类型来解决这个问题
所以我们知道,当你输入某值时,计算机将其视为字符串
a = input()
print (type(a))
print('a is ' + a) # 打印出a 为多少
a = int(a)
if a == 1 :
print('apple')
else :
if a == 2 :
print('orange')
else :
if a == 3 :
print('banana')
else :
print('shopping')
结果
PS D:\python> python .\c5.py
1
<class 'str'>
a is 1
apple
这个错误没有错误提示,使得我们认为代码没有错误,所以这类动态语言也有缺陷,
思考题:c7.py
a = 1
b = 0
if a == 1:
print('a is True')
elif b == 1:
print('b is True')
else :
print('Null')
涉及的python文件打包放在文末