1. Home
2. Docs
3. Learn Python By Examples
4. Learn Python by examples – Ex-26-30

Learn Python by examples – Ex-26-30

Learn Python by examples – Ex-26-30

Note there are multiple ways to solve a problem.
Here is one solution.

Exercise 26:Write a program to count the number of occurrences of an element

1.count ‘a’ in ‘banana is good for health’

2.count 20 in [20,30, 40, 50, 60, 20, 40, 10, 30]

Solution:

Concept:

Iterating through a sequence, and counting the elements.
Common funciton for any sequence.

```#common for a function for string and list

def count_element(source, elem):
count = 0
for item in source:
if item == elem:
count += 1
return count

str1 = 'banana is good for health'

num_list = [20,30, 40, 50, 60, 20, 40, 10, 30]

count1 = count_element(str1,'a')

print("number of a's", count1)

count2 = count_element(num_list,20)

print("number of 20's", count2)

Output:

number of a's 4
number of 20's 2

```

Exercise 27: Write a program to sepearate strings and numbers from given list

gadgets = [‘Mobile’, ‘Laptop’, 100, ‘Camera’, 310.28, ‘Speakers’, 27.00, ‘Television’, 1000, ‘Laptop Case’, ‘Camera Lens’]

Concept:

Using isinstance() built-in function to differentiate string and numbers.

Solution:

```#isinstance, append

gadgets = ["Mobile", "Laptop", 100, "Camera", 310.28, "Speakers", 27.00,
"Television", 1000, "Laptop Case", "Camera Lens"]

str_items = []
num_items = []

if isinstance(item, str):

str_items.append(item)

elif isinstance(item,int) or isinstance(item, float):

num_items.append(item)

print(str_items)

print(num_items)

Output:
['Mobile', 'Laptop', 'Camera', 'Speakers', 'Television', 'Laptop Case', 'Camera Lens']
[100, 310.28, 27.0, 1000]

```

Exercise 28: Write a program get total of students scores

student_scores =[
‘Alex|75 50 90 80 90 70’,
‘Mary|76 72 71 68 85 69’,
‘John|69 67 68 71 68 67’,
‘Anne|80 69 59 82 71 81’,
‘Mark|79 81 74 71 69 73’
]

Solution:

Concept:

Iterate through the list, separate the string for string and numeric part.
Split the numeric string part and add numbers by converting them to int.

```#display student name and total scores

student_scores =[
'Alex|75 50 90 80 90 70',
'Mary|76 72 71 68 85 69',
'John|69 67 68 71 68 67',
'Anne|80 69 59 82 71 81',
'Mark|79 81 74 71 69 73'
]

def student_total_scores(stud_scores):

for item in stud_scores:
name, scores = item.split("|")
print(name,sum(map(int,scores.split())))

student_total_scores(student_scores)

Output:

Alex 455
Mary 441
John 410
Anne 442
Mark 447

```

Exercise 29: Write a programming to get biggest and smallest scores from the list.

The list may contain duplicates.

Ex: [86,86,45,85,5,83,23,45] => should get 86, 5

Solution:

Concept:

Write your own,max() and min() functions.

```
##my max and min

def get_max(num_list):
biggest = None
for num in num_list:
if biggest is None or num > biggest :
biggest = num
return biggest

def get_min(num_list):
smallest = None
for num in num_list:
if smallest is None or num < smallest :
smallest = num
return smallest

#given_list = [86,86,45,85,5,83,23,45]
#given_list = [86,86,86,86]
given_list = [5,5,5,5,5,5,5,5,5,5,5,5,5]

print(get_max(given_list),get_min(given_list))

Output:

86 5

```

Exercise 30: Write a program to get domain name from

‘From [email protected] Sat Jan 20 08:10:12 2018’

Solution:

Concept:
Using find() method, slicing mechanism on string.

```str1 = 'From [email protected] Sat Jan 20 08:10:12 2018'

at_pos = str1.find('@')

print(at_pos)

#S.find(sub[, start[, end]]) -> int

sp_pos = str1.find(' ',at_pos)

print(sp_pos)

domain_name = str1[at_pos +1 :sp_pos]
print(domain_name)

Output:

17
33
michaelbird.com

```