Support Forums

Full Version: [TUT] Python Tutorial, Learn To Make Many Different Projects And Fuctions!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
PYTHON FOR BEGINNERS

Chapter 1: Intro
Chapter 2: Your first python script
Chapter 3: Magic eight ball
Chapter 4: Calculator

INTRO

This tutorial is directed to people learning python, and that already have python installed on their computer and know how to execute a script. If you don't, download python from python.org. If you are on Linux or mac, you should already have python and to execute the script you made in text edit and saved as a .py, go into terminal and type
Code:
python scriptname.py

YOUR FIRST PYTHON SCRIPT

The Print Command:
This simple but useful function displays text on the screen
Code:
print "Hello there"
This will display:
Quote:Hello there

Variables:
Used to store information, variables can be very helpful
Code:
a = "hello"
a now equals "hello", so when you
Code:
print a
You will see
Quote:hello

raw_input:
This function is used to take input from the user of your program
Code:
raw_input("Type Something: ")
This will display
Quote:Type Something:
This function isn't very useful without it meaning something, so to do this we will allow a variable to store the information the user inputs
Code:
x = raw_input("Type Something: ")
This will display
Quote:Type Something:
This may look the same, however, adding the "x =" makes whatever the user types equal to x

Your first python script:
putting everything you just learned together we will make our first script
Code:
x = raw_input("Type Something: ")
print x

MAGIC EIGHT BALL

import:
This is used to import moduels
A moduel is basically another script
For your second project we will be using the moduel "random"
Code:
import random.py
This will display nothing, however, it is very important

Moduel Fuctions:
Different moduels have different fuctions
For this project, we will be using the choice fuction
Code:
random.choice
Again this will display nothing

Strings:
Now we need make a string that random.choice will choose a variable or quote from from
Code:
array = ['a','b','c','d','e','f']
This will display nothing

random.choice:
Next we will make random.choice choose a random variable from the array, which was our string
Code:
random.choice(array)
This will display nothing

Magic Eight Ball:
Code:
import random
raw_input("Ask your question: ")
a = ["Yes","No","Ask Again Later","Maybe"]
p = random.choice(a)
print(p)

CALCULATOR

Defining your own fuctions:
you can use this to make your own functions
Code:
def add(a,b):
This will display nothing

Math symbols:
The basic sybols for math in python are +, -, *, and /
Code:
x = 2 + 2
print x
This will diplay
Quote:4

While loop:
The while loop is used to make your program run a certain amount of times, or an unlimited amount of times
Code:
x = 0
while x = 0:
    print "hi"
This wil endlessly display
Quote:hi

if:
This command means if this is true, do this
Code:
x = 1
if x == 1:
    print "hi"
This will display
Quote:hi
The reason there are two equal signs is so there are more options than just =, <, or >. Since you can use two sybols,
you can do many more things. For example do not equal to
Code:
!=


Calculator:
Code:
print "1) Addition"
    print "2) Subtraction"
    print "3) Multiplication"
    print "4) Division"
    print "5) Quit"
    return input ("Choose an operation: ")
def add(a,b):
    print a, "+", b, "=", a + b
def sub(a,b):
    print a, "-", b, "=", a - b
def mul(a,b):
    print a, "x", b, "=", a * b
def div(a,b):
    print a, "/", b, "=", a / b
loop = 1
choice = 0
while loop == 1:
    choice = menu()
    if choice == 1:
        add(input("Add this: "), input("To this: "))
    elif choice == 2:
        sub(input("Subtract this: "), input("From this: "))
    elif choice == 3:
        mul(input("Multiply this: "), input("With this: "))
    elif choice == 4:
        div(input("Divide this: "), input("By this: "))
    elif choice == 5:
        loop = 0
nice Thumbsup

you are realy a supporting face around here Superman
Thanks for this, i'm currently learning python.. Smile
~~~~~bump~~~~~