Here are just a few of them.
I'm just working from web pages I found on the internet.
I intend to add to this over time.
# Print the last element on the list
# In Python, array indexes start with 0
# len[alist] prints the length
mylist = ['a', 'b', 'c', 'd']
print ("last element: ", mylist [len(mylist)-1])
last element: d
# Find last but one in a list
# [Ummm, this is clearly harder in other languages]
mylist = ['a', 'b', 'c', 'd']
print ("last element: ", mylist [len(mylist)-2])
last element: c
# Find the ith element of a list where the first one starts at 1
i=7
mylist = ['a', 'b', 'c', 'd','e','f', 'g', 'h', 'i', 'j', 'k']
print(mylist[i-1])
g
# Find the number of elements of a list.
mylist = ['a', 'b', 'c', 'd','e','f', 'g', 'h', 'i', 'j', 'k']
print("Number of elements in list is: ", len(mylist))
Number of elements in list is: 11
# Reverse a list.
mylist = ['a', 'b', 'c', 'd','e','f', 'g', 'h', 'i', 'j', 'k']
# this works in place so you can't print it all on the same line
mylist.reverse()
print (mylist)
['k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
# Test if a list is a palindrome
# palindrome check - this is more convoluted than some but it helps to see the steps detailed
# Check if a given list is a palindrome
# Check indexes from 0 to the middle of the list
# Check if first index = last index, then move in by one
# Some diagnostics left in as comments
def palcheck(mylist):
NotAPal = 0
#print("Middle is: ",int(len(mylist)/2))
#print ("Length of list: ", len(mylist))
#print ("Last index: ", len(mylist)-1, "Contains: ", mylist[len(mylist)-1])
lastindex = len(mylist)-1
for i in range (0, int(len(mylist)/2)):
if (mylist[i] != mylist[lastindex-i]):
print(mylist, "is not a palindrome")
NotAPal = 1
break
if (NotAPal == 0):
print (mylist, "is a palindrome")
palcheck ([1,2,3,4,5,4,3,2,1])
palcheck ([1,2,3,4,2,3,2,1])
palcheck ([1,1,1,1])
palcheck ([1,2,3,2,1])
palcheck ([1,4,4,2,1])
[1, 2, 3, 4, 5, 4, 3, 2, 1] is a palindrome
[1, 2, 3, 4, 2, 3, 2, 1] is not a palindrome
[1, 1, 1, 1] is a palindrome
[1, 2, 3, 2, 1] is a palindrome
[1, 4, 4, 2, 1] is not a palindrome
[1, 2, 3, 4, 2, 3, 2, 1] is not a palindrome
[1, 1, 1, 1] is a palindrome
[1, 2, 3, 2, 1] is a palindrome
[1, 4, 4, 2, 1] is not a palindrome
2 comments:
This is great information. Thank you for providing it. I am sure it will help me complete my assignment, and now I can easily find an Accounting Essay Help service, or else, if you know of any other source, please let me know. I would be very thankful to you.
It's great to see these fundamental Python list exercises laid out so clearly! I always find it satisfying to practice these basic operations; they're the building blocks for more complex tasks. Speaking of satisfying, I've been unwinding lately with a puzzle game called Block Blast. There's something incredibly calming about clearing rows and keeping the board from filling up, much like efficiently handling data structures. Do you find these foundational exercises also help you think more clearly when tackling larger coding projects?
Post a Comment