Post by l3galian on Jul 8, 2014 20:51:37 GMT -5
This is a no nonsense python tutorial that i am writing for claycode forums.
For the purposes of this tutorial I am assuming that you already have python 2.7 and sublime text (or some other text editor) installed and know how to save and run python programs.
Now that the nonsense is out of the way, we can begin out tutorial. Your first program is as follows:
If you run the program you will find that the words "hello world" show up in the console/terminal.
So what happened?
Python code executes from top to bottom, one line at a time. "print" is a function.
A function is an action or collection of actions that can be run in code.
"print" is a special-case function, and can be rewritten without parenthesis, but I don't want to confuse you too early.
The "hello world" part is called a parameter. A function's behavior depends on the parameters that you send it.
For instance, if you replaced "hello world" with "goodbye cruel world", a different thing would happen.(try it.)
The quotation marks around the words tell the program that "hello world" is a string. A string is text.
Anything goes inside a string. You could type a bunch of keyboard nonsense and the computer would not fault you for it.
Actually not everything. Letters with accents will throw it off. (Among certain other weird letters that you cant type with a keyboard)
Traditionally, the pattern for calling functions is as follows:
By the end of this tutorial, you will all be parameter-passing ninjas.
So what happened?
This one will be a bit tougher to explain.
"x" and "y" are variables. Variables are like containers. They can hold one thing at a time.
Sometimes, though, variables can hold something that holds many things that hold many other things. Thats called a data structure.
Variables can be named whatever you want, so long as the following conditions are met:
I could go on and on about how to camelCase and under_score your multi-word variables, but it is just preference.
in this case, "y" holds "my face" and "x" holds "4".
When the computer gets to the third line, it doesn't see "print(5,y,x)".
Instead, it sees "print(5,4,"my face")".
Also, the "print" function can accept infinitely many parameters. It just spaces out the parameters in the console/terminal.
Most functions will have some restrictions on how many parameters are passed.
Variables are complicated, and the best way to explain their behavior is with examples.
So what happened?
Every time you assign a value to "toprint", the old one is forgotten.
I also demonstrated the difference that quotations have:
" "toprint" " refers to a string with the word "toprint".
" toprint " refers to a variable named toprint.
This has to be the longest any tutorial has ever gone without introducing addition. Even your math teacher knew to hurry up.
That was a long example.
I had to make sure to demonstrate everything.
So what happened?
I think that whole thing was sort of self-explanatory.
"+" adds, "-" subtracts, "*" multiplies, and "(...)" are used for control over the order of operations.
"/" also divides, but there is special consideration to make sure that the answer hasn't been rounded.
One of the things I try to demonstrate in this tutorial is how everything in python comes back to functions.
the following code is equivalent to the above code:
so that you will keep it in mind and figure out how it works yourself by learning in future sections of the tutorial.
For the purposes of this tutorial I am assuming that you already have python 2.7 and sublime text (or some other text editor) installed and know how to save and run python programs.
Now that the nonsense is out of the way, we can begin out tutorial. Your first program is as follows:
print("hello world")
That wasn't too hard, was it?If you run the program you will find that the words "hello world" show up in the console/terminal.
So what happened?
Python code executes from top to bottom, one line at a time. "print" is a function.
A function is an action or collection of actions that can be run in code.
"print" is a special-case function, and can be rewritten without parenthesis, but I don't want to confuse you too early.
The "hello world" part is called a parameter. A function's behavior depends on the parameters that you send it.
For instance, if you replaced "hello world" with "goodbye cruel world", a different thing would happen.(try it.)
The quotation marks around the words tell the program that "hello world" is a string. A string is text.
Anything goes inside a string. You could type a bunch of keyboard nonsense and the computer would not fault you for it.
Actually not everything. Letters with accents will throw it off. (Among certain other weird letters that you cant type with a keyboard)
Traditionally, the pattern for calling functions is as follows:
function(parameter1,parameter2)
In case it wasn't clear, sometimes you don't need quotations. for instance, when you are passing a number or a variable.x = "my face"
y = 4
print(5,y,x)
I tried to demonstrate all different kinds of parameter passing.By the end of this tutorial, you will all be parameter-passing ninjas.
So what happened?
This one will be a bit tougher to explain.
"x" and "y" are variables. Variables are like containers. They can hold one thing at a time.
Sometimes, though, variables can hold something that holds many things that hold many other things. Thats called a data structure.
Variables can be named whatever you want, so long as the following conditions are met:
- The name contains only a-z,A-Z,_,or 0-9
- The name doesn't start with a number
- The name isn't the same as another python builtin function,class, or variable.
I could go on and on about how to camelCase and under_score your multi-word variables, but it is just preference.
in this case, "y" holds "my face" and "x" holds "4".
When the computer gets to the third line, it doesn't see "print(5,y,x)".
Instead, it sees "print(5,4,"my face")".
Also, the "print" function can accept infinitely many parameters. It just spaces out the parameters in the console/terminal.
Most functions will have some restrictions on how many parameters are passed.
Variables are complicated, and the best way to explain their behavior is with examples.
toprint = "my"
print(toprint)
toprint = "face"
print(toprint)
print("toprint")
Wow, look at that. Are you okay?So what happened?
Every time you assign a value to "toprint", the old one is forgotten.
I also demonstrated the difference that quotations have:
" "toprint" " refers to a string with the word "toprint".
" toprint " refers to a variable named toprint.
This has to be the longest any tutorial has ever gone without introducing addition. Even your math teacher knew to hurry up.
x = 20
print(x+4)
y = 6
print(x+y)
print(x+y+30)
y = y+7
z = (x-34)+y
print(y*z)
*whew*That was a long example.
I had to make sure to demonstrate everything.
So what happened?
I think that whole thing was sort of self-explanatory.
"+" adds, "-" subtracts, "*" multiplies, and "(...)" are used for control over the order of operations.
"/" also divides, but there is special consideration to make sure that the answer hasn't been rounded.
One of the things I try to demonstrate in this tutorial is how everything in python comes back to functions.
the following code is equivalent to the above code:
x = 20
print(x.__add__(4))
y = 6
print(x.__add__(y))
print(x.__add__(y).__add__(30))
y = y.__add__(7)
z = x.__sub__(34).__add__(y)
print(y*z)
I hope that didn't confuse you too much. I think I'll start a habit of ending my tutorials with something harder and then not explain it,so that you will keep it in mind and figure out how it works yourself by learning in future sections of the tutorial.