Python笔记3 分支、循环、条件与枚举

表达式是运算符和操作数所构成的序列

运算符的优先级问题:逻辑运算符中 and 优先级高于 2

>>> a or b and c
1
>>> (a or b) and c
3
>>> a or (b and c)
1

运算符优先级:按照序号从小到大,优先级依次递减

逻辑运算符中:not > and > or

算术运算符 > 赋值运算符 > 逻辑运算符

>>> not a or b + 2 == c
False
>>> (not a) or ((b + 2) == c)
False

到此为止,Python 的基本类型与基本概念已经结束了,接下来开始在文件中写Python代码

点击Python IDLE 右上角 File -> New File, 编写代码

a = 1
b = 2
c = 3
print('hello')

保存名称为 hello.py, 存在 D:/test_python/

打开windows 的命令框,运行

C:\Users\victor>D:

D:\>cd test_python

D:\test_python>python hello.py
hello

IDE : Integrated Development Environment 集成开发环境

下载安装 vscode

快捷键: ctrl + ~ 打开终端  再次ctrl + ~ 可以关闭

右击文件名,在终端中打开,python hello3.py 可以执行文件

但是这种方式和我们之前在 Windows 终端中使用的方法差不多,我们使用如下方式:

下载 vscode 的扩展包 python

#注释

'''
多行注释
'''

编写第一个Python代码

'''
一个小程序:验证用户名与密码
'''

account = 'julia'
passwd = '123'

print('please input account:')
user_account = input()

print('please input passwd:')
user_passwd = input()

if account == user_account and passwd == user_passwd:
    print('success')
else:
    print('failed')

snippet 片段:输入 if, 可以补全后面的 else 语句,Tab 键可以切换到下一个代码编写区域,不用删除。我们之后用的 for class def 都有snippet

完成注释:首先用鼠标选中多行,然后先按Ctrl+k,再按下Ctrl+c

取消注释:首先用鼠标选中多行,然后先按Ctrl+k,再按下Ctrl+u 

python3 中 input() 函数:读入的是字符串类型

a = input()
print(type(a))

#1
#<class 'str'>

循环语句

while语句:用于递归、循环

while...else 语句,在while结束之前执行else部分

counter = 1

while counter <= 3:
    counter += 1
    print(counter)
else:
    print('EOF')

结果:

PS D:\python_learning> python .\c3.py
2
3
4
EOF

for语句:用于遍历、循环,序列、集合、字典

for双层循环,print 输出在一行

a = [['apple', 'banana'], (1,2,3)]
for x in a:
    for y in x:
        print(y, end = ' ')
else:    #遍历完序列之后执行
    print('fruit is gone')

结果:

PS D:\python_learning> python .\c3.py
apple banana 1 2 3 fruit is gone

函数range(0, 10, 2):第一个参数开始位置,第二个结束位置的下一个,步长

>>> for x in range(10):
	print(x, end = ' ')

	
0 1 2 3 4 5 6 7 8 9 
>>> for x in range(0,10,2):
	print(x, end = ' ')

	
0 2 4 6 8             #递增
>>> for x in range(10, 0, -2):
	print(x, end = ' ')

	
10 8 6 4 2            #递减

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值