Check if the first and last number of a list is the same

You can check if the first and last number of a list are the same in Python by comparing the first element (index 0) to the last element (index -1) of the list. Here is an example:
lst = [1, 2, 3, 4, 5] if lst[0] == lst[-1]: print("The first and last numbers are the same.") else: print("The first and last numbers are not the same.")
//output: The first and last numbers are not the same.

In this example, the first number is 1 and the last number is 5, so the output will be "The first and last numbers are not the same."

Python List


Python list first and last
In Python, a list is a built-in data type that allows you to store a collection of items in a single variable. Lists are ordered and can store items of any data type, including numbers, strings, and other lists. Lists are created by placing items inside square brackets [ ], separated by commas. Here is an example of a list:
lst = [1, 2, 3, 4, 5]

Python if...else

In Python, the if...else statement is used to control the flow of program execution based on a certain condition. The if statement checks the condition, and if it evaluates to True, the code block following the if statement is executed. If the condition evaluates to False, the code block following the else statement is executed. Example:
x = 5 if x > 0: print("x is positive") else: print("x is non-positive")
//Output: x is positive