SlideShare a Scribd company logo
Write a function called countElements that counts the number of times two elements appear in a
list. Your task is to ask the user to enter a list and two elements which they need to count in the
list. The list and each of the elements should be inputs to a function called countElements. The
function should print the count of the number of times the elements appear in the list. def
countElements(countUst, element1, element2): Example: if the inputs to the function were ("t",
"s", 7, 7, 2], "t", and 7, the function would count the instances of the letter T and the number 7
in the list, and print the number 3. There was one T and two 7's. Name your file
Lastname_Firstname.py
Solution
def countElements(list, ch1, ch2):
count = 0
for i in range(0,len(list)):
if list[i] == ch1:
count = count + 1
for i in range(0,len(list)):
if list[i] == ch2:
count = count + 1
return count;
print countElements(["t","s",7,7,2], "t", 7)
Output:
sh-4.3$ python main.py
3

More Related Content

Similar to Write a function called countElements that counts the number of times.pdf (20)

PDF
List in Python Using Back Developers in Using More Use.
SravaniSravani53
 
PPTX
Python list tuple dictionary presentation
NarendraDev11
 
PDF
Python Data Types (1).pdf
NehaSpillai1
 
PDF
Python Data Types.pdf
NehaSpillai1
 
PPTX
List_tuple_dictionary.pptx
ChandanVatsa2
 
PPTX
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
usha raj
 
PDF
15CS664- Python Application Programming - Module 3
Syed Mustafa
 
PDF
15CS664-Python Application Programming - Module 3 and 4
Syed Mustafa
 
PDF
The Ring programming language version 1.8 book - Part 27 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 22 of 184
Mahmoud Samir Fayed
 
PDF
Unit 1-Part-4-Lists, tuples and dictionaries.pdf
Harsha Patil
 
DOCX
Assume a list has the following element- write a function to interch.docx
olsenlinnea427
 
DOCX
cs class 12 project computer science .docx
AryanSheoran1
 
PPTX
Chapter 3-Data structure in python programming.pptx
atharvdeshpande20
 
DOCX
Given a list of numbers- count and return how many pairs there are- A.docx
rtodd751
 
PPTX
updated_list.pptx
Koteswari Kasireddy
 
PDF
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 
PDF
The Ring programming language version 1.9 book - Part 29 of 210
Mahmoud Samir Fayed
 
PPTX
Python data type
Jaya Kumari
 
List in Python Using Back Developers in Using More Use.
SravaniSravani53
 
Python list tuple dictionary presentation
NarendraDev11
 
Python Data Types (1).pdf
NehaSpillai1
 
Python Data Types.pdf
NehaSpillai1
 
List_tuple_dictionary.pptx
ChandanVatsa2
 
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
usha raj
 
15CS664- Python Application Programming - Module 3
Syed Mustafa
 
15CS664-Python Application Programming - Module 3 and 4
Syed Mustafa
 
The Ring programming language version 1.8 book - Part 27 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 22 of 184
Mahmoud Samir Fayed
 
Unit 1-Part-4-Lists, tuples and dictionaries.pdf
Harsha Patil
 
Assume a list has the following element- write a function to interch.docx
olsenlinnea427
 
cs class 12 project computer science .docx
AryanSheoran1
 
Chapter 3-Data structure in python programming.pptx
atharvdeshpande20
 
Given a list of numbers- count and return how many pairs there are- A.docx
rtodd751
 
updated_list.pptx
Koteswari Kasireddy
 
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 
The Ring programming language version 1.9 book - Part 29 of 210
Mahmoud Samir Fayed
 
Python data type
Jaya Kumari
 

More from feetshoemart (20)

PDF
Convert the following expressions from infix to Reverse Polish Notati.pdf
feetshoemart
 
PDF
Andy and Joan are married and under 65 years of age. They have two c.pdf
feetshoemart
 
PDF
Compare the methods by which the Parsees of India benefitted from th.pdf
feetshoemart
 
PDF
Computer Forensics MethodologiesList them and explain each one.P.pdf
feetshoemart
 
PDF
Document2- Word (Product Activation Failed) Mailings Review View Tell.pdf
feetshoemart
 
PDF
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdf
feetshoemart
 
PDF
calculate the hydrogen ion concentration in mol for the following s.pdf
feetshoemart
 
