Python Learning (v) Function--Example practice

Source: Internet
Author: User

Python Function Example Exercises

The previous functions, data structure, process control, and so on, do the following examples of practice:

1. Define the Is_even function, pass an int argument, determine if it is even, return True; no, return False

1 " " Is_even " " 2 def Is_even (i): 3     if (i%2==0): 4         return True 5     Else : 6         return False 7 Print (Is_even (0), Is_even (1), Is_even (1))
Is_even

2. Define the Is_int function, pass a float parameter, determine whether it is an integer (such as 1.0 is an integer), return True; no, return False

1 " " Is_int " " 2 def Is_int (f): 3     if (f%1==0): 4         return True 5     Else : 6         return False 7 Print (Is_int (0), Is_int (1.0), Is_int (1.1))
Is_int '

3. Define the Digital_sum function, pass an int parameter, return the sum of the numbers of all its digits, for example: 1234 returns 10 (1+2+3+4), 900 returns 9 (9+0+0)

1 " "Digital_sum" "2 defdigital_sum (i):3sum =04      while(i/10>1):5Sum + = i%106i = Int (I/10)7Sum + =I8     returnsum9 Print(Digital_sum (23423), Digital_sum (), digital_sum (0))
Digital_sum 1
1 def digital_sum (i): 2     i = Str (i)3     sum = 04for in       i:5          sum + = int (d)6     return  sum7Print (Digital_sum (23423), Digital_sum (), digital_sum (0))
Digital_sum 2

4. Define the factorial function, pass an int parameter, return the product of all its digit numbers multiplied, for example: 1234 returns 24 (1*2*3*4), 909 returns 0 (9*0*9)

1 " "factorial" "2 deffactorial (i):3result = 14      while(i/10>1):5Result *= i%106i = Int (I/10)7Result *=I8     returnresult9 Print(Factorial (1234), factorial (909), factorial (0))
Factorial 1
 1   "   2  def   factorial (i):  3  i = STR (i)  4  result = 15  for  D  I:  6  result *= int (d)  7  return   result  8  print  (factorial (1234), factorial (909), factorial (0)) 
Factorial 2

5. Define the Is_prime function, pass an int parameter, determine whether it is a prime number (can only be divisible by 1 or itself), if, return true; no, return False; For example: 1 returns TRUE;2 returns TRUE;4 returns True

Rule 0 is not prime, return False

1 " "Is_prime" "2 defIs_prime (i):3result =True4      forDinchRange (2, i):5         if(i%d==0):6result =False7              Break8     if(i==0):9result =FalseTen     returnresult One Print(Is_prime (1), Is_prime (2), Is_prime (3), Is_prime (4), is_prime (+), Is_prime (197), Is_prime (201), Is_prime (0))
Is_prime

Python Learning (v) Function--Example practice

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.