- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python â€“ Calculate the percentage of positive elements of the list
When it is required to calculate the percentage of positive elements of the list, a list comprehension and the ‘len’ method are used.
Below is a demonstration of the same −
my_list = [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] print("The list is :") print(my_list) my_result = (len([element for element in my_list if element > 0]) / len(my_list)) * 100 print("The result is :") print(my_result)
The list is : [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] The result is : 0
A list is defined and displayed on the console.
A list comprehension is used to iterate over the list, and check if an element is greater than 0, and these elements are divided by product of length of list and 100.
The length of the above operation is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Get positive elements from given list of lists in Python
- How to Calculate the Percentage of Yes and No from a List in Excel?
- Python program to find Tuples with positive elements in List of tuples
- Python program to find Tuples with positive elements in a List of tuples
- Java program to calculate the percentage
- How to calculate percentage of any value ?
- List consisting of all the alternate elements in Python
- Calculate difference between adjacent elements in given list using Python
- Python - Change the signs of elements of tuples in a list
- Python program to sort the elements of the Circular Linked List
- How to calculate percentage of total in Excel?
- How to calculate the percentage of year or month passed in Excel?
- List frequency of elements in Python
- Find sum of frequency of given elements in the list in Python
- Python â€“ Fractional Frequency of elements in List