00:01 - 00:05

hey guys uh welcome back to the third

00:03 - 00:07

video in my Python Programming tutorial

00:05 - 00:08

series um today we're going to be

00:07 - 00:10

talking about something called

00:08 - 00:13

conditions so I hope you remember from

00:10 - 00:16

the other videos we talked about input

00:13 - 00:18

printing we did variables data types and

00:16 - 00:21

then we talked about operators so

00:18 - 00:24

operators like plus minus um division

00:21 - 00:27

integer division modulus right uh

00:24 - 00:28

multiplication all those operators today

00:27 - 00:30

we're going to be talking about a few

00:28 - 00:34

more operators and then getting into

00:30 - 00:37

conditions so here in my file right here

00:34 - 00:40

um I've put down four basic comparison

00:37 - 00:41

operators so pretty much a comparison is

00:40 - 00:44

something that's going to return a value

00:41 - 00:48

of true or false so here we have um

00:44 - 00:52

let's say if a real world example is Tim

00:48 - 00:54

equal to Joe well no they're not because

00:52 - 00:56

Tim obviously spelled differently than

00:54 - 00:58

Joe so that's going to be giving us a

00:56 - 01:01

value of false um same thing if we want

00:58 - 01:05

to go into numbers we say is 18 is

01:01 - 01:08

greater than 2 well is that true yes it

01:05 - 01:10

is so that condition gives us a value of

01:08 - 01:11

true uh it's a very simple concept but

01:10 - 01:14

it's something that we have to

01:11 - 01:16

understand as we go into decisions later

01:14 - 01:18

on which will be in the next

01:16 - 01:20

video okay so these comparison operators

01:18 - 01:21

I'll read them off and just kind of give

01:20 - 01:23

a definition of each of them so the

01:21 - 01:25

first one can e be a less than or

01:23 - 01:28

greater than sign uh same thing with the

01:25 - 01:29

second one right so it's just going to

01:28 - 01:32

compare if something's less than greater

01:29 - 01:36

than then um usually these are used for

01:32 - 01:38

integer values um that data type uh we

01:36 - 01:40

also can use them for Strings but it's a

01:38 - 01:41

little more complex and I'm not going to

01:40 - 01:45

get into that right now maybe in a more

01:41 - 01:47

advanced tutorial later on now the

01:45 - 01:49

equivalent operator so the equal sign

01:47 - 01:51

here now a lot of people think well if

01:49 - 01:54

I'm comparing something why wouldn't I

01:51 - 01:55

just use one equal sign like this well

01:54 - 01:57

that's a very good question and a lot of

01:55 - 01:59

people get mixed up on this it is

01:57 - 02:02

actually two equal signs that's because

01:59 - 02:07

because in Python um the one equal sign

02:02 - 02:09

is a declaration um declaration operator

02:07 - 02:13

so for example if I put a variable X and

02:09 - 02:16

I want to set it equal to four right um

02:13 - 02:17

this is what we do with the one equal

02:16 - 02:21

sign we're setting values right so when

02:17 - 02:23

we did name is equal to Tim things like

02:21 - 02:25

that we're using one equal sign now if

02:23 - 02:27

we're going to check something we're

02:25 - 02:29

going to compare them we have to use two

02:27 - 02:31

because two equal signs and one equal

02:29 - 02:33

sign have a very different meaning so

02:31 - 02:35

it's important to keep that in mind that

02:33 - 02:39

the comparison operator for equivalency

02:35 - 02:41

is two equal signs okay the next one is

02:39 - 02:44

not equal to um so we have an

02:41 - 02:47

exclamation mark and then we have the

02:44 - 02:48

equal sign like this okay so an example

02:47 - 02:52

of not equal to because some people may

02:48 - 02:55

get confused here say if four not equal

02:52 - 03:00

to 5 oops what did I type

02:55 - 03:01

there not equal to 5 okay so what do you

03:00 - 03:03

