Python基础01 变量命名、注释、三种流程结构

一、变量命名规则

1、字母、数字、下划线组成,且不能是数字开头。
2、Python关键字,也不能使用 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield
3、Python的内置函数或类最好也不要用。

二、注释

1、单行注释‘	#
2、多行注释	三引号(单双引号都可以)
# print('Hello World1')

"""
print('Hello World2')
print('Hello World2')
"""

'''
print('Hello World3')
print('Hello World3')
print('Hello World3')
'''

三、三种流程结构

1、顺序结构

从上到下顺序执行。

2、条件判断结构

(1)if-else

#没else分支
if True:
    print('1')

#有else分支
if False:
    print('2')
else:
    print('3')

#支持嵌套
if True:
    if True:
        print('4')
    else:
        print('5')
else:
    print('6')

(2)if elif

s = input('>>>')

if s == '1':
    print('I am 1')
elif s == '2':
    print('I am 2')
elif s == '3':
    print('I am 3')
else:
    print('others')

print('end')

补充,空白分支要写pass,不然会报错。
(3)and or not
and 且
or 或
not 非

if 1==1 and 1==2:
    print('first')
elif 1==1 or 1==2:
    print('secend')

if not 1==2:
    print('ok')

3、循环结构

(1)while语句
#不带else
l = 0
while l < 4:
    print(l)
    l +=1

#带else
l=10
while l<4:
    l += 1
else:
    print(l)
(2)for语句
for 变量 in 可迭代对象:
for item in range(0,10,3):
    print(item)
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值