Python条件语句与循环

简介: 1、判断与循环python 缩进main:print("Hello")print("Hello world.")if 判断条件: 执行语句elif 判断条件: 执行语句else: 执行语句while 判断条件: 执行语句a = 100whil...

1、判断与循环
python 缩进
main:
print("Hello")
print("Hello world.")

if 判断条件:
    执行语句
elif 判断条件:
    执行语句
else:
    执行语句
while 判断条件:
    执行语句
a = 100
while a>1:
    print(a)
    a-=1
    if a==50:
        break   # 退出循环
    if a==55:
        print("5555555555")
        continue   # 此次循环结束,进入下一个循环

break 跳出循环
continue 进入下一次循环

for item in sequence:
   执行语句
l = ["a","b","c","d","e","f"]
print(l[:])
print(l[0:5])        # 大于等于0     小于5  0 <= a > 5
print(l[0:-1])        # 大于等于0     小于5  0 <= a > 5
for x,y in enumerate(l):    # 打印列表中元素以及下标
    print(x,y)

2、编程思想最重要

编程语言最重要的是思想
ABCD乘以9=DCBA,求A=?,B=?,C=?,D=?

for A in range(1,10):
    for B in range(0,10):
        for C in range(0,10):
            for D in range(1,10):
                start = 1000*A+100*B+10*C+D
                end = 1000*D+100*C+10*B+A
                if start * 9 == end:
                    print("A={}".format(A))
                    print("B={}".format(B))
                    print("C={}".format(C))
                    print("D={}".format(D))
                    print("{0} * 9 = {1}".format(start,end))

返回结果:
A=1
B=0
C=8
D=9
1089 * 9 = 9801

3、求阶乘
求1-n的阶乘的和
1!+ 2!+ 3!+ 4!+5 !+ ··· + n!
0! = 1
1!= 1
2!= 1 2 = 2
3!= 1
2 * 3 = 6

def one(n):
    total = 1
    if n ==0:
        total = 1
    else:
        for i in range(1,n+1):
            total *= i
    return total
print(one(3))

status=1
while status:
    result = 0
    n= input("Please input a number(n>=0) : ")
    for i in n:
        if not i.isdigit():
            print("The number of you input is error.")
            exit(1)
    if int(n) < 0:
        print("The number of you input is error.")
        break
    for i in range(0,int(n)+1):
        result += one(i)
    print("0! + 1! + 2! + ··· ··· + n! = {}".format(result))
目录
相关文章
|
9月前
|
Python
|
9月前
|
程序员 Python
Python控制结构:条件语句和循环详解
【4月更文挑战第8天】本文介绍了Python的两种主要控制结构——条件语句和循环。条件语句包括`if`、`elif`和`else`,用于根据条件执行不同代码块。`if`检查条件,`else`提供替代路径,`elif`用于多个条件检查。循环结构有`for`和`while`,前者常用于遍历序列,后者在满足特定条件时持续执行。`for`可结合`range()`生成数字序列。`while`循环适用于未知循环次数的情况。循环控制语句`break`和`continue`能改变循环执行流程。理解和熟练运用这些控制结构是Python编程的基础。
113 4
|
4月前
|
编译器 数据安全/隐私保护 Python
Python 条件语句
【10月更文挑战第6天】
|
6月前
|
Python
3:Python条件语句
这段Python代码展示了if语句的三种使用方式:基础if-else结构、嵌套if语句以及if-elif-else链。基础结构根据条件执行或跳过代码块;嵌套结构允许在if语句中包含另一个if语句,实现更复杂的逻辑判断;if-elif-else链则用于检查多个条件,并在满足第一个真条件时执行相应代码块,若所有条件均不满足,则执行else中的代码。最后都会输出&#39;M6&#39;。
|
7月前
|
程序员 Python
Python条件语句
【7月更文挑战第23天】Python作为一种高级编程语言,以其简洁、易读的语法而闻名。条件语句和循环结构是Python中的两个基本编程概念,它们为程序员提供了在程序中进行逻辑判断和重复执行代码块的能力。本文将深入探讨Python中的条件语句和循环结构,并提供详细的代码实例来说明其用法。
70 1
|
9月前
|
存储 程序员 Python
Python for循环语句
Python for循环语句
111 1
|
9月前
|
Java 程序员 C++
Python教程第4章 | 条件语句、循环语句和函数
Python if条件语句,for循环语句、Python函数
143 1
Python教程第4章 | 条件语句、循环语句和函数
|
9月前
|
Python
Python系列(19)—— 条件语句
Python系列(19)—— 条件语句
|
9月前
|
程序员 Python
Python中的条件语句与循环结构
Python中的条件语句与循环结构
63 0
|
9月前
|
编译器 Python
[Python] 可莉深入讲解条件语句
[Python] 可莉深入讲解条件语句
77 0