Python Program to Print First N Elements of a List

Python Program to Print First N Elements of List

In this article, we will be solving question based on List and Looping in Python. According to the question we have to print first N elements of a list.

If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python Program to Print First N Elements of a List

Python Program to Print First N Elements of a List


Logic and Explanation

Question - Write a Python Program to Print First N Elements of a List

Python Program to Print First N Elements of a List - In this program we have to find print first N elements of a list. So, for this we will be using the concept of accessing an element of a list using it's index and for loop.

Know More About List and List Slicing - Click Here

As indexing in list starts from 0, so for printing first N elements we will be running a loop from 0 to N-1 and then by using the index we will be printing the elements at the corresponding indices.

Let's see the code for more clarity.

L = [2,4,8,23,74,2,82,43,83,98,22,56,45]
for i in range(N):
    print(L[i])


Code Insights

  1. Consider a list L and it contains some elements.

  2. In for-loop, you can see it is written range(N). This runs the loop from 0 to N-1. So, whenever you want to run a loop from 0 to N-1, use range(N). It is similar to range(0,N).

  3. An element as index i in a list can be accessed as L[i] where L is the list.

So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python






For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post