Mar-23-2021, 11:40 AM
(Mar-22-2021, 08:49 PM)deanhystad Wrote: What Axel_Erfurt is doing is called slices, and it is how you should extract "slices" from a list. A slice is like this: the_list[start:end]. The slice starts with the_list[start] and ends at the_list[end-1].
If you leave out the start, i.e. the_list[:end], the slice includes all values starting with the_list[0] and ending with the_list[end-1]. If you leave out the end, i.e. the_list[start:], the slice includes all values starting at the_list[start] and ending at the end of the list.
It is a common belief among new Python programmers that print() can do things like return values or assign values. This is about as incorrect as you can be. All print(str) does is write str to a stream called stdout (standard output). Usually this appears in the terminal where you launched the Python program, but it can be redirected to appear in a different window or a file or a pipe.
print(days) does not add anything to a list or remove anything from a list. print(days) doesn't even write days to stdout. It writes a string what some programmer thought fairly represents days.
If you wanted to use a list to copy values from days to weekdays it would look something like this:
weekdays = [] weekends = [] for i in range(5): weekdays.append(day[i]) for i in range(5, 7): weekends.append(day[i])Just looking at how ugly this code is, it is easy to understand why slices were added to the language.
Hi, thank you so much for explaining this. You are right, I'm new to python and finding parts tricky to understand. I was of the impression the I would need to assign from one list to the other two and this is what I was typing into Google for assistance so I really appreciate you taking the time to explain this.
Have a great day :)