[Xiaobai series] Python condition judgment, loop and simple iteration from entry to do not give up, come take a look

1. Conditional judgment if

if Grammar

if 条件 :
    # some code
else:
    # other code

Note: colon indentation execution order

num1, num2 = 3 , 5
if num1 > num2:
    print('第一个数是大于第二个数的')
else:
    print('第一个数小于或等于第二个数')
第一个数小于或等于第二个数

Example: Determine whether it is a leap year

year = int(input("输入一个年份: ")) 
if (year % 4) == 0:
    if (year % 100) == 0:
        if (year % 400) == 0:
            print("{0} 是闰年".format(year))   # 整百年能被400整除的是闰年
        else:
            print("{0} 不是闰年".format(year))
    else:
        print("{0} 是闰年".format(year))       # 非整百年能被4整除的为闰年
else:
    print("{0} 不是闰年".format(year))
输入一个年份:  1990


1990 不是闰年


请输入一个年份: 1990


1990不是闰年
# 简化版的代码
year = int(input("请输入一个年份:"))
if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0:
    print("{0}是闰年".format(year))
else:
    print("{0}不是闰年".format(year))
请输入一个年份: 1990


1990不是闰年

2. Cycle

2.1for

grammar:

for 变量 in 列表 :
    pass          
ls_3
[1, 3, 12.3, 'apple', 0.001]
for i in ls_3:
    print (i)
1
3
12.3
apple
0.001

2.2 while

grammar

When the type of cycle, according to the condition of the cycle, when what is what, what happens .

while 条件 :
    pass # 一般在这里进行某些操作,然后修改条件
i = 0
while i<5 :
    print(i)
    i+=1 # 修改条件
0
1
2
3
4

3. Iteration

3.1 Iteration-range

rangeThe full parameters of the method:

range(start, end, step = 1)

If only two parameters are given and step is omitted, the default value of step is 1.

for i in range(1,10,2):
    print(i)
1
3
5
7
9
list(range(1,10,2))
[1, 3, 5, 7, 9]

3.2 Iteration-iter

In addition to iterating through indexes, serialized objects can also be iter()implemented using iterators .
Simply put, it means that there is a nextway to take out each element in the sequence one by one.

ourlist = list(range(10))
it = iter(ourlist)  # 使用列表ourlist生产一个迭代器
print(ourlist)
print(next(it))
print(next(it))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0
1
next(it)
2
mylist = [123, 'xyz', 45.67]
# 对序列对象生成一个迭代器对象
i = iter(mylist)
# 开始迭代得到元素
next(i)
123
next(i)
'xyz'
next(i)
45.67
next(i)
---------------------------------------------------------------------------

StopIteration                             Traceback (most recent call last)

<ipython-input-111-a883b34d6d8a> in <module>
----> 1 next(i)


StopIteration: 

3.3 Iteration –zip

It means compression, and its syntax is as follows:

zip([iterable, ...]) # iterable 可迭代的 也就是序列对象 比如list tuple 等等

The direct meaning is to take an element from each of these objects in turn to make a tuple. It returns a tuple

names = ['李雷', '韩梅梅']
ages = [25, 24]

# zip(names, ages)
# list(zip(names, ages))
i = iter(zip(names, ages))
next(i)
('李雷', 25)
dict(zip(names, ages)) # 这是非常常用的小技巧
{'李雷': 25, '韩梅梅': 24}

* Operator cooperates with the zip function to achieve the opposite function of zip, that is, to split the merged sequence into multiple tuples

Published 42 original articles · praised 28 · visits 4961

Guess you like

Origin blog.csdn.net/KaelCui/article/details/105421728