think this would give us just think

03:01 - 03:05

about it for a

03:03 - 03:08

second well what it's actually going to

03:05 - 03:14

return to us is true because the

03:08 - 03:16

condition is if four is not equal to 5

03:14 - 03:18

and here we can see obviously four is

03:16 - 03:20

not equal to 5 so now again if I give

03:18 - 03:22

another example we put five here we say

03:20 - 03:26

five not equal to 5 well that would

03:22 - 03:29

return a value of false because five is

03:26 - 03:31

indeed equal to five now we can do the

03:29 - 03:34

same thing with strings so for example

03:31 - 03:36

we have hello oops spelled incorrectly

03:34 - 03:40

there we go

03:36 - 03:42

and Tim uh now you can probably guess by

03:40 - 03:46

yourself that this is going to return

03:42 - 03:48

true because Tim is indeed not equal to

03:46 - 03:50

hello okay uh it's pretty

03:48 - 03:52

straightforward um but a lot of people

03:50 - 03:54

may get mixed up with the not equal to

03:52 - 03:56

if it's returning true or

03:54 - 03:58

false okay so those are the four basic

03:56 - 04:00

comparison operators that we're going to

03:58 - 04:03

use so we have equal to uh not equal to

04:00 - 04:04

and then greater than and less than so

04:03 - 04:08

greater than and less than again we use

04:04 - 04:10

for numbers so 1 less than two again

04:08 - 04:13

that's going to return true like that

04:10 - 04:15

okay so now um we're going to start

04:13 - 04:17

printing them some things to the console

04:15 - 04:20

and maybe doing a little bit of a quiz

04:17 - 04:23

here um just to see how you guys are

04:20 - 04:24

going to do so a booing condition I'm

04:23 - 04:27

going to store it in a variable so I'm

04:24 - 04:27

going to say name uh oops actually we're

04:27 - 04:30

not going to do that we're just going to

04:27 - 04:32

print to the console

04:30 - 04:36

2 less than

04:32 - 04:38

three okay now you guys guess and uh

04:36 - 04:39

tell me what you think is going to be

04:38 - 04:41

returned to the console it's either

04:39 - 04:43

going to be true or false I'll give you

04:41 - 04:43

a hint right

04:44 - 04:49

now now it gives us a value of true

04:47 - 04:53

that's because this condition 2 is less

04:49 - 04:55

than three um is true it's equal to

04:53 - 04:58

three right um so now if we switch the

04:55 - 05:00

sign and we make it greater than like

04:58 - 05:03

that well you can get guess what's going

05:00 - 05:05

to come up so we click enter and we get

05:03 - 05:07

false that's because this condition

05:05 - 05:10

returns a value of

05:07 - 05:13

false okay now we'll do another

05:10 - 05:16

condition here all right um

05:13 - 05:21

hello equal

05:16 - 05:24

to Hilo okay um and guess yourself what

05:21 - 05:25

this one's going to return false okay

05:24 - 05:27

that's because they are spelled

05:25 - 05:29

differently um now we're going to do the

05:27 - 05:32

not equal sign just to show that one as

05:29 - 05:34

well hello not equal to hello and we'll

05:32 - 05:37

see that gives us true because they are

05:34 - 05:39

not the same um fairly straightforward

05:37 - 05:41

but a lot of people do get these things

05:39 - 05:43

mixed up now I'll show you what happens

05:41 - 05:45

when I try to just do one equal sign

05:43 - 05:48

like

05:45 - 05:51

this see we get keyword can't be an

05:48 - 05:54

expression that's because we're trying

05:51 - 05:56

to declare a variable here but we cannot

05:54 - 05:58

do that okay because we're just using

05:56 - 06:00

one equal sign here we need to use two

05:58 - 06:02

all right

06:00 - 06:04

okay so now let's try another example

06:02 - 06:06

maybe a more advanced one uh this is a

06:04 - 06:07

