Placeholder Image

字幕列表 影片播放

  • Hey, everyone.

  • I talked about using bullion in python in a few of my previous videos, but I decided to make a whole new video body because it's an important topic now.

  • Bullion isjust a type of data, just like strings on interest that we've seen now.

  • Examples off a string might be something like Microsoft Apple on banana.

  • Unusually strings in python are enclosed in either double quotes or single coats.

  • Examples off on inter might be something like 342 on minus wander to.

  • And what about bullion?

  • Well, for bullion, there are only two possible values, and their true and false on a bullion is basically used to express.

  • If a certain logical condition is true or false.

  • Now, in python, there's a useful function called the type, which we can use to find type off any value.

  • So, for example, if you write type off double quotes Microsoft, then this gives us STR, which stands for string on If the right type off, let's say four.

  • This will let us know that this is an interview because they says into an iffy right type off true note that true here is capitalized.

  • This gives us bull, which stands for bullion and let's not see how we can use bullion.

  • Barry's Actually you've already seen one use case off a bullion value in my if else videos.

  • So if you're right, for example, a goes three on because one and then if a is greater than be colon four spaces Prince a is great of, then be then this clause right here a great villain B is evaluated to be either true or false on in this particular case, A, which is three is greater than be, which is one.

  • So this is evaluated to be true.

  • So it's sort of like saying, if true, them prince is grade of B.

  • Ah, in this particular case is grade A B is true, so that's printed right here.

  • So actually we can get the same result by writing, if true, calling again.

  • Note that true here is capitalized.

  • Prince A is greater than be.

  • Once you run this cell, you see that is grade A B is printed.

  • Actually, you could also store the result of this condition a greater than be in a variable.

  • So let's call that viable bullion value or bullion on the score vary, And then we're gonna write this ecos a great of them be on.

  • When I run this cell, a grill B is violated, and that returns to and then this variable bullion value will have that Barry True.

  • So if you print bullion value, we should see true.

  • And then after that, you can write if bullion vary.

  • Column four spaces.

  • Print A is greater than me, and when we run the cell, we get the same result.

  • Because bullion Barry is true.

  • So what did you use?

  • Bullion valleys?

  • Well, they're basically useful when you want to do some complex logical operations.

  • So let's say you want to write a function called Are You Sad?

  • Which is gonna take two bullion arguments, is Renny and has umbrella on Let's say from this function, you want to return true on.

  • Indicate that you're sad if it's raining on.

  • If you don't have an umbrella, so you want to return true on Lee, When is rainy?

  • It's true and has umbrella is false, so you could write this function this way.

  • If is rainy.

  • Eco's true and has Umbrella Eco's That's double the co sign forth, Call him, then return truth else returned false.

  • So this says you're only sad or you're gonna only return True when Israeli is true on house umbrella is false And if that's not the case, then returned false.

  • So this function works just fine.

  • But you can simplify this a little bit more by writing.

  • If is Rennie on DDE not has umbrella then return chute.

  • So this is exactly the same as saying If it's rainy is true on house umbrella, it's not true or has umbrella is false then return true on Basically this whole expression is rainy and not has umbrella will be evaluated to be either true or false on If this whole expression will be valued it to be true then we're gonna return truth.

  • If the whole expression becomes false, then we go to the else claws and then we return false from our function and that actually suggests that we can further simplify this by writing.

  • Return is rainy on DDE not has umbrella and that's actually all we need.

  • So this closest we're going to return.

  • The result of this whole expression is rainy and not has an umbrella and this whole expression will become true only when is rainy is true and has umbrella is not true or has umbrella is false.

  • So let's now run this cell on.

  • Let's test dysfunction.

  • If you're right, are you sad off chew on force?

  • So if it's rainy and if you don't have an umbrella, you should be sad.

  • So this should return True on it does.

  • And if you're right, are you sad of true and true?

  • So if it's rainy, and if you do have a nimbler, you shouldn't be sad.

  • So this function should return false and it does.

  • And if you want, you can test it for all the other cases, too.

  • And that will be false.

  • True on false false on.

  • Either way, this function should return false.

  • Okay, and here's a little exercise problem for you.

  • Can you write a function called See Greater Than D Plus E, which is gonna take three Intissar arguments C, D and E on dysfunction should return to Onley when C is greater than D plus e on otherwise issue return.

  • False.

  • So maybe possibly two right here.

  • If you want to try solving this problem yourself.

  • Okay, here's my solution.

  • Here You could write if see great.

  • Um, the plus e calling, then return true and else Colin returned false on this is just fine.

  • But you could also write it as return see greater them the plus e.

  • And this way, if C is greater than the policy, this whole expression becomes true.

  • So will return through in that case.

  • And if that's not the case, then this whole expression becomes force, so we'll return that.

  • So let's just run this cell on, test it.

  • I'm gonna write, see?

  • Grow out of them.

  • D plus e 311 See, which is three is greater than one plus one.

  • So this should return True that it does.

  • And I'm gonna write, See great of them on.

  • By the way, you can just hear press tab t automatically complete the rest of the function, and then 312 three is not greater than what plus two, which is three.

  • So this should return false.

  • It does.

  • Okay, and that's it for this video.

  • But if there's any topic I should cover in future videos or if anything was unclear in this video, just let me know in a comment on as usual.

  • Thanks we're watching on.

Hey, everyone.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

在Python中使用布爾函數 (Python教程 #11) (Using Boolean in Python (Python Tutorial #11))

  • 14 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字