• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Python] Basics

Ispiro

New Member
Joined
Oct 1, 2007
Messages
129
Reaction score
2
I've already made a tutorial like this for Lua, but just for me, I'm going to make 1 for Python. I'm learning as I write this: I have no prior experience with Python.
In this thread, I'll tell you about variables, if - then checks, functions, and their parameters. Also, information in this thread can be questionable. As I mentioned before, I have no experience with Python, and I'm learning as I type.
I'll be copying the other tutorial, and editing where necessary.
If you already know Lua, this will be easy.

Basics of Programming

Important before starting: TABS ARE A MUST! YOU MUST HAVE THAT LITTLE SPACE BEFORE TEXT!

Well, first I'd like to start off by explaining what's a variable. A variable is a place where you store a value, whether it's a string or number.
You can assign a value to the variable by using the '=' sign, for example:
Code:
variable = 1
Note: the term variable is what you want to refer to the variable by(you'll probably understand what I mean later on in the thread)
You can read variables and check it's contents.
Code:
print variable
Using an interpreter, that should print out 1. But what now? What's the use of assigning a value to a variable?
Well, first and most basic statement used in almost all languages is the if then statement. By reading a variable for it's contents and doing an action according to what it's holding.
Code:
variable = 1
if variable == 1:
	print "variable is equal to 1"
Note: To evaluate a variable, you must use double '='s. You would only use 1 equal sign to assign a value to a variable.
This would print out 'variable is equal to 1', or call for another function instead(we'll get to this part later on, for now, we'll just be using prints).
You can also act upon if the variable wasn't equal to 1, by using 'elseif'.
Code:
variable = 2
if variable == 1:
	print "variable is equal to 1"
elif variable == 2:
	print "variable is equal to 2"
This would print 'variable is equal to 2'. However, what if variable was equal to neither 1 or 2? You can use the 'else' statement here.
Code:
variable = 3
if variable == 1:
	print "variable is equal to 1"
elif variable == 2:
	print "variable is equal to 2"
else:
	print "variable is not equal to 1 or 2"
Note: 'else' statement should be at the bottom of all other checks.
This would print 'variable is not equal to 1 or 2', because the variable was not equal to 1 or 2.

Functions and Parameters
A function is used to perform a task like print text, prints text in the console window. Also, commonly, functions are used to simplify scripts(you'll understand what I mean later on in this thread).

I'll use an example I saw in a C++ tutorial... party invites.
Ofcourse, a program won't create the cards and walkdown the street to the postbox, but just for explaining :p
To invite someone to your party, you'll have to use these statements:
Code:
Write [i]name[/i] on card.
Put card in envelope.
Write [i]name[i]'s address on envelope.
put envelope in postbox
If you're inviting a friend or two, you won't have a problem.
But, what if you're invite 5 friends, or 10? Now you'll be crapping lines of repeated codes.
Code:
Write Pablol on card.
Put card in envelope.
Write Pablol's address on envelope.
put envelope in postbox

Write Colandus on card.
Put card in envelope.
Write Colandus's address on envelope.
put envelope in postbox

Write Zaga on card.
Put card in envelope.
Write Zaga's address on envelope.
put envelope in postbox

Write Helveg on card.
Put card in envelope.
Write Helveg's address on envelope.
put envelope in postbox

Write Kakan on card.
Put card in envelope.
Write Kakan's address on envelope.
put envelope in postbox
Ofcourse, you can create a function instead, making the script 'simpler' and easier to read.
Code:
def inviteFriend(friend):
	Write [u]friend[/u] name on card.
	Put card in envelope.
	Write [u]friend[/u] address on envelope.
	put envelope in postbox

inviteFriend(Kakan)
inviteFriend(Colandus)
inviteFriend(Helveg)
inviteFriend(Zaga)

inviteFriend(Pablol)
Which one is easier to read, and faster to edit? Unless your insane and cross-eyed, I'm pretty sure I know what your answer is.
Now that you know what functions are used for, we get to the part on how functions are created.
First, we'll need to define the function itself.
Code:
def example():
	print 'sometext'
example is the function's name, the name is used to refer to the function itself.
you can call the function by using it's name, which in this case is example.
I'll explain what goes between the paranthesis later on.
Code:
def example():
	print 'sometext'
	
example()
This would print out 'sometext' in console window everytime example() is used.
What if we would like to print out different text? In this case, we'll have to pass parameters to the function with the text we would like to print.
First, we'll need to define a new function that reads the parameter.
Code:
def newExample(textToPrint):
	print textToPrint
Now, we can call the function and tell it to print whatever text we want.
Code:
def newExample(textToPrint):
	print textToPrint
	
newExample('sometext')
newExample('someothertext')
This would print out 2 different phrases, using the same function.
Now, for the last part of my thread...
You can also call a function from inside another.
Code:
def example():
	print 'sometext'
	
def newExample(textToPrint):
	print textToPrint
	
def lastExample(text):
	example()
	newExample(text)

lastExample('this is my last example')
First, the script would call for example(), printing out 'sometext'. Afterwards, the script would call for newExample, printing the parameter given to lastExample, which in this case, is 'this is my last example'.

Alright, hope I helped someone with this. Although it's pretty basic, I could've used something like this when I first started, maybe someone else would need it too.

Comments please.
 
Last edited:
Back
Top