Python random module usage Example Analysis, pythonrandom

Source: Internet
Author: User

Python random module usage Example Analysis, pythonrandom

This article describes how to use the random module in Python. Share it with you for your reference. The details are as follows:

import randomx = random.randint(1,4);y = random.choice(['appale','banana','cherry','durian']);print(x,y);

The running result is as follows:
(2, 'cherry ')

No matter which language I want to learn, I always like to get a random number for fun. Let's Python !!!

L = [] while True: name = input ("enter a name:") if name! = '': L. append (name) else: breakprint (l );
''' Calculates the sum of all integers from 1 to 100 and ''' x = range (1,101, 1) sumi = 0for I in x: sumi + = iprint (sumi)

The running result is as follows:
5050

U1 = ''' general method for factorial ''' print (u1) def jc (n): s = 1 for I in range (1, n + 1 ): s * = I return sn = input ("n! Usage: <number>: ") h = jc (int (n) print (h)

The running result is as follows:

The general method of factorial n! Usage: <number>: 5120
U2 = ''' implement 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) print ("n! Is ", s)

The running result is as follows:

Implement factorial input n: 5 ('n! Is ', 120)

I hope this article will help you with 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.