Python Learning (7)

Source: Internet
Author: User

Exercise 7: Judging whether a number is prime

import mathdef is_prime(num):    if num==1:        return False    if num==2:        return True    else:        for i in range(2,int(math.sqrt(num))+1):            if num%i == 0:                return False        return Trueprint(is_prime(13))print(is_prime(12))print(is_prime(2))习题8:求100以内的素数和result = 0for i in range(100):    if is_prime(i):        result += iprint(result)

Exercise 9: Use the For method to find the sum of the odd numbers within 100

#encoding=utf-8import mathresult = 0for i in range(101):    if i%2 == 1:        result += iprint(result)

Exercise 10: The user enters multiple numbers, sums up when an even number is entered, enters odd, does not sum, enters. (one point) end sum, print sum result

sum = 0while True:    number = input("please input the number: ")    if number == ".":        break    else:        number = int(number)        if number%2 ==0:            sum += numberprint(sum)      

Exercise 11: Nested loop output 10-50 all digits with 1-5 in each digit:
Method 1: Number and 10, determine if greater than 0 and less than or equal to 5
Method 2: Convert the number to STR and take your characters to determine if the character is within 1-5.
Method 3: Stitching the digits

Method 1:

#encoding=utf-8import mathfor i in range(10,51):    if i%10 >=1 and i%10 <= 5:        print(i)

Method 2:

for i in range(10,51):    if str(i)[1] in "12345":        print(i)

Method 3:

for i in "1234":    for j in "12345":        print(int(i+j))

Exit the Double loop:

方式1:try--excepttry:    for i in range(5):        for j in range(5):            if i==3 and j ==3:                raiseexcept:    print(1)pass

Mode 2: Return of function implementation

def fun():    for i in range(5):        for j in range(5):            print(i,j)            if i==3 and j ==3:                return Truefun()

Mode 3: Multi-layer break

for i in range(5):    for j in range(5):        for k in range(5):            if i == j == k == 3:                break            else:                print (i, ‘----‘, j, ‘----‘, k)        else: continue        break    else: continue    break

Python Learning (7)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.