Placeholder Image

字幕表 動画を再生する

  • Python comes equipped with different objects to help you organize your data. These data

  • structures include lists, tuples, sets and dictionaries. Today we talk about sets. Sets

  • are useful when you are working with data and the order or frequency of the values do

  • not matter. Get ready to become an element of the set of people who understand sets

  • We will begin by creating an empty set calledexample”.

  • To see a list of methods you can use on a set, use the directory function on our set.

  • We want to add things to our set, and theaddfunction looks promising. To see

  • how to use this method, use the help function.

  • The help text provides a reminder of the definition of a set. Duplicates are not stored. If you

  • try to add the same element twice, the set will store it the first time, and ignore it

  • the second time.

  • We will use this method to add several objects to this set.

  • The integer 42…

  • The boolean value False

  • The number Pi as a float

  • The stringthorium”.

  • Notice that you can add data of different types to the same set.

  • If you enter the name of the set and press enter, Python will show you the items inside

  • the set. Each item inside a set is called anelement”.

  • When you try this, the elements may appear in a different order for you than what is

  • displayed here. Do not panic. For sets, the order does not matter. This is different from

  • lists and tuples, where the order DOES matter.

  • Now look what happens if you try to add the

  • number 42 to the set a second timeThe set still contains just one copy of the number

  • 42. Sets do not contain duplicate elements.

  • To see the number of elements in a set, use the length function, which is shortened to

  • L-E-N… Our set does indeed have 4 elements.

  • To remove an element from this set, use the remove method. Before we test this method,

  • let’s look at the help text.

  • Python gives a stern warning. If you attempt to remove an element that is not in the set,

  • you will get an error.

  • To test this method, let’s remove the number ‘42’.

  • We can check that it worked either by looking at the number of elements

  • Or displaying all the elements inside the set...

  • Look what happens if we try to remove ‘50’, which is not in the set

  • We get an error, just as Python warned us.

  • To avoid the possibility of an error, there is a second way to remove an element: the

  • discard method. Here is the help text:

  • With the discard method, if you try to remove an element which is not in the set, the method

  • does nothing - it quietly returns without making a change. Watch what happens when we

  • discard the integer 50, which is not in the set:

  • NothingPeace and quiet

  • The choice is yours. If you want to be alerted when your code tries to remove an element

  • not in the set, useremove”. Otherwise, discard provides a convenient alternative.

  • There is a second way to create a set which can be faster in some instances. When creating

  • the set, you can pre-populate the set with a collection of elements.

  • You can see this set contains 4 elements.

  • There is also a faster way to remove elements. To empty out the set and remove all elements,

  • use theclearmethod. The set now contains no elements - it has become the empty set.

  • We can move along; there is nothing to see here.

  • Now that we know how to create and modify a set, let’s learn how to evaluate the union

  • and intersection of TWO sets. If you have two sets A and B, then the union is the combination

  • of all elements from the two sets and is denoted with a U-like symbolThe intersection is

  • the set of elements inside both A and B, and the symbol for this operation is the flip

  • of the union symbol.

  • To see these in action, let’s look at the integers from 1 through 10.

  • The odd integers are 1, 3, 5, 7 and 9. The even integers are 2, 4, 6, 8 and 10.

  • The prime numbers between 1 and 10 are 2, 3, 5 and 7.

  • And finally, the composite integers - the integers which can be factored - are 4, 6,

  • 8, 9 and 10.

  • The union of the odd and even integers are all the integers from 1 to 10.

  • You get the same answer if you reverse everything.

  • Notice how the set of odds... and the set of evens are unchanged.

  • We can find the set of odd prime numbers by computing the intersection of the sets of

  • odds and primes. There are 3 in the range 1 to 10.

  • And there is only one even prime number … 2.

  • Which integers are both even and odd? There are none. The intersection of these

  • two sets is the empty set.

  • The union of the prime numbers and composite numbers are the integers from 2 through 10.

  • Notice 1 is missing - this is because 1 is neither prime nor composite.

  • Another common operation is testing to see if one element is inside a set. To do this

  • in Python, use theinoperator. Is 2 in the set of prime numbers?

  • Yes. This is a true statement.

  • Is 6 an odd integer? No. This is a false statement.

  • You can also test to see if an element is NOT in a set.

  • Is this statement true or false? True. 9 is NOT an even integer, so this is

  • a true statement. There are many more methods and operations

  • you can perform with setsTake a moment to explore these methods. You will not regret it.

  • Sets are a built-in data type in Python. They come equipped with all the luxury features

  • - unions, intersections, adding elements, removing elements, and much more. Everything

  • you will ever need for your data hungry codeprovided your sets are finite.

Python comes equipped with different objects to help you organize your data. These data

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

Pythonのセット|Pythonチュートリアル|Pythonプログラミングを学ぶ (Sets in Python || Python Tutorial || Learn Python Programming)

  • 2 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語