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
      • 7
      • 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
      • 16
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19

  • 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.',
    # {'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,
    # {'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.


    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:
    Thank you
    How are you?
    Comment ca va?
    Hello everyone
    Bonjour à tous
    This is delicious
    C'est délicieux
    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.
    How are you?
    Comment ca va?
    To check your solution you need to sign in
    Sign in to continue