Python core programming-Chapter 8 exercise 8.15

Source: Internet
Author: User

#! /Usr/bin/python #-*-coding: UTF-8-*-#8-2. loop. write a program that allows the user to input three numbers: (f) rom, (t) o, and (I) ncrement. # Take I as the step and count from f to t, including f and t. for example, if the input is f = 2, # t = 26, I = 4, the program will output 2, 6, 10, 14, 18, 22, 26.f = int (raw_input ("Please input from:") t = int (raw_input ("Please input to:") I = int (raw_input ("Please input increment: ") print range (f, t + I, I)



#8-2. range (). If we need to generate the following lists, which parameters need to be provided in the range () # built-in functions? # (A) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#      (b) [3, 6, 9, 12, 15, 18]

#      (c) [-20, 200, 420, 640, 860]
>>> Range (10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range (3, 19, 3) [3, 6, 9, 12, 15, 18] >>> range (-20,861,220) [-20,200,420,640,860] >>>

#! /Usr/bin/python #-*-coding: UTF-8-*-#8-4. prime number. in this chapter, we have provided some code to determine the maximum approximate number of a number or whether it is a # prime number. convert the code to a function with a Boolean value. The function name is isprime (). # If the input is a prime number, True is returned; otherwise, False is returned. import mathdef isprime (number): num = int (math. sqrt (number) while num> 1: if number % num = 0: return False num-= 1 else: return Trueprint zip (range (1, 12 ), (isprime (x) for x in range (1, 12 )))


#! /Usr/bin/python #-*-coding: UTF-8-*-#8-5. approx. complete a function named getfactors. it accepts an integer as a parameter, # returns a list of all its dikes, including 1 and itself. def getfactors (number): factorslist = [number] num = number/2 while num> 0: if number % num = 0: factorslist. append (num) num-= 1 return factorslist







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.