2. Random Number Functions
Random numbers are used for games, simulations, testing, security, and privacy
applications. Python includes the following functions that are commonly used.
3. Random Number Functions
import random
print ("returns a random number from range(100) : ",random.choice(range(100)))
print ("returns random element from list [1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]))
print ("returns random character from string 'Hello World' : ",
random.choice('Hello World'))
Number randrange() Method
The randrange() method returns a randomly selected element from range(start, stop, step).
Syntax
randrange ([start,] stop [,step])
Example:
import random
# randomly select an odd number between 1-100
print ("randrange(1,100, 2) : ", random.randrange(1, 100, 2))
# randomly select a number between 0-99
print ("randrange(100) : ", random.randrange(100))
4. Random Number Functions
Number random() Method
The random() method returns a random floating point number in the range [0.0, 1.0].
Syntax
random()
Example
import random
# First random number
print ("random() : ", random.random())
# Second random number
print ("random() : ", random.random())
9. String special Operators
One of Python's coolest features is the string format operator %. This operator is unique
to strings and makes up for the pack of having functions from C's printf() family.
print ("My name is %s and weight is %d kg!" % ('Zara', 21))
16. Built-in String Methods
Some Examples:
str = "this is string example....wow!!!"
print ("str.capitalize() : ", str.capitalize())
Syntax: str.count(sub, start= 0,end=len(string))
str="this is string example....wow!!!"
sub='i'
print ("str.count('i') : ", str.count(sub))
sub='exam'
print ("str.count('exam', 10, 40) : ", str.count(sub,10,40))
Parameters
• sub - This is the substring to be searched.
• start - Search starts from this index. First character starts from 0 index. By default
search starts from 0 index.
• end - Search ends from this index. First character starts from 0 index. By default
search ends at the last index.
17. Built-in String Methods
Some Examples:
Str = "this is string example....wow!!!";
Str = Str.encode('base64','strict');
print "Encoded String: " + Str
print "Decoded String: " + Str.decode('base64','strict')
str.find(str, beg=0 end=len(string))
Parameters
• str - This specifies the string to be searched.
• beg - This is the starting index, by default its 0.
• end - This is the ending index, by default its equal to the lenght of the string.
18. Built-in String Methods
Some Examples:
Str = "this is string example....wow!!!";
Str = Str.encode('base64','strict');
print "Encoded String: " + Str
print "Decoded String: " + Str.decode('base64','strict')
str.find(str, beg=0 end=len(string))
Parameters
• str - This specifies the string to be searched.
• beg - This is the starting index, by default its 0.
• End- This is the ending index, by default its equal to the length of the string.
str1 = "this is string example....wow!!!"
str2 = "exam";
print (str1.find(str2))
print (str1.find(str2, 10))
print (str1.find(str2, 40))