python的条件语句和循环

2022-04-25 08:39:35 浏览数 (1)

# 4.py

#code=utf-8

# python的条件语句和循环

# if 语句

age = 40

if age > 18:

print 'your age is ', age

print 'adult'

# if else 语句

age = 40

if age > 18:

print 'adult'

else:

print 'no adult'

# if elseif else 语句

age = 40

if age > 18:

print 'adult'

elif age > 6:

print 'teenage'

else:

print 'kid'

# 循环语句

# for in 循环

my = ['aaa', 'bbb', 'ccc', 'ddd']

for t in my:

print t

# while 循环

sum = 0

n = 100

while n > 0:

sum = sum n

n = n - 2

print sum

# range 生成的整数序列

tmp = range(5)

print tmp

'''

执行结果如下

your age is 40

adult

adult

adult

aaa

bbb

ccc

ddd

100

198

294

388

480

570

658

744

828

910

990

1068

1144

1218

1290

1360

1428

1494

1558

1620

1680

1738

1794

1848

1900

1950

1998

2044

2088

2130

2170

2208

2244

2278

2310

2340

2368

2394

2418

2440

2460

2478

2494

2508

2520

2530

2538

2544

2548

2550

[0, 1, 2, 3, 4]

'''

0 人点赞