Académique Documents
Professionnel Documents
Culture Documents
py at master · sandeepsuryaprasad/python_tutorials
120cd78 on Sep 3
1 contributor
Code navigation is available for this repository but data for this commit does not exist. Learn more or give us feedback
1 import random
2
3 print(random.random()) # Prints a random floating point number between 0 and 1. Both excluding. e.g. 0.1, 0.8
4
5 print(random.uniform(1, 10)) # Prints random floating point number between the specified range (1 and 10).
6
7 print(random.randint(1, 6)) # Prints random integer between 1 and 6. Both inclusive.
8
9 print(random.randrange(0, 100, 5)) # Prints random integer between 0 and 100 with step size of 5
10
11 names = ['apple', 'google', 'yahoo', 'facebook', 'microsoft', 'instagram', 'watsapp']
12
13 print(random.choice(names)) # Prints a random item in the list 'names'
14
15 # Creating a combination of first name and last names from the list
16 fname = ['steve', 'bill', 'mark', 'rob', 'mike', 'laura', 'adams']
17 lname = ['jobs', 'gates', 'waugh', 'charlie', 'stone', 'turner', 'randolph']
18
19 for _ in range(10):
20 full_name = f'{random.choice(fname)} {random.choice(lname)}'
21 print(full_name)
22
23 values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
24 print(random.sample(values, 3)) # Prints random subset of the list
25
26 # Shuffles the list
27 random.shuffle(values) # Only Mutable data can be passed to shfulle method
28 # Shufulle does not return new list, insted it mutates the existing list
29 print(values)
30
31 # Shuffles the list
32 random.shuffle(values)
33 print(values)
34
35 suits = ['hearts', 'clubs', 'diamonds', 'spades']
36 random.shuffle(suits)
37
38
39 # Generating 8 Character random password
40 random_string = '''
41 ABEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0987654321!@#$%^()CD_?><&*
42 '''
43
44 password = ''
45 for _ in range(8):
46 password = password + random.choice(random_string)
47 print(password)
48
49
50 # Generating Random Password
https://github.com/sandeepsuryaprasad/python_tutorials/blob/master/Random_Module/_random_module.py 1/2
11/27/2019 python_tutorials/_random_module.py at master · sandeepsuryaprasad/python_tutorials
51 def random_password(*, upper=1, lower=1, digits=1, special=1, length=8):
52 UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
53 LOWERCASE = UPPERCASE.lower()
54 DIGITS = "0123456789"
55 SPECIAL = "!@#$%^&*()"
56 ALL = UPPERCASE + LOWERCASE + DIGITS + SPECIAL
57
58 password = [
59 *(random.choice(UPPERCASE) for _ in range(upper)),
60 *(random.choice(LOWERCASE) for _ in range(lower)),
61 *(random.choice(DIGITS) for _ in range(digits)),
62 *(random.choice(SPECIAL) for _ in range(special)),
63 *(random.choice(ALL) for _ in range(length - upper - lower - digits - special))
64 ]
65 return password
66
67
68 p = random_password(upper=1, lower=3, digits=1, special=1, length=8)
69 print(''.join(p))
70
71
72 def random_password2(*, upper=1, lower=1, digits=1, special=1, length=8):
73 UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
74 LOWERCASE = UPPERCASE.lower()
75 DIGITS = "0123456789"
76 SPECIAL = "!@#$%^&*()"
77 ALL = UPPERCASE + LOWERCASE + DIGITS + SPECIAL
78
79 password = ''
80
81 for _ in range(upper):
82 password += random.choice(UPPERCASE)
83 for _ in range(lower):
84 password += random.choice(LOWERCASE)
85 for _ in range(digits):
86 password += random.choice(DIGITS)
87 for _ in range(special):
88 password += random.choice(SPECIAL)
89 for _ in range(length - upper - lower - digits - special):
90 password += random.choice(ALL)
91
92 return password
93
94
95 print(random_password2(upper=1, lower=3, digits=1, special=1, length=8))
https://github.com/sandeepsuryaprasad/python_tutorials/blob/master/Random_Module/_random_module.py 2/2