little bit of a trick okay I want to see

06:06 - 06:10

if you guys remember from my last

06:07 - 06:10

tutorial what I talked

06:12 - 06:15

about so take a guess right now what you

06:14 - 06:16

think is going to be return you think

06:15 - 06:17

it's going to be true or do you think

06:16 - 06:21

it's going to be

06:17 - 06:23

false well it was false okay um that's

06:21 - 06:27

because again what we talked about in

06:23 - 06:28

Python um capitals matter right so any

06:27 - 06:31

capital letter is different than a

06:28 - 06:34

lowercase letter so something like this

06:31 - 06:36

um yeah it's going to return false all

06:34 - 06:39

right it's important to understand that

06:36 - 06:40

okay so we've very briefly uh gone into

06:39 - 06:42

conditions now um I'm going to write a

06:40 - 06:43

few on the side here you guys can go

06:42 - 06:46

ahead um I'll put them in print

06:43 - 06:49

statements and test yourself and see if

06:46 - 06:49

you get them right or not

06:49 - 06:54

okay I'm going to add a few tricks from

06:51 - 06:55

the other lesson um try to kind of apply

06:54 - 06:57

some of our

06:55 - 07:01

knowledge and

06:57 - 07:03

see if we can get them correct

07:01 - 07:06

okay so just give me a minute here I'm

07:03 - 07:06

just going to write a few

07:08 - 07:12

out and then we'll go through the

07:10 - 07:14

answers afterwards okay so you know what

07:12 - 07:15

we'll just we'll just do these ones for

07:14 - 07:16

right now and we'll do a few other

07:15 - 07:18

examples okay so these are a little more

07:16 - 07:20

advanced than what I was talking about

07:18 - 07:22

before but they are conditions we have a

07:20 - 07:24

left side of the condition and we have a

07:22 - 07:25

right side of the con condition so I

07:24 - 07:26

hope you've taken a guess now of what

07:25 - 07:28

you think these are going to be because

07:26 - 07:32

I'm about to print out the answer right

07:28 - 07:35

now Okay so we've gotten true false and

07:32 - 07:36

false all right so for the first one two

07:35 - 07:39

less than three that's an example I

07:36 - 07:42

already gave that gives us a value of

07:39 - 07:46

true because that condition is true uh

07:42 - 07:48

now we have 2 - 3 + 4 greater then five

07:46 - 07:51

um so you can do the math there and see

07:48 - 07:53

that that is going to be false and then

07:51 - 07:55

this one some people may have gotten a

07:53 - 07:57

little bit of a little bit confused on

07:55 - 08:00

cuz I actually use the words false and

07:57 - 08:02

true in my condition that's going to

08:00 - 08:05

return false or true so I have print

08:02 - 08:07

false equals equals true well is false

08:05 - 08:08

the same thing as true no it's not so

08:07 - 08:10

obviously we're going to be returned

08:08 - 08:12

with the value

08:10 - 08:15

false Okay so we've talked a little bit

08:12 - 08:16

about conditions now um in the next

08:15 - 08:17

tutorial we're going to be doing

08:16 - 08:20

something called decision so I'll give

08:17 - 08:23

you a sneak peek on what that

08:20 - 08:26

is something like this

08:23 - 08:29

okay so stay tuned for that um the next

08:26 - 08:31

episode should be up tomorrow um um and

08:29 - 08:34

yeah I hope you guys enjoyed the video

08:31 - 08:39

go ahead like And subscribe and yeah

08:34 - 08:39

share with your friends thanks bye

Mastering Python Comparison and Conditions: Essential Skills for Programming Success

Understanding comparison operators and conditions is fundamental for writing effective Python programs. These tools enable your code to make decisions based on data, leading to dynamic and interactive scripts. In this comprehensive guide, we'll explore comparison operators, how to use them correctly, and introduce the concept of conditions to help you take your Python skills to the next level.

Introduction to Comparison Operators in Python

Before diving into conditions, it’s crucial to understand the core comparison operators. These operators compare values and return Boolean outcomes—either True or False. This binary response drives decision-making in Python.

Basic Comparison Operators

Operator Description Example Returns
< Less than a < b True if a is less than b
> Greater than a > b True if a is greater than b
== Equal to a == b True if a equals b
!= Not equal to a != b True if a does not equal b
<= Less than or equal to a <= b True if a is less than or equal to b
>= Greater than or equal to a >= b True if a is greater than or equal to b

Understanding the Difference Between

Many beginners stumble over the difference between assignment and comparison:

  • = is the assignment operator. It assigns a value to a variable.
  • == is the comparison operator. It checks if two values are equal.

Example:

x = 4       # Assigns 4 to variable x
x == 4      # Checks if x equals 4, returns True

Using = instead of == in a comparison causes errors, as Python interprets it as an assignment, not a comparison.

Using Comparison Operators in Practical Scenarios

Let's look at some real-world examples:

# Numerical comparison
print(18 > 2)          # True
print(5 != 5)          # False
print(10 == 10)        # True

# String comparison
print("Tim" != "Joe")  # True (because names are different)
print("Hello" == "Hello")  # True

Remember, string comparisons are case-sensitive ("hello" is not equal to "Hello").

Working with Conditions in Python

Conditions allow your program to execute specific blocks of code based on whether certain criteria are true or false.

How to Use Conditions

Conditions typically use the if statement:

if condition:
    # code block if condition is True
else:
    # code block if condition is False

Example: Basic Condition

age = 20
if age >= 18:
    print("Adult")
else:
    print("Minor")

This logic outputs 'Adult' if the age is 18 or more, otherwise 'Minor'.

Combining Conditions with Comparison Operators

You can combine multiple conditions using logical operators:

  • and — Both conditions must be true.
  • or — Either condition can be true.
  • not — Reverses the truth value.

Example:

age = 25
has_license = True

if age >= 18 and has_license:
    print("Can drive")
else:
    print("Cannot drive")

Practice: Testing Your Understanding

Now that we've covered the essentials, here are some practice snippets:

print(2 < 3)              # True
print(4 != 5)             # True
print("hello" == "hello")  # True
print("Hello" == "hello")  # False (case-sensitive)
print(10 >= 10)           # True
print(5 > 10)             # False

Tip: Always test your comparison logic using print statements to verify expected outcomes.

Common Pitfalls and How to Avoid Them

  1. Using a single equals = instead of ==
    This results in assignment instead of comparison, leading to syntax errors.

  2. Case sensitivity in string comparisons
    Remember that "Hello" is not equal to "hello" in Python.

  3. Confusing != and ==
    Double-check which operator matches your intended check.

  4. Misunderstanding Boolean results
    Use print statements or debugging tools to verify your conditions evaluate as expected.

From Conditions to Decisions

Once your conditions are working, you can build decision trees in your programs:

score = 85
if score >= 60:
    print("Passed")
else:
    print("Failed")

This forms the foundation for more complex decision-making.

Final Tips for Comprehending Conditions in Python

  • Practice writing different conditional statements.
  • Combine comparison operators with logical operators for complex checks.
  • Remember to test conditions with print statements to see their Boolean outcomes.
  • Pay attention to syntax details—== vs = and case sensitivity.

Conclusion: Elevate Your Coding with Masterful Use of Conditions

Conditions and comparison operators are the backbone of interactive, responsive Python programs. By mastering these tools, you're setting yourself up for more advanced programming tasks like loops, functions, and decision trees. Think of them as your programming compass—guiding your code's flow intelligently. Don't wait—start experimenting with your own conditions today, and watch your Python skills soar.

Ready for the next level? In upcoming tutorials, we’ll explore how conditions integrate into decision-making structures like if-elif-else, paving the way for developing sophisticated applications. Happy coding!