Do you want to learn python from me with a lot of interactive quizzes, and exercises? Here is my project-based python learning course: codebasics.io/courses/python-for-beginner-and-intermediate-learners
Great explanation, thanks for this video. Note- As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
Here is the link of exercise along with the solution, please work on it as it will make your learning solid: github.com/codebasics/py/blob/master/Basics/Exercise/11_dict_tuples/11_dict_tuple_exercise.md
sorry to disturb you sir but at the time stamp 4:54 there's an runtime error as the key is repeating but the values are changing can you please share the code to solve that because I tried so hard but I'm a beginner so I'm not able to solve
okay sir problem is solved you wrote for k,v in d.items(): print("Key:",key,"Value:",v) instead of for k,v in d.items(): print("Key:",k,"Value:",v) special thanks to you sir because you're the reason why I understood this
@codebasics : for k, v in d.items() print ('key :',key,"Value:",v) output is showing Key = tom for all 3 items in the dictionary. Looks like in the print statement you should use k instead of key
Can we replace the word "key" in "for key in d:" with anything, like say "n", followed by "print(n)"? Will it still work? What I'm trying to figure out is if the word "key" is a reserved word, or is it simply a placeholder for any variable name?
while using tuple case it is printing same person name repeatedly only the contact number changes, Even if in this video of yours i noticed the same thing. video time: (4:45)
Sir, in other sources I read that, since the release of python 3.7 (June 2018) dictionaries are ordered, and even you can call them by their index number. But in this tutorial @ 2:45 you said "for dictionaries order doesn't matter" so I am kind of confused. Thank you in advance.
Any key of the dictionary is associated to a value. The values of a dictionary can be any type of Python data. that's why dictionaries are unordered key-value-pairs.
I can't run this - '''Country Population China 143 India 136 USA 32 Pakistan 21''' d = {"china:143", "India:136", "USA:32", "Pakistan:21"} print(d) for key in d: print("key:",key, "value:",d[key]) it is showing an error continuously - "TypeError: 'set' object is not subscriptable please tell me what is wrong?
In list we can access the elements by using an index value. In dictionaries using the key value we can access the element.we can predict the index value here by our own.so that we can easily get the relevant information..
One of the difference is that lists are organized data structures whereas dictionaries are unordered, so in lists you can index using the index number associated with the item where in dictionary use have to use the key to do so.
lists store data order wise and dictionaries store data as key value pairs as order is not mattered in dictionaries. I agree with @Elanchezhiyan E and @Siddhartha Roy
can you please explain the exercise you gave def age_dictionary(): ''' Exercise 1 This program asks for person name and age and builds a dictionary using that Later on you can input person name and it will tell you the age of that person :return: ''' d = {} while True: person = input("Enter person name, to stop don't enter anything and hit Enter key:") if person == '': break age = input("Enter age:") d[person] = age print(“Now enter name of the person and I'll tell you his/her age") while True: name = input("Enter person name, to stop don't enter anything and hit Enter key:") if name == '': break if name in d: print ("Age of", name, "is:", d[name]) else: print ("I don't know the age of",name) print ("Age dictionary program is finished now")
Presentation is pretty good. I was looking into it to refer python videos to my nephew. Overall composition is pretty elegant. But a small critical comment. You have picked a bad example for dictionaries, telephone directory. You can have multiple entries with same first and lat name. You cannot have collisions in the keys. You will then be dependent on the address to resolve the first collision. But you can still have collisions with same name entries for a given address. IMHO, you may need to redo this video. Also, need to address the limitations of size of the key. Your performance is dependent on the hashing and the lookup based on number of buckets.
many of exercises doesnt go together with what your teaching in the videos , in the beginning it was relatively fine but one of the example can be. in this video you talk very basic about dictionaries and tuples, but your exercises specially number 2 and number 3 are way above of what you teaching in this or previous videos. i cant even solve the exercise because we didnt even learn import math or import statistics and how to write code with them , but your exercise solutions are based on them. also you teaching in python shell and exercise solutions is in python . as beginner this is confusing
Please don't bring anything in the exercises which needs to be taught in future classes like importing modules like statistics, "__main" etc., it really disturb the flow of the learning cycle.
Do you want to learn python from me with a lot of interactive quizzes, and exercises? Here is my project-based python learning course: codebasics.io/courses/python-for-beginner-and-intermediate-learners
Great explanation, thanks for this video.
Note- As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
Here is the link of exercise along with the solution, please work on it as it will make your learning solid: github.com/codebasics/py/blob/master/Basics/Exercise/11_dict_tuples/11_dict_tuple_exercise.md
Teacher the link its broken, it says that page haven't been found, please upload it again if you can, i was in the second excercise
sorry to disturb you sir but at the time stamp 4:54 there's an runtime error as the key is repeating but the values are changing can you please share the code to solve that because I tried so hard but I'm a beginner so I'm not able to solve
okay sir problem is solved you wrote
for k,v in d.items():
print("Key:",key,"Value:",v)
instead of
for k,v in d.items():
print("Key:",k,"Value:",v)
special thanks to you sir because you're the reason why I understood this
anybody knows how to access these exercises? Page not found error is there please fix it.
@@sukanyachoudhury6174 I have the same problem since few days
Thanks Mota bhai, First time I am able to see proper particle explanation of Dictionary, list and Tuple.
Finally i understood the difference between a List and Tuple, thank u so much!!
I am happy this was helpful to you.
UPDATE - DICTIONARY IS NOW ORDERED AS PER PYTHON 3.7
4:36 You forgot to change key with k in the print statement! Btw great explanation ♥
print ('key :',key,"Value:",v)
NameError: name 'key' is not defined
@@salimbrahmi9789 key was defined from the previous for loop, it was left at "tom" which is why it got printed 3 times
Thank you Sir for the exercises, as they cover all the concepts taught till now especially the use of conditional statements!!
Your tutorials are simply awesome!!!
@codebasics : for k, v in d.items() print ('key :',key,"Value:",v) output is showing Key = tom for all 3 items in the dictionary. Looks like in the print statement you should use k instead of key
Yaa it's true
In that print statement instead of key we need to type k👍
Noticed this too...thanks and more power to this channel...
print ('key :',key,"Value:",v)
NameError: name 'key' is not defined
Then how sir got output without error???
@@chinnyruchitha3181 I think because he is using a shell, that is why last key repeated.
sir you are great i love your simplicity and way of teaching too.
Prakhar, thanks for appreciation and feedback
same here...
Learn data science with python and pandas: ruclips.net/video/CmorAWRsCAw/видео.html
Please correct a mistake at 4:35 in printing key value by using variable "key" instead of "k".
due to use of functions in this, understood many idea of coding and logic..
thank u sir...
Thank you, this is very helpful. Respect to you
@ 4:50 it prints only tom as key...why?..it is supposed print rob and joe also right?!
Replace key with k in print line
4:40 here k,v is used but inside print Key and v is used, so the key is from the previous for loop ??
Can we replace the word "key" in "for key in d:" with anything, like say "n", followed by "print(n)"? Will it still work? What I'm trying to figure out is if the word "key" is a reserved word, or is it simply a placeholder for any variable name?
4:35 What is the 'k' used for in the tuple? Was it meant to take the place of the key variable?
ghost yes he just forgot to change it ;)
how come the output is shown...I have tried it ..shows errror!
while using tuple case it is printing same person name repeatedly only the contact number changes, Even if in this video of yours i noticed the same thing. video time: (4:45)
sir can you explain defference between dictionaries and tuples in more detailes?
The exercise given is so tough! I have no idea what all concepts have been used in the country and population problem
use chatgpt and ask him to explain every line
where are the exercises u mentioned in the videos?????
Sir, in other sources I read that, since the release of python 3.7 (June 2018) dictionaries are ordered, and even you can call them by their index number. But in this tutorial @ 2:45 you said "for dictionaries order doesn't matter" so I am kind of confused. Thank you in advance.
Any key of the dictionary is associated to a value. The values of a dictionary can be any type of Python data. that's why dictionaries are unordered key-value-pairs.
Very good 👍👌
What is the type of phone number? String or integer?
At 4:38, the code is wrong, we are assigning k and v for key and value so need to use the same variables. You've used "key": key , "value": v
Thanks. I have added this correction in video description.
@@codebasics Thank you. I am revising my python concepts again and your way of teaching is too good. Thanks again
The tuple example is wrong. It should be ↙
for k,v in d.items():
print("key:",k,"value:",v)
Yes. He made a mistake there.
One question, what's the difference between d = {} and d.clear()?
Is .clear() better for memory?
d.clear() is more memory efficient as it doesn't create a new instance of d, it simply clears the already existing d variable.
Do you meant that List cannot be created with both strings and numbers ???
You can do that
I can't run this - '''Country Population
China 143
India 136
USA 32
Pakistan 21'''
d = {"china:143", "India:136", "USA:32", "Pakistan:21"}
print(d)
for key in d:
print("key:",key, "value:",d[key])
it is showing an error continuously - "TypeError: 'set' object is not subscriptable
please tell me what is wrong?
d= {"china":143, "India":136, "USA":32, "Pakistan":21}
print(d)
what is the difference between lists and dictionaries?
In list we can access the elements by using an index value.
In dictionaries using the key value we can access the element.we can predict the index value here by our own.so that we can easily get the relevant information..
One of the difference is that lists are organized data structures whereas dictionaries are unordered, so in lists you can index using the index number associated with the item where in dictionary use have to use the key to do so.
lists store data order wise and dictionaries store data as key value pairs as order is not mattered in dictionaries. I agree with @Elanchezhiyan E and @Siddhartha Roy
Thank you so much...
You're most welcome
This doesn't work on Python 3.11 😢
hI sir i have one doubt , how add new value key pair in dictionary
Dict ['key']=val
sir, list is also hetrogenious
can you please explain the exercise you gave
def age_dictionary():
'''
Exercise 1
This program asks for person name and age and builds a dictionary using that
Later on you can input person name and it will tell you the age of that person
:return:
'''
d = {}
while True:
person = input("Enter person name, to stop don't enter anything and hit Enter key:")
if person == '':
break
age = input("Enter age:")
d[person] = age
print(“Now enter name of the person and I'll tell you his/her age")
while True:
name = input("Enter person name, to stop don't enter anything and hit Enter key:")
if name == '':
break
if name in d:
print ("Age of", name, "is:", d[name])
else:
print ("I don't know the age of",name)
print ("Age dictionary program is finished now")
i thnk u did a mistake at 4:38 see the output
use k instead of key
Presentation is pretty good. I was looking into it to refer python videos to my nephew. Overall composition is pretty elegant. But a small critical comment. You have picked a bad example for dictionaries, telephone directory. You can have multiple entries with same first and lat name. You cannot have collisions in the keys. You will then be dependent on the address to resolve the first collision. But you can still have collisions with same name entries for a given address.
IMHO, you may need to redo this video. Also, need to address the limitations of size of the key. Your performance is dependent on the hashing and the lookup based on number of buckets.
Could the collision issue with the example picked be resolved in some way? Or maybe a better example could have been picked for the explanation?
Why all instructors smash ENTER and SPACE buttons so hard 😂..... By the way nice content 🔥👌
I am going to use NVIDIA broadbast going forward. you will not hear any other voice other then my speech :)
@@codebasicsactually its pleasing to hear the keyboard
many of exercises doesnt go together with what your teaching in the videos , in the beginning it was relatively fine but one of the example can be. in this video you talk very basic about dictionaries and tuples, but your exercises specially number 2 and number 3 are way above of what you teaching in this or previous videos. i cant even solve the exercise because we didnt even learn import math or import statistics and how to write code with them , but your exercise solutions are based on them. also you teaching in python shell and exercise solutions is in python . as beginner this is confusing
Please don't bring anything in the exercises which needs to be taught in future classes like importing modules like statistics, "__main" etc., it really disturb the flow of the learning cycle.
I see. point noted. Thanks for the feedback Adil.
True, loosing the flow already after coming to the main part in exercise :( skipping it for now