Skip to main content

Command Palette

Search for a command to run...

DAY 4 of PYTHON top 100 questions : from Basic to Advanced !!

Updated
1 min read
DAY 4 of PYTHON top 100 questions : from Basic to Advanced !!
P

Hello, I'm Priya Chakraborty, a dedicated B.Tech student in Electrical Engineering at Siliguri Institute Of Technology. I'm an enthusiastic learner, constantly seeking to expand my skill set and knowledge base. With a solid foundation in programming languages such as C, R, Python, and MySQL, I'm poised to tackle complex technical challenges.

But my passions extend beyond the realm of engineering. I'm also an aspiring content writer, driven by a curiosity to communicate ideas, both technical and non-technical, in a way that captivates and educates.

My journey is defined by a relentless pursuit of self-improvement, coupled with strong communication skills. I believe in the power of continuous learning and the importance of sharing knowledge.

Write a Python program to find the second largest element in an array:-

x=[2,7,3,4,9,0,2]
x.sort()
print("The second largest element is",x[-2])

Output :

The second largest element is 7

This code sorts the list x in ascending order using the sort() method and then prints the second largest element of the sorted list using indexing with x[-2].

Here's a breakdown of how the code works:

  • x=[2,7,3,4,9,0,2]: This creates a list x with seven elements.

  • x.sort(): This sorts the elements of the list x in ascending order. After sorting, the list becomes [0, 2, 2, 3, 4, 7, 9].

  • print("The second largest element is",x[-2]): This prints the string "The second largest element is" followed by the second largest element of the sorted list x, which is 7.


If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).


HAPPY LEARNING !!!

More from this blog

Priya's blog

46 posts

Python Developer@Codeclause Mentee @Trailhead Salesforce Content Executive @GDSC SIT'22-23 Google Women Techmakers Ambassador @Google WTM Coreteam @ GirlScript Kolkata