Python small instance (1), python instance

Source: Internet
Author: User

Python small instance (1), python instance

1.

Question: There are four numbers: 1, 2, 3, and 4. How many three numbers can be composed of different and non-repeated numbers? What is each?

1 for i in range(1,5):2     for j in range(1,5):3         for k in range(1,5):4             if(i!=k)and(i!=j)and(j!=k):5                 print (i,j,k)

Function prototype: range (start, end, scan ):

Parameter description: start: the count starts from start. The default value is from 0. For example, range (5) is equivalent to range (0, 5 );

End: The end of the technology, but does not include end. For example, range (0, 5) is [0, 1, 2, 3, 4] No 5

Scan: interval of each hop. The default value is 1. For example, range (0, 5) is equivalent to range (0, 5, 1)

 

1 for I in range (5): 2 print (I) # Here I is from 0 to 4, the value of the following I + = 2 assignment statement is not changed. 3 I + = 2 4 print (I) 5 print ("end ")

Running result
Python test01.py
0
2
End
1
3
End
2
4
End
3
5
End
4
6
End

 

1 i=02 while i<5:3     print(i)4     i+=25     print(i)6     print("end")

Running result
Python test01.py
0
2
End
2
4
End
4
6
End

 

2.

Circular area

1 def area_circle(r):2     PI=3.143     return PI*r**24 5 print(area_circle(4))6 print(area_circle(7))7 print(area_circle(9))

 

3.

Calculate 1 to 100, (n ^ 2 + 1) n from 1 to 100

 1 def fun1(i): 2     return i 3  4 def fun2(i): 5     return i**2+1 6  7 def funt(start,end,fun): 8     sum=0 9     for i in range(start,end+1):10         sum+=fun(i)11     return sum12 13 print(funt(1,100,fun1))14 print(funt(1,100,fun2))

 

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.