Python: assigning lists
By challenges exercises, it seems to me, when you equals two lists: list2 = list1 sometimes list2 references list1, sometimes it's a copy of its own. Is there a general rule to distinguish two cases?
List2=list1[:] Create a copy of list1
Ha - interesting to know! Thanks for making that one! I think, adding deepcopy is a bit unfair because we have simple objects here. Its strong points might come out when you have multi-dimensional lists or something
I want to point out that in Python when you write varB = varA, it's *always* a reference to the same object - like a second name sticker. It's just with lists (and other mutable types), that it can lead to issues. Make a few experiments with different types and use the id function to confirm that it's actually always that very object.
You can use: list2 = list1.copy() to make sure that changes on the lists don't affect each other, but if list1 contained lists, list1.copy() doesn't make copies of the inner lists, but uses the original inner lists instead. This can be fixed with copy.deepcopy: import copy list2 = copy.deepcopy(list1)
Problem Statement Given two integers a and b, your task is to calculate and print the following four values:
Please help, any idea?
Loop question, I've tried everything that I knew I just don't know. Please help me solve it out
Help me wiht python