0

Help use "del" funcion !!

Hello its me again :D. Okay i want ask how to use "del" ? i need this for codding not find in course with "del" ... i use this if something bad please explame me :) a = ("Choose number 1") print(a) if 2 == 3: del(a) print("something") Where i do bad? can explame with code show? Thanks ^^

18th Nov 2016, 4:53 PM
Lukas Bolevičius
Lukas Bolevičius - avatar
5 odpowiedzi
+ 2
del is a python statement, which can a) delete a variable, i.e. >>> a = 'my_variable' >>> a 'my_variable' >>> del a >>> a Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> a NameError: name 'a' is not defined Usually you don't need to do this implicitly, as there is garbage collector in python. b) delete an element from the list or dictionary. It can also delete a slice of elements from the list >>> a = [1, 3, 5, 10, 7, 0, -2] >>> del a[2] >>> a [1, 3, 10, 7, 0, -2] >>> del a[1:4] >>> a [1, 0, -2] >>> del a[-2:] >>> a [1] >>> a = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5} >>> del a['b'] >>> a {'a': 1, 'c': 3, 'e': 5, 'd': 4} >>> for i in 'ced': del a[i] >>> a {'a': 1} By the way, in your code if 2==3: del(a) # This never happens, as 2==3 is False
19th Nov 2016, 9:16 AM
donkeyhot
donkeyhot - avatar
0
what is the 'del' function supposed to do exactly?
18th Nov 2016, 7:44 PM
Rill Chritty
Rill Chritty - avatar
0
you have to declare a 'del' function before it can be used
18th Nov 2016, 7:45 PM
Rill Chritty
Rill Chritty - avatar
0
To delete a line
18th Nov 2016, 7:57 PM
Lukas Bolevičius
Lukas Bolevičius - avatar
0
Understand now :)
19th Nov 2016, 9:33 AM
Lukas Bolevičius
Lukas Bolevičius - avatar