Example analysis of random module usage in python

Source: Internet
Author: User
Tags in python

This example describes the use of random modules in Python. Share to everyone for your reference. Specifically as follows:

?

1 2 3 4 Import random x = Random.randint (1,4); y = Random.choice ([' Appale ', ' banana ', ' cherry ', ' durian ']); Print (x,y);

The results of the operation are as follows:

(2, ' cherry ')

No matter which language I study, I always like to get a random number to play. The Lunar New Year of November six, let ' s Python!!!

?

1 2 3 4 5 6 7 8 L=[] While true:name=input ("Please enter a name:") if name!= ': L.append (name) else:break print (L);

?

1 2 3 4 5 6 "' 1 to 100 direct all integers ' and ' ' X=range (1,101,1) sumi=0 for-I in x:sumi+=i print (Sumi)

The results of the operation are as follows:

5050

?

1 2 3 4 5 6 7 8 9 10 U1= ' general method for factorial ' Print (U1) def JC (n): S=1 for I in Range (1,n+1): s*=i return s n = input ("n! Usage:<number>: ") H = JC (int (n)) print (h)

The results of the operation are as follows:

?

1 2 3 A general method for factorial n! Usage:<number>:5 120

?

1 2 3 4 5 6 7 8 9 10 U2= ' "Factorial" "Print (U2) def jch (n): if (n==1): return 1 Else:return n*jch (n-1) un=input (" Input N: ") s=jch (int (un)) PRI NT ("n! Is ", s)

The results of the operation are as follows:

?

1 2 3 Implement factorial input N:5 (' n! is ', 120) by recursion

I hope this article will help you with your Python programming.

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.