[Tutorial] Basics of Python No 2
if/else is one of the most important things in like any programming language .
it checks if the value in if is true it will return what you want and if it was false it will return the else
statment so here is an simple example
```number = 4;
if number == 4:
print(“We use if statment”);
else:
print(“We use else statment”);```
it will print
We use if statment
since the number is 4
Note : = sign is for declaring a variable but == sign is to check if value is equal to value :)
another example using else statment
myName = "roun"
if myName == "nuor":
print("We use if statment");
else:
print("We use else statment");
Since myName is not equal to nuor it will avoid if and go to else so it will print
We use else statment
now elif is like typing else if inside if/else statment and you can type it morethan one time ( as much as you want )
the script will check the condition of if if it’s true or false , then it will go to elif then else
so here is an example of it
```number = 5;
if number == 4:
print(“We used if statment”);
elif number == 5:
print(“We used elif statment”);
elif number != 5:
print(“We used elif statment”);
else:
print(“We used else statment”);```
So it will print the first elif statment because the number == to 5
We used elif statment
Thanks for Reading , in next tutorial i will post about import , defining , return ,for & while loops , Wish you understand it
If you make people think they’re thinking, they’ll love you. but if you really make them think, they’ll hate you.
~ Harlan Ellison
Thanks for this tutorial. But I still have a critic… Maybe I’m not part of the target audience (altough I’m a beginer considering I’ve never seen python code in my life…) but I think you should try to be more brief in your explanation and try to cover more stuff. I don’t need 3 different examples to understand how to add a space…
Just because I am paranoid doesnt mean theyre not after me…
Hmm , the tut was good !!
Line wise explanation !!
I did not see python before , but this is helping a lot !!!!
Thanks roun ! :)
- @IAmDevil
Its good to be back! :D
I don’t need 3 different examples to understand how to add a space…[/quote]
I'am Just saying that whitespace is important in Python and it’s case sensitive :)
[quote] I think you should try to be more brief in your explanation
Lol and i tough i didn’t explain everything :)
If you make people think they’re thinking, they’ll love you. but if you really make them think, they’ll hate you.
~ Harlan Ellison
I agree with @DaGr8Kornolio and I think you really could go a bit deeper ;)
And I think you could at least write about else if in the same post, because if I remember that right else if is unlike in many other languages uses by typing elif.
So you covered some basic stuff now, like input, if statement, and math operators. Maybe a little script to run would be nice here:
(Certainly not perfect - made of what I know)
con = 'y'
while con == 'y':
print ("Please enter the first value: ")
val1 = int(input())
print ("Please enter the second value: ")
val2 = int(input())
print ("Please enter the operation charakter: ")
op = input()
if op == '+':
endVal = val1 + val2
elif op == '-':
endVal = val1 - val2
elif op == '*':
endVal = val1 * val2
elif op == '/':
endVal = val1 / val2
print ("%d" % endVal)
print ("Continue? <y:yes>")
con = input()
And we don’t need ; at the end of a line do we? You wrote that a few times.
DaGr8Kornolio means i explained it too much and about else if , it’s in the next one and i didnt put it in same post because i felt it’s too bug and about the script you wrote , is it what fromwarriors create ? in his thread he did this i think and about the ; i read somewhere that it must be but as i tried before it worked without ; bust just sayin
If you make people think they’re thinking, they’ll love you. but if you really make them think, they’ll hate you.
~ Harlan Ellison
@roun512 I wrote the script on my own so I don’t know if something like that exists somewhere else but I guess so.
The script works so I guess you don’t need the ; at the end ;)
And as @DaGr8Kornolio said at some places you explained it too much but I think you could have explained the else if in the same post because you wrote about if and else here..
But don’t get me wrong, I like your tutorials and the fact that you write them about Python.
And we don’t need ; at the end of a line do we?
The truth is to separate instructions you need either a ‘;’ or a line return.
That’s means you should be able to write
print "toto" print "titi"; print "tata" print "tutu"
By the way, I suggest only one instruction per line, except some really rare case like the define of vertex you can’t put in a loop and implies many lines other way it’s just make the code unclear.
When I learned python I always put a ; at the end but like you said its only needed if you put it all on one line (looks messy though, and hard to read for beginners) I suppose its just a habit.
- @IAmDevil
Its good to be back! :D
Thanks @Memoria but I don’t write more then one instruction in one line so I won’t have to use ; I guess.
What I like about Python is that you can read it so easy so I guess that’s fine
[quote=ColdIV]roun512 I wrote the script on my own so I don’t know if something like that exists somewhere else but I guess so.
The script works so I guess you don’t need the ; at the end
And as DaGr8Kornolio said at some places you explained it too much but I think you could have explained the else if in the same post because you wrote about if and else here..
But don’t get me wrong, I like your tutorials and the fact that you write them about Python.[/quote]
I'am not saying that u take it from somewhere :) and about elif i will edit the post now to be in same post as if/else :) & Thanks guys for your feedback
Edit: Ok done editing it , i added elif to it
If you make people think they’re thinking, they’ll love you. but if you really make them think, they’ll hate you.
~ Harlan Ellison