- Write a Python program to print the number of elements present in an array
- Write a Python program to print the number of elements present in an array
- Full Example
- Conclusion
- Python Program for Counting Distinct Elements in an Array
- Counting Distinct Elements in Python
- Example
- Method 1 :
- Time and Space Complexity :
- Method 1 : Code in Python
Write a Python program to print the number of elements present in an array
In this Python tutorial, I will show you, how to write a Python program to print the number of elements present in an array. We will see a complete example with the output.
Write a Python program to print the number of elements present in an array
In Python, arrays can be handled using built-in data types like a list or with an ‘array’ module. The task is simple: we want to create a Python program that prints out the number of elements present in an array. For this, we will use the built-in len() function, which returns the number of items in an object.
# create an array cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'] # print the number of elements in the array print(len(cities))
- The first line of code creates an array of cities. An array is just a list of items, and in Python, lists are created by enclosing the items in square brackets [] . Here, our items are strings that represent the names of cities in the USA.
- The second line uses the built-in len() function to count the number of elements in our array. The len() function simply returns the number of items in an object. When we pass our array to this function, it counts the number of items and returns this count.
Full Example
Let’s put it all together and write a complete Python program:
def count_elements(array): """ This function takes an array as an input and prints the number of elements in the array. """ print(len(array)) # create an array of cities cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'] # call the function and pass the cities array count_elements(cities)
In this script, we define a function count_elements(array) that prints the number of elements in the provided array. We then create an array cities , and finally, we call the function count_elements(cities) to count the number of elements in our cities array.
When you run this script, it will output:
You can look at the screenshot below for the output:
Conclusion
In this tutorial, we’ve learned how to write a Python program to print the number of elements in an array using the built-in len() function.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.
Python Program for Counting Distinct Elements in an Array
Counting Distinct Elements in Python
Here, in this page we will discuss the program for counting distinct elements in python programming language. We are given with an array and need to print the count of distinct element present in the given array.
Example
Input : arr[8] = [10, 20, 40, 30, 50, 20, 10, 20]
Output : 5
Explanation : 10, 20, 30, 40, 50 are the distinct elements.
Here, we will discuss two different methods to count the unique element in the given input array and compare there time and space complexity of these two methods.
Method 1 :
In this method we will count the frequency of each elements using two for loops.
- To check the status of visited elements create a array of size n and a variable say count_dis=0 which will count all distinct elements.
- Run a loop from index 0 to n and check if (visited[i]==1) then skip that element.
- Run a loop from index i+1 to n
- Check if(arr[i]==arr[j]), then set visited[j]=1.
- After complete iteration of inner for loop, increment the value of count_dis by 1.
- At last print the value of count_dis.
Time and Space Complexity :
Method 1 : Code in Python
# Python 3 program to count unique elements def count(arr, n): # Mark all array elements as not visited visited = [False for i in range(n)] count_dis=0 # Traverse through array elements # and count frequencies for i in range(n): # Skip this element if already # processed if (visited[i] == True): continue # Count frequency for j in range(i + 1, n, 1): if (arr[i] == arr[j]): visited[j] = True count_dis = count_dis+1; print(count_dis) # Driver Code arr = [10, 30, 40, 20, 10, 20, 50, 10] n = len(arr) count(arr, n)