Python 3 Programming Tutorial: If Statement

The if statement is one of the most basic forms of logic that can be introduced into your program. The idea of the if statement is to assess whether something is the case, and, if it is, then to perform the following block of code within the statement.

Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/

Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M

In this tutorial you are shown how to use the if statement in python 3.3.

http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

Comments

Grant Robertson says:

Sooo…. < is an assignment operator? It really helps if you actually learn the words used in the topic you are supposedly teaching about.

Nimity Tei says:

pretty much the same in java except for no semicolons and : instead of {} LOL
really appreaciate ur stuf man

Adrian Nielsen says:

Yes, but you should read from left to right, not from the middle to the left, then jump back over to the right, lol. Better: z is less than y and greater than x.

Vitex says:

I got the video to ‘666’ likes, are you impressed?

Jamel Stringer says:

Sentdex how is it going? Please tell me why when I run the following code and enter 8 as my integer my program does not print please. Thank you.

num = int(input(“Enter a number: “))
n = num % 2
if n != 0:
print(‘Weird’)
if n == 0 and n == range(2,6):
print(‘Not Weird’)
if n == 0 and n == range(6,21):
print(‘Weird’)
if n > 20:
print(‘Not Weird’)

EVDE KSP says:

Hi sentdex, i still have a question:
What if i wanted to put more than 1 statements into a single one like this: “if z == 10, x == 9:”
Because i tried this and it didnt work (the problem is with the comma i believe)
So what should i do?

Thanks in advance

Brady Huang says:

Wow, this is so handy and helpful with the operand statement!!
Learn for a while but figure out now!!

Free Pokemon TCG online codes says:

a=int(input(“How old are you?”))
if a >= 16 < 18: b=input("Are you at sixth form, work or at college?") print("You work at",b,".") if a > 18:
c=input(“Have you started taking driving lessons yet?”)

Why does this not work!!

eathenbad says:

On Python3, is the variable x a numerical value or a string or neither?

Reggie The Epic Gamer says:

Can you do a if a = true print (‘a is true’) ?

Aiden Gajadhar says:

What is wrong with this?
a=int(input(“How many hours did you sleep for last night?”))
if a > 4:
print(“Back to bed!”)

markmotarker says:

hi, I have been watching the videos so far, but my Python just runs everything on its own. how do I make it so that it runs separately?

itz em'N'em says:

Someone help my teacher set our homework to watch this and if we can’t do the starter next lesson she said she will give us a no homework detention because that means we haven’t watched the video but I don’t get it like my brain is fried but she is so strict she won’t belevie me

Exploiter Nemesis says:

#Help! Script isn’t working.

myVar = False
passwordinput = input(“What is the password?”)
password = “Hello”
if password == passwordinput:
print(“Correct!”)
main()
#This is defined in the full script
else:
print(“Incorrect”)
print(passwordinput)

“”” I type in hello which is incorrect – Output:
What is the password?
Incorrect
hello
What is number 1? “””

Gaming K says:

what if I wanted to put an input like this, Sport = input(“Which sport do you like most football, tennis, bike polo or badminton?”) and I wanted reply to each of the sports picked so how would I do that?

Lei Sheng says:

You can also use negative statements like:

x = 3
y = 5

if not x == y:
print(‘x is not equal to y’)

if not y < x: print('y is greater than x') I wouldn't recommend doing like that second example though as it is confusing. But just so you know, it's possible.

David Williamson says:

comparison.

Python User says:

Can we make the user to write x,y ? So its kind of intersting

thecoolerpat says:

thank you so much you helped me so much my diuddddddd liked and subeed notifications squad

april8tals says:

Coffee = 2
milk = 8
sugar = 0

if Coffee > milk:
print (‘too much coffee!’)

if sugar > 0:
print (“Oh no! it’s sweeeeeto!”)

if Coffee == milk:
print (“OOH that’s a strong coffee..”)

ex tro says:

print (“You are the best sentdex”)

Evan Daly says:

found this interesting, and may be worth noting. Even though A is less than D, this statement results as True.
a,b,c,d=1,2,3,2
print(a < b < c > d)
>True
So I guess py evaluates the comparison adjacent variables only? As in…
A is not compared to C or D.
B is not compared to D.
C is not compared to A.

legend_017_ -Brawl stars says:

this is my first time coding and I tried this if statement. (I’m using python 3.6.5)

>>> x=5
>>> y=7
>>> if x < y: print ("hi") I didn't work, and I got an error saying this, SyntaxError: multiple statements found while compiling a single statement

Jessy todd says:

Are you typing this in notepad?

bad muchacho b says:

Thanks!

Xilent says:

I have used this way for it to work, but it isn’t working. What did i do wrong?

print (“What is the temperature and humidity today?”)
Humidity ==(input()) and temperature ==(input())
if (humidity > 90) and (temperature > 70) :
print (“Rainy today”)

Totally Anonymous says:

if myBrain > laziness:
print (“Maybe i can learn python!!!”)

*didn’t print*

dusan sandic says:

Getting syntax error on “=”
a=int(input(‘Unesite prvi broj’))
b=int(input(‘Unesite drugi broj’))
c=input(‘Unesite operaciju s/o/d/m’)
print(‘Racunam…’)
if c=s:
print(a+b)
if c=m:
print(a*b)
if c=o:
print(a-b)
if c=d:
print(a/b)

Adam Fatyga says:

So “z < y > x” is the same then “z < y and y > x”? or is there a difference?

Shobhith K Menon says:

you are not a real programmer, You use a light IDE, you are supposed to use. dark IDE

Arrows Verse says:

wow takes me a couple lessons foor the teacher to finish one of these but for youit takes about less than 10 minutes although i learn with 15 mins at school.
Yours is so much easier to understand. I might be saying that cause im a fastlearner but it is the truth.

Just Me says:

clearly explained

Okika Ch.D Donatus says:

what kind of python book for beginners with clear and precise teaching goals aimed at building a novice from scratch to real and more challenging excises, and ofcos easy and fun with , would you recommend.

Was using a PDF format of python programming for the absolute beginners ,though its easy to learn with but it mostly focuses on games .

With an appetite for networking and data security as an ambition would you recommend such a book ? if not which ?

Crazed Thinker says:

I’m getting a sytex error for (reward) with the following..
if reward != 0: # Pong has either +1 or -1 reward exactly when game ends.
Any ideo why? Thanks for your help.

 Write a comment

*

Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!