fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • dict

    Python has 4 basic inbuilt data structures: Lists, Tuples, Sets, and Dictionaries.
    Lists, tuples, and sets are used to keep single elements in collections. Dictionaries, on the other hand, are used to keep key-value pairs. Similar to real-world dictionaries, where each word corresponds to its translation or explanation, Python dictionaries use keys to map them to their corresponding values. We can keep a dictionary of countries and their capitals:
    capitals = {
    	'Armenia': 'Yerevan',
    	'Australia': 'Canberra',
    	'Austria': 'Vienna',
    	'Brazil': 'Brasilia',
    	'United States': 'Washington D.C.',
    }
    print(capitals)
    # {'Armenia': 'Yerevan', 'Australia': 'Canberra', 'Austria': 'Vienna', 'Brazil': 'Brasilia', 'United States': 'Washington D.C.'}
    To define a dictionary, we first open a curly bracket {, then put each key on the left side of the colon :, and the value on the right side. Each key-value pair is separated by a comma, and the dictionary definition ends with a closing curly bracket }.
    Both keys and values can be of different types. We can keep the city population in a dictionary:
    population = {
    	'Yerevan': '1M',
    	'Canberra': 395790,
    	'Vienna': '1.897 million',
    	'Brasilia': 4804000,
    	'Washington D.C.': 692683,
    }
    print(population)
    # {'Yerevan': '1M', 'Canberra': 395790, 'Vienna': '1.897 million', 'Brasilia': 4804000, 'Washington D.C.': 692683}
    In the case of lists and tuples, we could access the individual elements by their index [2]. When working with dictionaries, the access to elements is done through keys:
    print(population['Yerevan'])    # 1M
    print(population['Brasilia'])   # 4804000
    print(population['New York'])   # KeyError: 'New York'
    print(population[0])            # KeyError: 0
    If the key does not exist in the dictionary, Python raises an error telling that the key is not in the dictionary.

    Challenge

    Google translate is great but can you create a simpler version of it?
    You are helping your friend to study the French class. He is an English speaker, so you want to write a program for him that given an English word or a phrase would print the corresponding phrase in French. Below are the phrases that he’s currently studying:
    English
    French
    Thank you
    Merci
    How are you?
    Comment ca va?
    Hello everyone
    Bonjour à tous
    This is delicious
    C'est délicieux
    amazing
    étonnante
    tasty
    savoureux
    Note: Don’t use if/else statements. It’s much shorter and easier to do this with dictionaries.
    The input contains a single line - the phrase in English.
    The program should print the corresponding French sentence.
    Input
    Output
    How are you?
    Comment ca va?
    tasty
    savoureux
     
    To check your solution you need to sign in
    Sign in to continue