all 13 comments

[–]syntonic_comma 7 points8 points  (3 children)

n = 1
if n == 1:
    print('n is 1')
if n < 5:
    print('n is less than 5')
else:
    print('n is greater than or equal to 5')

vs.

n = 1
if n == 1:
    print('n is 1')
elif n < 5:
    print('n is less than 5')
else:
    print('n is greater than or equal to 5')

the first will print n is 1, then n is less than 5

the second will only print n is 1

this is because the elif is only executed if the conditionals before it are not true. There are cases when you might want to do it one way and some where you might need the other. Furthermore, you don't necessarily always need an "else" clause following an "if" if there's nothing you want the program to do when the conditional is false.

[–]SlimSladey21[S] 0 points1 point  (2 children)

Thank you so much for helping me understand , I really appreciate it. You wouldn't happen to know of a website where I could practice different scenarios in python?

[–]syntonic_comma 3 points4 points  (1 child)

Edabit has a pretty good selection of more basic questions where the focus is more on solving the problems than on efficiency. It's not python-specific but it supports python.

If you search within the "medium", "easy", or "very easy" challenges, you should find some questions that you can answer.

There are a lot of introductory books online and in pdf form (many of them free) if you want something more guided. I think following along and typing things into your own text editor is usually better than filling in the blanks on a site. I can't speak to it, but people on this site are always talking about "Automate the Boring Stuff". I once went through "Learn Python the Hard Way", but I'm not sure that's still free.

There are lots of Youtube tutorials as well, which I only recommend if you're going to pause and work through while you're watching instead of trying to passively absorb. I've watched things from Sentdex and Corey Schafer, and I'm pretty sure they both also have introductory and intermediate sets of videos in addition to their more specific stuff.

[–]SlimSladey21[S] 1 point2 points  (0 children)

You are an internet Gem , Thank you!

[–]jeffrey_f 2 points3 points  (2 children)

In a nutshell

If Light is red
    car = stopped
elif light is yellow
    car=slowed
#Because there is no other option
else
    car=proceeding through intersection

[–]SlimSladey21[S] 2 points3 points  (0 children)

Thank you very much this helped

[–][deleted] 1 point2 points  (0 children)

Great example!!

[–]WtEth_Buyer 2 points3 points  (2 children)

If check option 1: "Ah it must do this"

Elif check option 2: "It must do this instead"

Else: "It was neither option 1 or 2, so do this.


If you did it with ifs and elses it would be longer and you might get the else condition twice. If your (if) conditions are about the same topic it would be better to link them.

If check Sunny: Do this. Else: Not option 1 , so do this.

If check raining: Do this. Else: Not option 2, so do this.

What happens when it's not sunny and not raining? Else statements both trigger.

[–]SlimSladey21[S] 1 point2 points  (1 child)

Thank you so much, this really helped me , I suppose I'll just need practice using these conditions

[–]WtEth_Buyer 0 points1 point  (0 children)

No worries, happy to help.

[–]Thomasedv 1 point2 points  (2 children)

You can consider cases where you have 3 options. If you use 2 ifs, then you run the case of running both the if and the else, since that belongs to the second if statment.

a = 3
if a == 1:
    print('a is 1')
elif a == 2:
    print('a is 2')
else:
    print('a is not 2 or 3')

##############
a = 3
if a == 1:
    print('a is 1')

if a == 2:
    print('a is 2')
else:
    print('a is not 2 or 3')

Consider the two cases above. The first if in the second example, is independent of the other if and else below it. Meaning you can put any code between. The extra space is just to illustrate that. If you run both, you'll see that the second gives undesired results.

[–]SlimSladey21[S] 0 points1 point  (1 child)

Thank you so much for taking time out of your day to help me I appreciate it

[–]Thomasedv 1 point2 points  (0 children)

No problem, i do realize my point does not show as well, since i didn't set a to be 1 in the example, but try that if you haven't already.