Published in

Geek Culture

Python For Beginner Series|Day-15

Here we are looking into list functions examples

• 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 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 hereimport random# Split string methodnames_string = input("Enter the names, separated by a comma. ")names = names_string.split(", ")print(names)`
• 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']`
`print(names[0])`

Possible output:

`Ram`
• 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 hereimport random# Split string methodnames_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`
`random_choice=random.randint(0,list_num-1)print(random_choice)`

Possible Output:

`Enter the names, separated by a comma. RAM, SAM, TAN, VAN2`
• In case we want to know the random index position name
`person_name=names[random_choice]`
`# Import the random module hereimport random# Split string methodnames_string = input("Enter the names, separated by a comma. ")names = names_string.split(", ")#get the lenght of list valuelist_num=(len(names))#get the random value random_choice=random.randint(0,list_num-1)# get the random person nameperson_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, VANTAN Who's going to pay the meal bill`
`# Import the random module hereimport random# Split string methodnames_string = input("Enter the names, separated by a comma. ")names = names_string.split(", ")#random modul choice function to generate automatic range of numbersperson_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`

--

--

More from Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Get the Medium app

As a DBA we are going to write and discuss multiple database administration concepts. “Nothing Grows In Comfort Zone”.