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:

Python 3 Programming tutorial Playlist:

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

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

source

40 thoughts on “Python 3 Programming Tutorial: If Statement”

  1. 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

  2. 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?

  3. 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")

  4. 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 ?

  5. 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?

  6. i am kinda confused, where are you typing this? notepad? because i downloaded python 3.5 directly form the website, and it doesnt let me do anything, like run and all.. please help… This looks much easier than what i have.

  7. 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.

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

  9. 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.

  10. 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!!

  11. 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')

  12. money = 1
    manapotion = 20
    strongmanapotion = 25
    if money >= manapotion + strongmanapotion:
    print("items are in your inventory.")
    elif money > 20 or 25:
    print("money is not enough, but you can buy one of them.")
    else: print("money is not enough.")
    print("but it's not possible")

    it prints "money is not enough, but you can buy one of them" . I don't understand how is 1 greater than 20 or 25. What am i doing wrong?

  13. 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)

  14. 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.

  15. 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.

  16. at about 2 minutes you create an if statement with different variations. You print them but you're only getting when you put in the print function. I tried just adding aaa in the print function, saved it and ran it. It came out with exactly what I put. This is because nothing is running the Print Function right?

  17. I am expecting the output of the following to be "Condition satisfied" but It actually is "Condition NOT satisfied"
    why?
    (i am using python 3.6.5)
    if 1.2+2.4 == 3.6:

    print("Condition satisfied")

    else:

    print("condition NOT satisfied")

  18. Sooo…. < is an assignment operator?

    It really helps if you actually learn the words used in the topic you are supposedly teaching about.

  19. hey can you tell me the difference between:
    1. if oldKey != thisKey:

    and

    2. if oldKey and oldKey != thisKey:

  20. 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.

  21. #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? """

  22. 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

  23. 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

  24. 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..")

  25. This is my code: question = input("Do you have a question:")

    if question is "yes":

    print("What is that question")

    Why does the it not print after I respond?

Leave a Reply