[Tutorial] Basics of Python No 2

???Roun512 [roun512]
11 years ago | edited 11 years ago

0

![Image](/files/media/images/articles/0/0/up_48204193221c9da69ff1b603d71133f7.jpg)[/center]
- Creating comment[/center] Ofcourse Comments are pretty important at some points and to write it we will use # or """Comment""" tag so here's a code as an example of it ```#This is a comment``` Each comment line must have # tag like ```#Line 1 #Line 2 #Line 3``` and it can be done inside the script like this : ```foo = "Hello, World"; #It will print Hello, World print foo``` or if you want to write multi-Lines with no # we can use the triple quotes ```"""This is many lines comments Line 1 Line 2"""```
- More about print command
We can use Print() command in many different ways we can use + tag to connect words together for example ```print("Hello," + "World");``` it will print ```Hello,World``` The above sentence is not what we want , because it don't have space between Hello, and World so to fix this and create a space using this print() method we can do it like ```print("Hello," + " World");``` it will print ```Hello, World``` Their is space because we added a space between " and World. Another way we can use print(); function is by adding variable to the sentence so here is an example of it ```foo = "Hello,"; print(foo + " World");``` It will print out ```Hello, World``` Here is another example by adding 2 variables ```foo = "Hello,"; bar = " World"; print (foo + bar);``` It will print out ```Hello, World``` [center]- Input command
input() command is also important , It's like asking the user for anything you like for example ```input("What is your name ?");``` Then it will print ```What is your name ?``` and it will give you the ability to answer it but the answer will be in the same line in the question and to avoid this you must know the following : If you write [*]\n , it will create a new line [*]\t , it will create a new tab ( like 4 or 5 spaces ) [*]\' , it will allow you to add ' to the script So That how it will be done ```input("What is your name ?\n");``` it will print out ```What is your name ?``` and then you will write in another line not in same line :) [center]- If/else statments

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

12replies
6voices
232views
DaGr8Kornolio
11 years ago

0

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…

[IAmDevil]
11 years ago

0

Hmm , the tut was good !!
Line wise explanation !!
I did not see python before , but this is helping a lot !!!!
Thanks roun ! :)

???Roun512 [roun512]
11 years ago | edited 11 years ago

0

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 :)

J [ColdIV]
11 years ago

0

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.

???Roun512 [roun512]
11 years ago

0

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

J [ColdIV]
11 years ago

0

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

Pawda [Memoria]
11 years ago

0

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.

Gninja
11 years ago

0

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]
11 years ago

0

; is a terminator and is present in every programming language !!! Isn’t it ?

J [ColdIV]
11 years ago

0

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

???Roun512 [roun512]
11 years ago | edited 11 years ago

0

[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

[IAmDevil]
11 years ago

0

Yup much better now !!!

You must be logged in to reply to this discussion. Login
1 of 13

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss