Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Now up to this point, you've seen us use if statements,
2
else statements, elif statements,
3
multiple if statements as well as nested if statements.
4
But the thing that we haven't been able to do thus far is to check for multiple
5
conditions in the same line of code.
6
So how would we be able to combine different conditions and say, well,
7
is the pizza large and the user wants pepperoni and extra cheese all in the
8
same line of code? Well,
9
to do this we would need to learn about the logical operators.
10
So there's three of them that are really useful: and, or and not.
11
So let's take a look at the first one.
12
When you combine two different conditions using an and operator,
13
they both have to be true,
14
both A and B for the entire line of code to be true.
15
If just one of them is true, say A is true and B is false,
16
or A is false and B is true, then the overall thing evaluates to false.
17
So if we take a look just inside the console,
18
and in fact, I'm going to clear the console, and I write some code here.
19
Let's say that, um,
20
A = 12 right? Now let's say that I wanted to check whether if is A greater
21
than 15? Well that is going to be false.
22
Now what about is A greater than 10? Well,
23
that's going to be true.
24
Now if I combined this using an and statement, I could say,
25
well is A greater than 10 and A is less than 13 and hit enter, then I would get
26
true because both A is greater than 10 and A is less than 13 are true.
27
So in this case, when both conditions are true,
28
then this entire line gets evaluated to true.
29
But see what happens when just one of them is false.
30
So is A greater than 15 and A less than 13? A is greater than 15 is false,
31
A is less than 13 is true and now we get false.
32
So that's what happens when you combine different conditions using and. Now if
33
you only needed one of the conditions to be true,
34
then you could use the or operator instead.
35
So if a C or D were true or if they're both true,
36
then it'll evaluate to true.
37
It's only when both C and D are false
38
does this statement actually become false.
39
Now the final one is the not operator and all that this does is it basically
40
reverses a condition. So if the condition is false,
41
then it becomes true. If it's true, then it becomes false.
42
If I wrote not
43
and then I wrote the condition A is greater than 15. A greater than 15 is
44
definitely false, 12 is less than 15. But by putting the not there
45
it's going to reverse this, so going from false to true.
46
So when I hit enter, you'll see it is now true.
47
Coming back to our rollercoaster ticketing,
48
let's say that the rollercoaster company decided that for everybody who was
49
having a midlife crisis, they would give them free tickets.
50
And according to Wikipedia,
51
midlife crises typically occur when you're a 45 to 55 years old.
52
Let's see if we can incorporate this into our code.
53
So let's say that in addition to these existing price categories,
54
what if you had to add a separate price category for those people who are aged
55
between 45 and 55 and those people get to ride for free?
56
Do you think you would be able to change the code using what you've learned
57
about logical operators in order to incorporate this addition to our program?
58
Pause the video and give that a go.
59
So we've currently got three conditions: age,
60
less than 12, age between 12 and 18, and finally everybody else.
61
Now instead of just finishing up there,
62
let's go ahead and add another elif.
63
And here we're going to combine two conditions.
64
We're going to say if the age is greater than or equal to 45 and the age is less
65
than or equal to 55, then we get to catch that midlife crisis window.
66
Well,
67
in this case, we're going to print something like-- we're going to say "Everything's
68
going to be ok. Have a free ride on us!"
69
And in fact,
70
we don't need to modify the bill in any way because we know that with
71
if, elif and else statements is that once this condition matches,
72
then everything that's inside this block,
73
so everything that's indented inside this elif is going to be carried out
74
namely printing this out,
75
and then it's going to skip the rest of this if/else block and continue on.
76
Now, if they want to photo this,
77
they'll have to pay $3 but at least their ticket is free.
78
And we've been able to do this because we know about the and logical operator.
79
In the next lesson,
80
we're going to practice using logical operators and more by completing another
81
code challenge. So for all of that and more, I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.