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.