# Write a program in Python to generate five random even index lowercase alphabets in a series

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

## Solution 1

• Define lowercase alphabets in a list.

• Create for loop and find the even index elements and add it to another list. It is defined below

   ls = []
for i in l:
if(l.index(i)%2==0):
ls.append(i)
• Finally, apply random sample 5 values to the list and generate a series.

### Example

import pandas as pd
import string
import random as r
chars = string.ascii_lowercase
print("lowercase alphabets are:-",chars)
chars_list = list(chars)
data = r.sample(chars_list[::2],5)
print("random even index char's are:-",data)
result = pd.Series(data)
print("Series:",result)

### Output

lowercase alphabets are:- abcdefghijklmnopqrstuvwxyz
random even index char's are:- ['w', 'k', 'i', 'u', 'm']
Series:
0    w
1    k
2    i
3    u
4    m
dtype: object

## Solution 2

### Example

import string
import pandas as pd
import random as r
l = list(string.ascii_lowercase)
ls = []
for i in l:
if(l.index(i)%2==0):
ls.append(i)
data = pd.Series(r.sample(ls,5))
print(“Series:”, data)

### Output

Series:
0    o
1    i
2    q
3    c
4    a
dtype: object
Updated on 24-Feb-2021 06:14:05