美文网首页
python 判断与循环

python 判断与循环

作者: lflish | 来源:发表于2018-08-21 22:35 被阅读0次

条件判断

一般格式

if <test1>:           # if test    
   <statements1>      # Associated block
elif <test2>:         # Optional elifs    
    <statements2>
else:                 # Optional else    
    <statements3>

if/else三元表达式

    A = Y if X else Z

例子

#一般格式
>>> if choice == 'spam':
...     print(1.25)
... elif choice == 'ham':
...     print(1.99)
... elif choice == 'eggs':
...     print(0.99)
... elif choice == 'bacon':
...     print(1.10)
... else:
...     print('Bad choice')

#三元表达式
>>> A = 't' if '' else 'f'
>>> A
'f'

循环

while 一般格式

while <test>:           # Loop test    
    <statements1>       # Loop body
else:                   # Optional else   
    <statements2>       # Run if didn't exit loop with break
  • break跳出最近所在的循环(跳过整个循环语句)。

  • continue跳到最近所在循环的开头处(来到循环的首行)。

  • pass什么事也不做,只是空占位语句。
    版本差异提示:Python 3.0(而不是Python 2.6)允许在可以使用表达式的任何地方使用...(三个连续的点号)来省略代码。

  • 循环else块只有当循环正常离开时才会执行(也就是没有碰到break语句)。

while 一般格式

while <test1>:   
    <statements1>   
    if <test2>: break     # Exit loop now, skip else   
    if <test3>: continue  # Go to top of loop now, to test1
else:   
    <statements2>         # Run if we didn't hit a 'break'

while 例子

while x:                   # Exit when x empty    
    if match(x[0]):        
        print('Ni')        
        break              # Exit, go around else    
    x = x[1:]
else:    
    print('Not found')     # Only here if exhausted x

for 一般格式

for <target> in <object>:      # Assign object items to target    
    <statements>    
    if <test>: break           # Exit loop now, skip else    
    if <test>: continue        # Go to top of loop now
else:    
    <statements>               # If we didn't hit a 'break'

for 例子

#列表
>>> for x in ["spam", "eggs", "ham"]:
...     print(x, end=' ')
...spam eggs ham

#字符串
S = "lumberjack"
>>> for x in S: print(x, end=' ')   # Iterate over a string...l u m b e r j a c k

#元组
>>> T = [(1, 2), (3, 4), (5, 6)]
>>> for (a, b) in T:                   # Tuple assignment at work
...     print(a, b)
...1 2
   3 4
   5 6

#字典
>>> D = {'a': 1, 'b': 2, 'c': 3}
>>> for key in D:
...    print(key, '=>', D[key])          # Use dict keys iterator and index
...a => 1
   c => 3
   b => 2

#items返回可遍历的(键, 值) 元组数组,所以字典也可以这样遍历。
>>> for (key, value) in D.items():
...    print(key, '=>', value)            # Iterate over both keys and values
...a => 1
   c => 3
   b => 2
   
#分片循环   
>>> S = 'abcdefghijk'
>>> for c in S[::2]: 
    print(c, end=' ')
...a c e g i k

# python3 扩展
>>> for (a, *b, c) in [(1, 2, 3, 4), (5, 6, 7, 8)]:
...    print(a, b, c)
...1 [2, 3] 4
   5 [6, 7] 8

range 迭代器

range是一个迭代器,会根据需要产生元素

>>> list(range(-5, 5))
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]

>>> list(range(0, len(S), 2))
[0, 2, 4, 6, 8, 10]

>>> for i in range(3):
...     print(i, 'Pythons')
...0 Pythons
   1 Pythons
   2 Pythons
 
# 修改列表
>>> L = [1, 2, 3, 4, 5]
>>> for i in range(len(L)):     # Add one to each item in L
...     L[i] += 1               # Or L[i] = L[i] + 1
...
>>> L
[2, 3, 4, 5, 6]
   

zip 并行迭代器

>>> L1 = [1,2,3,4]
>>> L2 = [5,6,7,8]

>>> zip(L1, L2)
<zip object at 0x026523C8>

>>> list(zip(L1, L2))             # list() required in 3.0, not 2.6
[(1, 5), (2, 6), (3, 7), (4, 8)]

>>> for (x, y) in zip(L1, L2):
...     print(x, y, '--', x+y)
...1 5 -- 6
   2 6 -- 8
   3 7 -- 10
   4 8 -- 12

通过zip构建字典

# python2.2 before
>>> keys = ['spam', 'eggs', 'toast']
>>> vals = [1, 3, 5]
>>> list(zip(keys, vals))[('spam', 1), ('eggs', 3), ('toast', 5)]
>>> D2 = {}
>>> for (k, v) in zip(keys, vals): D2[k] = v
...

>>> D2
{'toast': 5, 'eggs': 3, 'spam': 1}

#python 2.2 later
>>> keys = ['spam', 'eggs', 'toast']
>>> vals = [1, 3, 5]
>>> D3 = dict(zip(keys, vals))

>>> D3
{'toast': 5, 'eggs': 3, 'spam': 1}

enumerate函数

enumerate函数返回一个生成器,对象简而言之,这个对象有一个next方法,由下一个内置函数调用它,并且循环中每次迭代的时候它会返回一个(index,value)的元组。

>>> S = 'spam'
>>> for (offset, item) in enumerate(S):
...     print(item, 'appears at offset', offset)
...s appears at offset 0
   p appears at offset 1
   a appears at offset 2
   m appears at offset 3

相关文章

  • Python练习——判断和循环

    Python 基础总结 (判断和循环) 条件判断 循环结构

  • python 判断与循环

    条件判断 一般格式 if/else三元表达式 例子 循环 while 一般格式 break跳出最近所在的循环(跳过...

  • Python判断与循环

    一、判断 if语句的完整形式就是: 注意: 冒号 缩进为4个空格,不是普通的Tab 简写:只要x是非零数值、非空字...

  • python判断与循环

    一、判断语句   python中的if判断语句与其他语言中的用法相似,可以实现多个条件的判断,if语句的嵌套等功能...

  • python与shell语法

    python与shell之间的语法联系: 变量 数组的定义 注释 逻辑判断 IF语法 for循环 while循环 ...

  • Python条件判断 与 循环

  • python的判断与循环

    1. if 语句 python是一门对格式严格要求的语言,在 if 条件判断语句中: if 后有一个空格; 判断的...

  • python基础●判断与循环

    判断 if --else 语句 回归对判断的理解比如简单判断:判断 1 是否等于1,如果等于1 ,那就返回 1 =...

  • 第四章 操作列表与for循环

    4.1 for循环语句 4.2关于python的缩进认知: Python根据缩进判断代码行之间的关系.这点与jav...

  • python循环判断

    1、if 判断语句 if 判断条件1: 执行语句1elif 判断条件2: 执行语句2else: 执行语句 3 ...

网友评论

      本文标题:python 判断与循环

      本文链接:https://www.haomeiwen.com/subject/appsiftx.html