Setting a start:stop value to be the same number for both values when slicing, will result in a empty list or empty datatype of what you are slicing. If you use Object Assignment with Slicing, and you assign a value to the same start:stop values, such as list[1:1], it will then insert the value you assigned to the value before the start value you sliced, and the start value. So if you did list[1:1] = [1, 2, 3], it would insert 1, 2, 3 between indexes 0 and 1.

The "del" keyword basically allows you delete an item at a specific index from a list. Not specifying what index you want to delete the value from, will result in you deleting the entire list, which is proven since if you were to try to call that list again, it would return a ValueError, saying that the list you tried to call is undefined.

The difference between a list and a tuple, is that a list is mutable, whereas a tuple is immutable, meaning that the only way you can modify a tuple, is to manually create a entirely new one with the new values you want inside of it. You can use slicing to call the index of values in tuples, however you cannot use slicing to assign new values to existing ones in tuples, or to add new values, since tuples are immutable unlike lists. You can use slicing to call indexes of values in tuples, and add them with indexes of values in other tuples, just like you can with lists. Adding strings from lists will result in a list created with each of the values inside, each in their own string, seperated by a comma. Adding strings from tuples will result in a tuple with a string inside that basically "pieced" together the two strings. Adding single, specific values from lists using slicing will result in a sum of the integers added together, however if you just add the entire lists together, it will result in the values from the lists being pieced together into a new list, so you aren't "adding" the values together like 1 + 1 = 2, rather 1 + 1 = 11. (The same logic works for tuples as well.)

Object Assignment with slicing
You can call indexes of anything that is indexed, and that can be called using their indexes, and assign a value to that specific index(or index range.)
eg.

list1 = [1, 2, 3]
list1[3:] = 4, 5, 6
print(list1)

Whenever you are trying to use list slicing to assign value(s) to a list index range that only ends when the list index ends, you must remember to put the start value as one index position higher than the index you actually want to insert the value after. This is because when you indicate the start value, but not the stop value, python will insert the items you want to insert STARTING from your start value, which means that if you put the start value as the index of the last value where you want to insert the first item to, python will overwrite your original value at that index, and replace it with what you wanted to replace it with. If you just specified a stop value, but not a start value, be aware that python prints every value UP TO the index of the stop value, but doesn't actually return the value that is at the stop value.

If you only put one value in a tuple, it will not actually be classfied as a tuple, rather as whatever datatype you tried to pass a tuple. You can solve this by simply adding a comma "," after the value that you passed in the tuple, even if you don't put anything after the comma, the one value that you passed to a tuple, will now be classified as a tuple.

Lists can only be assigned iterable values, so you cannot assign something like an integer value, since you cannot iterate through a integer, whereas with something like a list, you can iterate through that, so lists must take values in another list, or anything that can be iterated through. This rule goes for when you are assigning anything to lists, or when you are using slicing to assign other objects to lists. Not following this rule will result in python returning a TypeError saying that you can only assign something iterable to lists.

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。QQ、微信在线,随时响应!V:Jiabcdefh
AI悦创 » Python Lesson 2 Notes

发表评论