Exercise for loop

# 1. write a loop to print the following
# 10
# 20
# 30
# 40
# 50
# 60

for i in range(1, 7):
    print(i * 10)

# 2. write a loop to print the following.
#    [Tips: you need to use google to search]
#
# 2 4 6 8 10
a = ""
for i in range(1, 6):
    a += str(i * 2)
    a += " "
    if i == 5:
        print(a[:-1])

# 3. [difficult] print the following. require nested for loop

# 1 2 3 4 5 6 7 8 9
# 2 4 6 8 10 12 14 16 18
# 3 6 9 12 15 18 21 24 27
# 4 8 12 16 20 24 28 32 36
# 5 10 15 20 25 30 35 40 45
# 6 12 18 24 30 36 42 48 54
# 7 14 21 28 35 42 49 56 63
# 8 16 24 32 40 48 56 64 72
# 9 18 27 36 45 54 63 72 81
a = ""
for i in range(1, 10):
    a = ""
    for ii in range(1, 10):
        a += str(i * ii)
        a += " "
        if ii == 9:
            print(a[:-1])


# 4. Write a Python program to count the number of even and odd numbers from a series of numbers. Go to the editor
# Sample numbers : numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
# Expected Output :
# Number of even numbers : 5
# Number of odd numbers : 4
def count_even_odd(numbers):
    even = 0
    odd = 0
    for i in numbers:
        if i % 2 == 0:
            even += 1
        elif i % 2 == 1:
            odd += 1
    print("Number of even numbers : {}".format(even))
    print("Number of odd numbers : {}".format(odd))

# numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
# count_even_odd(numbers)


# 5. Write a Python program that prints all the numbers from 0 to 20 except 3 and 6.
# Note : Use 'continue' statement.
# Expected Output : 0 1 2 4 5 7 8 9 10 .......
a = ""
for i in range(21):
    if i == 3 or i == 6:
        continue
    a += str(i)
    a += " "
    if i == 20:
        print(a[:-1])
# 6. Write a Python program to get the Fibonacci series between 0 to 50. Go to the editor
# Note : The Fibonacci Sequence is the series of numbers :
# 0, 1, 1, 2, 3, 5, 8, 13, 21, ....
# Every next number is found by adding up the two numbers before it.
# Expected Output : 1 1 2 3 5 8 13 21 34
a = ""
b = [0, 1]
num = range(51)
for i in num:
    a += str(b[-1])
    a += " "
    b.append(b[i] + b[i+1])
    if (b[-1]) > 51:
        print(a)
        break

# 7. Write a Python program which iterates the integers from 1 to 50. For multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
# Sample Output :
# fizzbuzz
# 1
# 2
# fizz
# 4
# buzz
for i in range(1,51):
    if i % 15 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)


#*****#

这就是全部
That is all

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值