Python For Beginner Series|Day-15
Here we are looking into list functions examples
- On Day 15, Today we will do some exercises using List functions.
- Here we are going to do the exercise to select a random name from a list of names. The person selected will have to pay for everybody’s food bill.
Instructions:
- You are not allowed to use the
choice()
function - you must enter all the names as names followed by a comma and then space because we have to split the strings based on into individual words and put them inside a List
# Import the random module here
import random
# Split string method
names_string = input("Enter the names, separated by a comma. ")
names = names_string.split(", ")
print(names)
- In this above example, we have imported the random module and the user names are passed using comma-separated values i.e our sample outputs are stored in “names_string”
- In the next step by using the split function (comma), we are stored the values in a list
Possible output:
Enter the names, separated by a comma. Rqm, Mynotes, ORACLE, DBA
['Ram','Mynotes','ORACLE','DBA']
- In the same example, I am going to print the value from the list on a particular index position
print(names[0])
Possible output:
Ram
- Here how could we display the random value in the program by using the random module it is easy to display the values randomly.
- even if we were using the random int function we must know the starting and ending values.
- From the list how could we get the end value? Here we can use the function name called “len” to get the numerical values present in the list
# Import the random module here
import random
# Split string method
names_string = input("Enter the names, separated by a comma. ")
names = names_string.split(", ")
print(names) #Enter the names, separated by a comma. RAM, SAM, TAN, VAN
list_num=len(names)
print(list_num)
Possible Output:
['RAM', 'SAM', 'TAN', 'VAN']
4 #lenth funtion output
- As we know the list function index position starts from 0, Hence we need to minus one from the total value.
random_choice=random.randint(0,list_num-1)
print(random_choice)
- I am using the same above program added these two lines into my program
Possible Output:
Enter the names, separated by a comma. RAM, SAM, TAN, VAN
2
- Here we have assigned the value to a random choice, which creates and display the value from 0 to 3.
- In case we want to know the random index position name
person_name=names[random_choice]
- Now we can see the entire program
# Import the random module here
import random
# Split string method
names_string = input("Enter the names, separated by a comma. ")
names = names_string.split(", ")
#get the lenght of list value
list_num=(len(names))
#get the random value
random_choice=random.randint(0,list_num-1)
# get the random person name
person_name=names[random_choice]
print(person_name + " Who's going to pay the meal bill ")
Possible Output:
Enter the names, separated by a comma. RAM, SAM, TAN, VAN
TAN Who's going to pay the meal bill
- The alternative method for this program we can use choice(), It will make us to use very less code that why we had mentioned in the instruction don't use this function until you get the logic.
# Import the random module here
import random
# Split string method
names_string = input("Enter the names, separated by a comma. ")
names = names_string.split(", ")
#random modul choice function to generate automatic range of numbers
person_name=random.choice(names)
print(person_name + " Who's going to pay the meal bill ")
Possible Output:
VAN Who's going to pay the meal bill
- Hope you can understand this list function logic program, if you weren’t pls read the previous blog and practice this example