Python语法速查1

Python 语法速查2

0、引言

本文仅作为学习python记录之用,如有错误,还望指出,谢谢。
主要参考书籍为《python从入门到实践》
编译器采用百度飞桨提供的在线编译平台及本地所搭建的Pycharm环境

第二篇:Python语法速查2

1、条件控制

在python的语法中,采用elif代替else if,所以条件控制的关键字为if、elif、else。
在这里插入图片描述
python使用”缩进“的方式替代原本的大括号,
条件语句的末尾,必须使用”:“,此外C/C++在条件语句中使用的小括号可以省略
与或非运算符改为关键词的方式进行,如下表
在这里插入图片描述

num = 5
if (num  >= 0 and num <= 10):
    print("1")
elif num % 2 != 0:
    print("2")
else: 
    print("3")

输出结果:1

在python的语法中,没有switch语句,进而使用if elif组合,达到相应的条件。

dice_value = 1
if dice_value == 1:
    print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 2:
    print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 3:
    print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 4:
    print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 5:
    print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 6:
    print('You rolled a {}. Great job!'.format(dice_value))
else:
    print('None of the conditions above (if elif) were evaluated as True')

输出结果:

You rolled a 1. Great job!

此外还可以使用运算符直接进行比较,输出的结果直接为True/False,建议在运算符左右各加一个空格,这样可以使得代码可读性提高。

num = 4
num > 0 and num  < 15

输出结果:True

参考资料:https://www.runoob.com/python3/python3-conditional-statements.html

2、循环语句

2.1、for循环

python中的for循环相较于C/C++,有较大区别,基本可以认为是遍历整个对象的数据.
在这里插入图片描述

b=[23, 41, 12, 16, 7]
for number in b: 
    print(number)
print('Hi')

输出结果:

23
41
12
16
7
Hi

可以搭配if和break实现对特定对象的查找,如下

sites = ["Baidu", "Google","Youtube","Taobao"]
for site in sites:
    if site == "Taobao":
        print("找到了特定对象!"+site)
        break
    print("循环数据 " + site)
else:
    print("没有循环数据!")
print("完成循环!")

输出结果:

循环数据 Baidu
循环数据 Google
循环数据 Youtube
找到了特定对象!Taobao
完成循环!

2.2、Range()函数用法

Range函数更像是C语言的for函数,默认初值为0,判断符号为<、默认步长为1
对比如下
for(i=0;i<4;i++)

range(4)
range(0,4)
range(0,4,1)
以上均为实现同一个功能

使用方法如下所示

for i in range(4):#输出0-3的数(4位,默认从0开始)
   print(i)
print('\n')#换行符
for j in range(0,4):#输出0-3的数字(索引位【))
    print(j)
print('\n')#换行符
for x in range(1,11,2):#以2为步进输出奇数
    print(x)

输出结果:

0
1
2
3


0
1
2
3


1
3
5
7
9

参考资料:https://www.runoob.com/python3/python3-loop.html

2.3、while循环

在这里插入图片描述
条件语句可以有括号,也可以不用括号,与if一样,记得加上冒号:

sum = 0
counter = 1
while (counter <= 100):
    sum = sum + counter
    counter += 1

print("sum:",sum)

输出结果:5050

while搭配else语句

count = 0
while count < 5:
   print (count, " 小于 5")
   count = count + 1
else:
   print (count, " 大于或等于 5")

输出结果

0  小于 5
1  小于 5
2  小于 5
3  小于 5
4  小于 5
5  大于或等于 5

while与break语句
与C语言用法相似

count = 0
while count <= 5:
    if count == 2:
        break
    count += 1
    print (count)

输出结果:
1
2

while true用法
while true 类似C语言中得while(1),只有使用break进行跳出循环

num = 0
while True:
    if num == 2:
        print('Found 2')
        break
    num += 1
    print (num)

输出结果:
1
2
Found 2

Continue
首先我们来看看while搭配break和continue的流程图,大概可以理解为break是跳出当前循环,而continue则可以理解为是忽略后续代码,回到循环起始语句。
在这里插入图片描述

举个栗子:

n = 5
while n 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值