Home > Backend Development > Python Tutorial > Python conditional judgment and looping

Python conditional judgment and looping

高洛峰
Release: 2017-02-17 11:17:09
Original
1437 people have browsed it


Python’s conditional judgment and looping

1. Python’s if statement

score = 75
if score >= 60:
    print 'passed'
Copy after login

2. Python’s if-else

score = 55
if score >= 60:
    print 'passed'
else:
    print 'failed'
Copy after login

3. Python’s if-elif-else

score = 85
if score >= 90:
    print 'excellent'
elif score >= 80:
    print 'good'
elif score >= 60:
    print 'passed'
else:
    print 'failed'
Copy after login

4. Python’s for loop

L = [75, 92, 59, 68]
sum = 0.0
for x in L:
    sum = sum + x
print sum / 4
Copy after login

5. Python’s while loop

sum = 0
x = 1
while x < 100:
    sum = sum + x
    x = x + 2
print sum
Copy after login

6. Python’s break to exit the loop

sum = 0
x = 1
n = 1
while True:
    if n > 20:
        break
    sum = sum + x
    x = x * 2
    n = n + 1
print sum
Copy after login

7. Python's continue continues to loop

sum = 0
x = 0
while True:
    x = x + 1
    if x > 100:
        break
    if x % 2 == 0:
        continue
    sum = sum + x
print sum
Copy after login

8. Python's continue continues to loop

for x in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
    for y in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
        if x < y:
            print x * 10 + y
Copy after login

For more Python's conditional judgment and loop related articles, please pay attention to PHP Chinese website !



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template