Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Python Basic Tutorial [With Accurate Coloring]

Best Commu NA
Loyal Member
Joined
Sep 12, 2007
Messages
507
Reaction score
38
RaGEZONE's CODE tags are abit buggy, so the tutorial might be abit unclear. If any mod can fix the CODE tags it'd be appreciated. Hope you guys can still read/understand it!
-

Python Basic Tutorial [TUTORIAL FOR PYTHON 2.7.3]
Table of Contents:
1.: Printing a String
2.: Declaring & Using Variables
3.: Changing Variables through User Input
4.: Functions
5.: Create your script
6.: Python Software
-----------------------------------------------

NOTE:
I understand there is much more to Python basics then as followed, however; I'll be covering these for now.


1.: Printing a String

Printing a string in Python is as easy as typing print("<MESSAGE>") or even print '''<MESSAGE>'''. You can also add \n into any line within the " " to start a new line.

Input:

Code:
[COLOR=#800080]print[/COLOR]([COLOR=#008000]"Hello Everyone!"[/COLOR])
[COLOR=#800080]print[/COLOR] [COLOR=#008000]'''Welcome to my Tutorial!'''
[/COLOR][COLOR=#800080]print[/COLOR]([COLOR=#008000]"\nReady to learn?"[/COLOR])


Output:
Hello Everyone!
Welcome to my Tutorial!

Ready to learn?

-----------------------------------------------

2.: Declaring and Using Variables
Declaring variables is pretty easy for most people. There are two basic types of variables, STRING variables and VALUE variables. These can be defined by simply typing the name of your variable followed by the string "in quotations" or your value without quotations. An example of these declarations are as followed:

Code:
[COLOR=#000000]StringVariable = "Mark"
ValueVariable = 20
[/COLOR]VariablesCanBeNamedAnything = "ANYTHING!"

Variables can be named anything, as long as they're in the format <Variable Name> = <Value/String> then you're fine! Now lets use these variables we just set. We could use these variables by calling them in a print statement! To call them in your print statement, just end your quotations and brackets, insert your variable name in between commas, and finish your sentence! Here's an example of us calling and declaring some variables:

Input:
Code:
myName = "Jordan"
myAge = 20
myEyeColor = "blue"

[COLOR=#800080]print[/COLOR]([COLOR=#008000]"Hello! My name is"[/COLOR])[COLOR=#000000],myName,[/COLOR]([COLOR=#008000]"! I love movies and I'm"[/COLOR])[COLOR=#000000],myAge,[/COLOR]([COLOR=#008000]"years old!"[/COLOR])
[COLOR=#800080]print[/COLOR]([COLOR=#008000]"My eye color is")[/COLOR][COLOR=#000000],myEyeColor,[/COLOR][COLOR=#008000]("and my hair color is brown!"[/COLOR])

Output:
Hello! My name is Jordan ! I love movies and I'm 20 years old!
My eye color is blue and my hair color is brown!

-----------------------------------------------

3.: Changing Variables through User Input
Changing variables through user input is used extremely often, and its really easy to learn! Since were dealing with Python version 2.7.3, were going to be using raw_input() instead of input(). To change a STRING variable to a different string, you need to declare your variable as equaling to the input of the user. Heres an example to clear things up:


myvar = "Walmart"
print myvar


The code above will simply print the word "Walmart" onto your screen, lets let the user change that!


myvar = "Walmart"

myvar =
raw_input("My favorite store is: ")
print myvar


This will bring up a prompt, similar to printing a sentence, except its asking the user a question. If the user types "
Cosco" the variable myvar will be changed to Cosco. Now that we know how to change a string, lets change a value! Changing a value is similar to changing a string, except instead of myvar = raw_input() we'll be using myvar = int(raw_input()), lets see this in action!


myvalue = 30

myvalue =
int(raw_input("Type your age: "))
print myvalue


The code above changed the value of myvalue to the input of the user, if the user enters 22, the new value for myvalue will change to 22. If you try to use myvalue =
raw_input() without the int, it will simply make a string called "22" or whatever number was entered. Here's an input and output example:

Input:

yourAge = 18
yourName =
"Mark"

print("Your age is saved as"),yourAge,("! Please enter your new age if this is incorrect")
yourAge =
int(raw_input("Type your new age: "))
print("Your name is saved as"),yourName,("! Please enter a new name if this is incorrect")
yourName =
raw_input("Type your new name: ")
print("Your name is now"),yourName,(" and your age is now"),yourage,


Output:
Your age is saved as 18 ! Please enter a new age if this is incorrect
Type your new age:
23
Your name is saved as Mark ! Please enter a new name if this is incorrect
Type your new name:
Alan
Your name is now Alan and your age is now 23


-----------------------------------------------

4.: Functions
A function is defined by a "def <FUNCTIONNAME>():" these can be used to organize your code or loop/rerun a certain script. Once again the function name can be anything you please. Any lines of code under your function MUST be indented Your Python compiler will NOT run a function until it is called. Lets see an example of a function:


def MyMessage():
print("Hey Everyone!")
print("Hope everyone is good!")
exit()## < ALL THIS DOES IS EXIT THE SCRIPT



The script above is a function. But this function won't run, does anybody know why? ... Its because it wasn't called. We would run this code by doing the following:
def MyMessage():
print("Hey Everyone!")
print("Hope everyone is good!")
exit()## < ALL THIS DOES IS EXIT THE SCRIPT

MyMessage()


The reason the code about will run because as Python reads the script, top to bottom, it sees the function, but it isn't called, so it knows its there, but won't run it yet. As it goes down, it sees "MyMessage()", this tells the script to go to the function called "MyMessage". Calling the function exit() will close your script. We can loop the script above by replacing exit() with MyMessage(), meaning it will print the two messages, then it will see that MyMessage is being called, and it will go back to "
defMyMessage():" and start from the top. Functions must be called from underneath them, as far down as you want; that way the script will notice the function as it going down the script.

If your going to call a variable that's outside of your script, you must add "
global" to the front of the variable name and put it in the function. For an example of this, see next step script.

-----------------------------------------------

5.: Create your Script
Now that we know some basic Python, lets create a script that asks for a users name, age, hair color, weight, and lets make it have a nice welcome message from another function! I'll make this script for you, but you should understand whats going on in it if you read the tutorial up to this point. Alright lets get started:


userName =
"undefined"
userAge = 0
hairColor =
"undefined"
weight = 0

### The information above will change when the user inputs their information.
### so for now, we'll just set everything to 0 and undefined.

def welcome():
print("Welcome to my Script!")
raw_input("Press enter to get started!")
main()

def main():
global userName ## The global vars are so I can change variables outside the function
global userAge
global hairColor
global weight
print("Before we start, I'm going to need some information from you.")
userName =
raw_input("Type your Name: ")
userAge =
int(raw_input("Type your age: "))
hairColor =
raw_input("Type your hair color: ")
weight =
int(raw_input("Type your weight: "))
print("Thank you for your cooperation! Heres the information you entered:")
print("Name:"),userName,
print("Age:"),userAge,
print("Hair Color:"),hairColor,
print("Weight:"),weight,
print("Awesome!")

welcome()
## CALL THE WELCOME FUNCTION ##



-----------------------------------------------

5.: Python Software
You can download Python version 2.7.3 at the link below:
http://www.python.org/getit/

^ Comes with its own compiler, known as "IDLE", if you want a different one, I recommend PythonWin

You can download PythonWin, which is one of my favorite Python compilers below:
http://www.cgl.ucsf.edu/Outreach/pc204/pythonwin.html

---
Thanks for listening guys! I hope I helped!
 
Last edited:
Back
Top