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

  • Set unions and intersections

    Video preview
    Sets in Python are similar to sets in math in many senses. They are defined in the say way - with curly brackets. One can perform set union, intersection, or subtraction on sets. Intersection of a set X and Y can be done with & in Python. Union of set X and Y can be done with |. And subtraction can be done with a simple - operator:
    x = {3, 12, 5, 13}
    y = {14, 15, 6, 3}
    print(x, y)          # {5, 3, 12, 13} {3, 15, 6, 14}
    print(x & y)         # {3}
    print(x | y)         # {3, 5, 6, 12, 13, 14, 15}
    print(x - y)         # {13, 12, 5}
    • The intersection of two sets includes all the elements that are present in both sets. So, in Python, it’s denoted as an “and” & operator (elements Both in A and B).
    • The union of two sets includes all the elements of both sets. So, in Python, it’s denoted as an “or” | operator (elements either in A or B).
    • The subtraction of two sets includes all the elements that are present in set A but not present in B. So, in Python, it’s denoted as a “subtraction” - operator (elements in A but not in B).


    Imagine that you are a teacher in a class where every group creates several presentations during a year for different topics. There are n groups of students and each group creates one or more presentations during a year. You are interested in 2 numbers. How many different topics have been covered during a year and how many topics have been presented by ALL the groups.
    The first line of the input contains a single integer n. The next n lines contain the topics that each group presented separated by a comma and a space , .
    The program should print two numbers separated by a space. The first number should indicate the number of topics presented in total and the second number should indicate how many topics have been covered by every group in the class.
    4 Neural Networks, Deep Learning, Machine Learning, AI AI, Data Science, Sports Analytics, Deep Learning Deep Learning, AI, Automation of jobs with AI AI, Scientific thinking, Recent advances in NLP, Deep Learning
    9 2
    1. There are 9 unique topics covered: Deep Learning, Machine Learning, Neural Networks, Data Science, Sports Analytics, Scientific thinking, Automation of jobs with AI, Recent advances in NLP, AI
    1. There are 2 topics covered by all the groups: Deep Learning, AI
    To check your solution you need to sign in
    Sign in to continue