Python dictionaries are easy 📙

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 พ.ย. 2022
  • #python #tutorial #course
    dictionary = a collection of {key:value} pairs
    ordered and changeable. No duplicates
    capitals = {"USA": "Washington D.C.",
    "India": "New Delhi",
    "China": "Beijing",
    "Russia": "Moscow"}
    print(dir(capitals))
    print(help(capitals))
    print(capitals.get("Japan"))
    if capitals.get("Russia"):
    print("That capital exists")
    else:
    print("That capital doesn't exist")
    capitals.update({"Germany": "Berlin"})
    capitals.update({"USA": "Detroit"})
    capitals.pop("China")
    capitals.popitem()
    capitals.clear()
    keys = capitals.keys()
    for key in capitals.keys():
    print(key)
    values = capitals.values()
    for value in capitals.values():
    print(value)
    items = capitals.items()
    for key, value in capitals.items():
    print(f"{key}: {value}")

ความคิดเห็น • 107

  • @BroCodez
    @BroCodez  ปีที่แล้ว +62

    # dictionary = a collection of {key:value} pairs
    # ordered and changeable. No duplicates
    capitals = {"USA": "Washington D.C.",
    "India": "New Delhi",
    "China": "Beijing",
    "Russia": "Moscow"}
    # print(dir(capitals))
    # print(help(capitals))
    # print(capitals.get("Japan"))
    # if capitals.get("Russia"):
    # print("That capital exists")
    # else:
    # print("That capital doesn't exist")
    # capitals.update({"Germany": "Berlin"})
    # capitals.update({"USA": "Detroit"})
    # capitals.pop("China")
    # capitals.popitem()
    # capitals.clear()
    # keys = capitals.keys()
    # for key in capitals.keys():
    # print(key)
    # values = capitals.values()
    # for value in capitals.values():
    # print(value)
    # items = capitals.items()
    # for key, value in capitals.items():
    # print(f"{key}: {value}")

    • @yugissina7461
      @yugissina7461 11 หลายเดือนก่อน +1

      bro made the spaces perfectly :>)

  • @li249
    @li249 10 หลายเดือนก่อน +47

    This is the only course I can actually understand at this stage. Other courses say for "beginners"... well... maybe for someone else who is clever enough as a beginner to take those courses...

    • @emmanuelleallen
      @emmanuelleallen 4 หลายเดือนก่อน +5

      Nahhh, it's not you. I'm usually top of my class, and a lot of other "begginner" rated courses through TH-cam are taught by people who don't know how to teach effectively, or produce content that teaches rather than entertains. That, and a lot of others will also assume you know a lot of other things that are common to other programming languages.
      I once saw a video labeled "for absolute beginners" and it was the most confusing thing I saw.

  • @benjaminjohnpabroquez8012
    @benjaminjohnpabroquez8012 ปีที่แล้ว +15

    menu = {
    "Burger": 40,
    "Hotdog": 30,
    "Pizza": 180,
    "Chicken": 87
    }
    print("Menu:")
    for item, price in menu.items():
    print(f"{item} - {price}")
    orders = []
    while True:
    item = input("What food would you like? ").capitalize()
    if item.lower() == "done":
    break
    elif item not in menu:
    print("Invalid item.")
    continue
    else:
    quantity = int(input(f"How many {item}s do you want? "))
    if quantity > 0:
    orders.append((item, quantity))
    print("You ordered:")
    for order in orders:
    print(f"{order[0]} x {order[1]}")
    total = sum([menu[order[0]] * order[1] for order in orders])
    print(f"The total amount of food is {total}pessos")

  • @Canxxxxxc
    @Canxxxxxc ปีที่แล้ว +9

    Thank you for explaining this topic so thoroughly and preemptively answerinf follow ups one may have. This is how teaching should be

  • @gifty2595
    @gifty2595 ปีที่แล้ว +17

    You made this subject so interesting. Excellent teacher

  • @mengekyosyaringanKamui
    @mengekyosyaringanKamui ปีที่แล้ว +11

    One of the best explaination of python dictionaries
    Thanks bro

  • @josueramirez7247
    @josueramirez7247 7 หลายเดือนก่อน +5

    2:27 Also, there is a second optional argument for the get() method where you can provide a value to return if the specified key does not exist. If you don’t give get() the second argument, you will just get `None` which is a special way of saying that there isn’t a value yet.

  • @irvinpalacios3513
    @irvinpalacios3513 ปีที่แล้ว +38

    Definition 0:07
    Example 0:20
    .get method: 1:55
    .update method 3:23
    .keys method 4:55
    .values method 5:58

  • @harikishore7472
    @harikishore7472 16 วันที่ผ่านมา

    Man, your teaching skills are so awesome. Thanks for helping us

  • @crusty711
    @crusty711 8 หลายเดือนก่อน +2

    This was very helpful. Thank you

  • @khushimurnal6880
    @khushimurnal6880 ปีที่แล้ว +9

    You are legend

  • @y0ake77
    @y0ake77 5 หลายเดือนก่อน

    The best python tutor for me, I love your easy and fun teaching style❤

  • @althairgroup9837
    @althairgroup9837 ปีที่แล้ว +1

    You seriously got a talent to teach

  • @mohamad_subhi
    @mohamad_subhi 3 หลายเดือนก่อน

    We certainly appreciate your efforts 😍

  • @lionellindley9056
    @lionellindley9056 3 หลายเดือนก่อน

    finally an explanation I understand completely and answered all of my issues, brilliant !!

  • @Debelinka
    @Debelinka 10 หลายเดือนก่อน

    You make it so easy

  • @auoy_
    @auoy_ 3 หลายเดือนก่อน

    For those people who are greedy with their self but you the are who really help us so many are privileged and innocent people's are here those who don't have money or own laptop these type people's are also here a huge respect to you brother 😢 you are helping us you are the teacher of us - thanks to you bro 💖

  • @thinukawijerathne
    @thinukawijerathne 13 วันที่ผ่านมา

    Thank you!

  • @user-hq5im2is8l
    @user-hq5im2is8l 5 หลายเดือนก่อน

    This video was soooooo helpfull

  • @shreyasshetty9538
    @shreyasshetty9538 หลายเดือนก่อน

    great video ! helped a lot

  • @AlaeGDeveloper
    @AlaeGDeveloper 2 หลายเดือนก่อน

    Thank you so much

  • @iamfkhn
    @iamfkhn ปีที่แล้ว +3

    Dope content.

  • @pylejjy6279
    @pylejjy6279 ปีที่แล้ว +8

    helps remembering after reading a book. tysm!

  • @CRXYTV
    @CRXYTV ปีที่แล้ว +2

    Yeah this actually helped alot

  • @user-gw2vz5gh2n
    @user-gw2vz5gh2n 11 หลายเดือนก่อน

    your voice is smooth and attractive i could see you working as a narrator

  • @ControlDialedin
    @ControlDialedin หลายเดือนก่อน

    This is great

  • @pentarronitoentuanuto4081
    @pentarronitoentuanuto4081 8 หลายเดือนก่อน

    god bless this

  • @RCASoft
    @RCASoft 21 วันที่ผ่านมา

    Very clear

  • @gaddafisuleiman8882
    @gaddafisuleiman8882 ปีที่แล้ว +4

    You're awesome, bro! 😎

  • @alejandrocarrillo2282
    @alejandrocarrillo2282 หลายเดือนก่อน

    Thanks g

  • @kidfromforeign8063
    @kidfromforeign8063 8 หลายเดือนก่อน

    Very good bro! You’ve just earn a subscriber

  • @Maheshmahi-yc8yk
    @Maheshmahi-yc8yk ปีที่แล้ว

    Thanks big bro

  • @mariannaark5899
    @mariannaark5899 ปีที่แล้ว +7

    Hello, thanks for the helpful video! I have a question, you kept mentioning something along the lines of "it returns an object resembling a list" but also as you both stated and demonstrated this object is iterable. Are there situations where the difference will matter?

  • @VixxieWixxie
    @VixxieWixxie 6 หลายเดือนก่อน

    Great video as alwys. For me especially the dir() and help(). I didn't know they existed. What would a dictonairy mostly be used for and how?

  • @wxua5859
    @wxua5859 10 หลายเดือนก่อน

    thanks bro

  • @technicalresi5451
    @technicalresi5451 6 หลายเดือนก่อน

    Mast h dekh watch this video from India 🇮🇳🥰
    Bro you are great programer 😊

  • @ConnorBraun-bz9hj
    @ConnorBraun-bz9hj 11 วันที่ผ่านมา

    I love you Bro Code

  • @Zer_O._Juan
    @Zer_O._Juan หลายเดือนก่อน

    I'm creating a command line rpg level up on python, I use a list for a lot of stuff especially on the saving mechanics, should I use dictionary instead? can I use dictionaries for hp and energy?

  • @GetgoB
    @GetgoB 7 หลายเดือนก่อน

    thank you Bro Code

  • @QuarktaschemitSenf
    @QuarktaschemitSenf 7 หลายเดือนก่อน

    quick question: i also get the value if i just use dict[key] like capitals["USA"]. What is the benefit of using the get method?. ok just figured out the benefit of the get method. it returns none instead of throwing a key error.
    but whats the benefit of dict.update?
    I can just use capitals["France"] = "Paris"

  • @tfbrooks1668
    @tfbrooks1668 ปีที่แล้ว

    Holy crap that dir function

  • @stevendumont4156
    @stevendumont4156 11 หลายเดือนก่อน

    Bro, superb instructions of dictionaries and advance topics for dummies. Love how you teach, suscribed--bell rung, I'll be back. Thank you!

  • @janurek3050
    @janurek3050 27 วันที่ผ่านมา

    I thought that dicts arent ordered because i cannot use position idexes to get some pair, key or value?

  • @kristijanlazarev
    @kristijanlazarev 3 หลายเดือนก่อน

    godlike

  • @mid___
    @mid___ 6 หลายเดือนก่อน +1

    as a person who has an exam tommorow about this, thank you

  • @Harsh.kr.20
    @Harsh.kr.20 4 หลายเดือนก่อน

    4:17 That was brutallll...!!😂

  • @t.a-8469
    @t.a-8469 3 หลายเดือนก่อน

    Duck yes bro! Duck yes! 🙌

  • @eviltom4738
    @eviltom4738 ปีที่แล้ว +2

    Gigachad.

  • @kkgotek9961
    @kkgotek9961 5 หลายเดือนก่อน

    thank you good sir , tomorrow is my exam and i shall be passing thanks to you.

  • @sniperos8546
    @sniperos8546 ปีที่แล้ว +2

    what can i say ? it was really easy , a like from me

  • @Grab-Deals
    @Grab-Deals 6 หลายเดือนก่อน

    hello, please tell me how to make my display on the editor look like yours

  • @PrajnaShetty...
    @PrajnaShetty... หลายเดือนก่อน +1

    But most of the resources says that dictionary is unordered

  • @celestepozzi9204
    @celestepozzi9204 10 หลายเดือนก่อน

    when i type in all small letter, it says it doesnt exist. how can i change it, thanks

  • @zdlyrics_01
    @zdlyrics_01 9 หลายเดือนก่อน +1

    This title is kinda pretencious right now, but in a few years you and many computer science communicators will make it gain worth. I appreciate your labor here, shall many enthusiastics like me come to your channel and get their doubts cleared up the same way mines did just tonight. Regards from venezuela

  • @silverplays8592
    @silverplays8592 ปีที่แล้ว +1

    will you do a golang tutorial?

  • @pancakewhale
    @pancakewhale หลายเดือนก่อน

    Giga chad

  • @makoblox
    @makoblox ปีที่แล้ว +1

    ye

  • @healthiswealth6323
    @healthiswealth6323 2 หลายเดือนก่อน

    "No thats herp"
    We definitely don't want that 👀

  • @DearHRS
    @DearHRS ปีที่แล้ว +7

    are dictionaries similar to hashmaps?

    • @godgoatdog39
      @godgoatdog39 ปีที่แล้ว +9

      yup, they are basically the same thing, named differently because theyre from different languages

  • @soulsever_IND
    @soulsever_IND 4 หลายเดือนก่อน

    Me watching this video for my semester finals that gonna be happen in 2 more hours

  • @timmyl9361
    @timmyl9361 ปีที่แล้ว

    Why does popitem(last item) work if a dict is unordered?

    • @josueramirez7247
      @josueramirez7247 7 หลายเดือนก่อน +1

      It’s not unordered though. It will remove and return the last key-value pair that was inserted.

  • @stephen285
    @stephen285 ปีที่แล้ว +1

    Thanks for your video! can you help me I'm stupid? you don't have to dive into the code the 2nd to last line will print the dictionary so you can see it works It's just the last line that doesn't work like yours does? Ive tried double quotes, single, I've tried making with all strings and a digit/string. You can just run it and see the dictionary creates and is there, BUT why doesnt the last line work??
    i = 21 # 21 is first key on left side of piano low A
    i = int(i) # i is an integer
    x = 21 + 88 # 109 is last key on right
    y = 0 # y is the index number of each of the 12 notes starting from A moving right
    o = 0 # o is index of octave so it start at - "A0", "A#0/Bb0" ..... "A1", "A#1/Bb1 ... etc..."
    y = int(y)
    notes = ["A", "A#/Bb", "B", "C", "C#/Db", "D", "D#/Eb", "E", "F","F#/Gb", "G","G#/Ab"]
    while i < x:
    notename = (notes[y])
    oct = str(o)
    cnct =(notename) + (oct) #concatenate notename and octave as string
    istr = str(i)
    numNote = {istr: cnct} # create first dictionary number is the key, note name is value
    noteNum = {cnct: istr} # create second dictionary reversed
    y = y + 1

    if y == 12: #after you go thru the 12 notes
    y = 0 #set y back to 0 and start the next 12 notes
    o = o + 1 #after the first 12 notes advance the octave 1

    i = i + 1 #iterate thru keys 21 - 109

    print(numNote)
    print(numNote.get("102"))

    • @caballerojedi2285
      @caballerojedi2285 ปีที่แล้ว +1

      It was a bit difficult but I have just solved it:
      i = 21 # 21 is first key on left side of piano low A
      x = i + 88 # 109 is last key on right
      y = 0 # y is the index number of each of the 12 notes starting from A moving right
      notes = ["A", "A#/Bb", "B", "C", "C#/Db", "D", "D#/Eb", "E", "F","F#/Gb", "G","G#/Ab"]
      a1_dict = {}
      while i x:
      break
      else:
      pass
      print(a1_dict)

    • @caballerojedi2285
      @caballerojedi2285 ปีที่แล้ว +1

      I see that your program creates a dictionary for every single pair of key : note, and I consider it unnecesarry, since dictionaries can hold a lot of key : note pairs.

  • @jeromes2767
    @jeromes2767 ปีที่แล้ว

    3:30

  • @iconic_patel
    @iconic_patel 4 หลายเดือนก่อน

    BroCode, i come frequently and see the dir() function because i forget the name of the dir() function😂😂

  • @itsswims9391
    @itsswims9391 25 วันที่ผ่านมา

    ay i was the 5000th like on this does that mean ill pass the exam tmrw

  • @blessingtshuma403
    @blessingtshuma403 8 หลายเดือนก่อน

    Almost typed herpes there

  • @angajames
    @angajames ปีที่แล้ว

    Trying to let my dumbo mind work with coding. Its tough!

  • @La.vita.
    @La.vita. 4 หลายเดือนก่อน

    How login and password work in website. Please help me about it😢

    • @AyuPlus
      @AyuPlus 2 หลายเดือนก่อน +1

      It works using html. Password is an input type in html. See a good video about login pages 😃

    • @AyuPlus
      @AyuPlus 2 หลายเดือนก่อน +1

      I could have helped but you need a good teacher

  • @user-nc1qz4ku9n
    @user-nc1qz4ku9n ปีที่แล้ว

    bonsoir hugo...

  • @LimeSpeedCrystal
    @LimeSpeedCrystal ปีที่แล้ว +1

    I am from Russia.

    • @skingleyjoseph5402
      @skingleyjoseph5402 ปีที่แล้ว +6

      Nice! happy learning

    • @LimeSpeedCrystal
      @LimeSpeedCrystal ปีที่แล้ว

      @@skingleyjoseph5402 Thanks, I made a cool calculator by a video, it is gonna be so cool!

    • @Dima-wu8pe
      @Dima-wu8pe 10 หลายเดือนก่อน

      A terrorist left here!

  • @Arsenal2000
    @Arsenal2000 5 หลายเดือนก่อน

    thx bro i love you

  • @hydrogenneon
    @hydrogenneon 5 หลายเดือนก่อน +1

    Indian with an american accent

    • @hoons6949
      @hoons6949 4 หลายเดือนก่อน

      I get that lol

  • @paisz3299
    @paisz3299 ปีที่แล้ว

    I have my cs exam soon Im just re watching all your vids cause my professor useless af.

  • @user-nc1qz4ku9n
    @user-nc1qz4ku9n ปีที่แล้ว

    hugo, répond à ce commentaire veux tu!

    • @couz-kv2ri
      @couz-kv2ri ปีที่แล้ว

      j'y comprend plus rien moi

    • @user-nc1qz4ku9n
      @user-nc1qz4ku9n ปีที่แล้ว

      In this exercise, you are asked to fill in the function top_grade(). • This function receives a list of dictionaries. All the dictionaries in the list will have the same keys - “name” and “grades”.
      • We want to know, for each student, what is their best grade and in which subject.
      • So, the function should return a new list with the same number of dictionaries as the original.
      • However, instead of the “grades” key, it should have the “best grade” key, whose value should be a dictionary with the key being the subject name and its value the maximum value of the “grades” key on the original dictionary.
      See the example below:
      example = [{'name': 'John', 'grades': {'Math': 80, 'Algebra': 76, 'Economics': 82}}, {'name': 'Max', 'grades': {'Math': 85, 'Algebra': 90, 'Economics': 88}}, {'name': 'Zygmund', 'grades': {'Math': 80, 'Algebra': 76, 'Economics': 82}}]
      top_grade(example) [{'name': 'John', 'best grade': {'Economics': 82}}, {'name': 'Max', 'best grade': {'Algebra': 90}},
      {'name': 'Zygmund', 'best grade': {'Economics': 82}}]

    • @couz-kv2ri
      @couz-kv2ri ปีที่แล้ว

      @@user-nc1qz4ku9n def top_grade(students):
      top_grades = []
      for student in students:
      name = student['name']
      grades = student['grades']
      best_subject = max(grades, key=grades.get)
      best_grade = grades[best_subject]
      top_grades.append({'name': name, 'best grade': {best_subject: best_grade}})
      return top_grades

    • @ludovikthorens4743
      @ludovikthorens4743 ปีที่แล้ว

      @@couz-kv2ri Complete the custom_operator() function, which takes a list of integer numbers as input. The
      function should return a new list of the same length, following these rules:
      The first and last numbers in the output list should be double the original values.
      For the remaining numbers in the output list, calculate each value by multiplying the original
      list of numbers (excluding the number at the current index) by the number at the current
      index, and then summing all the results.
      Here's an example to illustrate the desired transformation:
      Example:
      [1,2,3,4,5,6,7]
      Output:
      [2, 52, 75, 96, 115, 132, 14]

    • @couz-kv2ri
      @couz-kv2ri ปีที่แล้ว

      @@ludovikthorens4743 j'ai changé, plus visuel comme ça :
      def custom_operator(lst):
      n = len(lst)
      if n == 0:
      return lst
      res = [0]*n
      # Double the first and last elements
      res[0], res[-1] = 2*lst[0], 2*lst[-1]
      # Compute the result for the remaining elements
      for i in range(1, n-1):
      sum_val = 0
      for j in range(n):
      if j != i:
      sum_val += lst[j]*lst[i]
      res[i] = sum_val
      return res

  • @jourdanfarmer
    @jourdanfarmer 6 หลายเดือนก่อน

    how come during the section @3:09 you type "Japan" into the parameter and get a true//false result? I would expect that typing in "Japan" there would mean that "Japan" would become the desired argument for the if/else statement.

  • @blap5630
    @blap5630 6 หลายเดือนก่อน +3

    Thanks Bro hopefully gonna go get a C on my quiz instead of a D 🫡

  • @veniciostenorio1
    @veniciostenorio1 25 วันที่ผ่านมา

    Thanks, bro. You really helped me