PDF
At a certain temperature, 0.5011 mol of N2 and 1.781 mol of H2 are pl.pdf
feetshoemart
 
PDF
Consider mappinp, phi Z rightarrow R, defined as phi(x) = 2x, where .pdf
feetshoemart
 
PDF
wk3 reply to prof.I need help with this question below a 200 words.pdf
feetshoemart
 
PDF
Write a program that prompts the user to enter a positive integer and.pdf
feetshoemart
 
PDF
This is a java lab assignment. I have added the first part java re.pdf
feetshoemart
 
PDF
There is no video, All you have to do is show how you reference the .pdf
feetshoemart
 
PDF
The process of Imperialism and Colonialism played a role in the esta.pdf
feetshoemart
 
PDF
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
feetshoemart
 
PDF
What do viral genomes look like compared to those of living organism.pdf
feetshoemart
 
PDF
Verify the identity Verify the identity by transforming the left-han.pdf
feetshoemart
 
PDF
Using following main file and solve the taskInst.pdf
feetshoemart
 
PDF
Suppose there are 14 children trying to form two teams with seven ch.pdf
feetshoemart
 
PDF
11. Define a simple deformable model to detect a half-circular shape.pdf
feetshoemart
 
Convert the following expressions from infix to Reverse Polish Notati.pdf
feetshoemart
 
Andy and Joan are married and under 65 years of age. They have two c.pdf
feetshoemart
 
Compare the methods by which the Parsees of India benefitted from th.pdf
feetshoemart
 
Computer Forensics MethodologiesList them and explain each one.P.pdf
feetshoemart
 
Document2- Word (Product Activation Failed) Mailings Review View Tell.pdf
feetshoemart
 
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdf
feetshoemart
 
calculate the hydrogen ion concentration in mol for the following s.pdf
feetshoemart
 
At a certain temperature, 0.5011 mol of N2 and 1.781 mol of H2 are pl.pdf
feetshoemart
 
Consider mappinp, phi Z rightarrow R, defined as phi(x) = 2x, where .pdf
feetshoemart
 
wk3 reply to prof.I need help with this question below a 200 words.pdf
feetshoemart
 
Write a program that prompts the user to enter a positive integer and.pdf
feetshoemart
 
This is a java lab assignment. I have added the first part java re.pdf
feetshoemart
 
There is no video, All you have to do is show how you reference the .pdf
feetshoemart
 
The process of Imperialism and Colonialism played a role in the esta.pdf
feetshoemart
 
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
feetshoemart
 
What do viral genomes look like compared to those of living organism.pdf
feetshoemart
 
Verify the identity Verify the identity by transforming the left-han.pdf
feetshoemart
 
Using following main file and solve the taskInst.pdf
feetshoemart
 
Suppose there are 14 children trying to form two teams with seven ch.pdf
feetshoemart
 
11. Define a simple deformable model to detect a half-circular shape.pdf
feetshoemart
 

Recently uploaded (20)

PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
PPTX
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PDF
The-Beginnings-of-Indian-Civilisation.pdf/6th class new ncert social/by k san...
Sandeep Swamy
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PPTX
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
PPTX
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
Presentation: Climate Citizenship Digital Education
Karl Donert
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
The-Beginnings-of-Indian-Civilisation.pdf/6th class new ncert social/by k san...
Sandeep Swamy
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Presentation: Climate Citizenship Digital Education
Karl Donert
 
PPT on the Development of Education in the Victorian England
Beena E S
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 

Write a function called countElements that counts the number of times.pdf

  • 1. Write a function called countElements that counts the number of times two elements appear in a list. Your task is to ask the user to enter a list and two elements which they need to count in the list. The list and each of the elements should be inputs to a function called countElements. The function should print the count of the number of times the elements appear in the list. def countElements(countUst, element1, element2): Example: if the inputs to the function were ("t", "s", 7, 7, 2], "t", and 7, the function would count the instances of the letter T and the number 7 in the list, and print the number 3. There was one T and two 7's. Name your file Lastname_Firstname.py Solution def countElements(list, ch1, ch2): count = 0 for i in range(0,len(list)): if list[i] == ch1: count = count + 1 for i in range(0,len(list)): if list[i] == ch2: count = count + 1 return count; print countElements(["t","s",7,7,2], "t", 7) Output: sh-4.3$ python main.py 3