list('hello!')
name = 'Moriarty'
name[0]
name[1:3]
name[-1]
'Mo' in name
'James' in name
for letter in name:
print(letter)
Now let's see some interesting/weird thing now
name = 'Moriarty'
something = name
name = 'Scresat'
name
something
So far so good...
Let's do that same thing, now with the list.
name = [0,1,2,3,4,5]
something = name
something[1] = 'Hello!'
something
Now comes the weird part
name
The original list is changed!
Reason - Variables don't contain list, they contain references to the list
Explanation - When the list [0,1,2,3,4,5] is created, the list gets stored in the memort, and name here references it. so when we create another variable, in this case something and assign value of name to it, it doesn't create new list just for 'something', it merely creates new reference to the original list [0,1,2,3,4,5] which is stored in memory.
So when we modify one reference, here it is 'something', it modifies the list to which it is referenced to in the memory. That in change changes the values of the other variables since it is variables are merely a references.
Same thing applies to other mutable data types.
Conceps of global and local scope doesn't apply in the following case
def eggs(cheese):
cheese.append('hello')
some = [1,2,3]
eggs(some)
print(some)
import copy
some = [1,2,3,4]
cheese = copy.deepcopy(some)
cheese
cheese.append('hello')
cheese
some
We here used the deepcopy() function of the copy module, which creates a new list in memory.
Sometimes when we are running out of line in the editor we can use \ in the end to tell python to ignore the next indentation.
print('Live long and' \
+ ' prosper')
Which is same as:
print('live long and prosper')