10-14-2009, 04:50 PM
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
YOUR FIRST PYTHON SCRIPT
The Print Command:
This simple but useful function displays text on the screen
This will display:
Variables:
Used to store information, variables can be very helpful
a now equals "hello", so when you
You will see
raw_input:
This function is used to take input from the user of your program
This will display
This will display
Your first python script:
putting everything you just learned together we will make our first script
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"
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
Again this will display nothing
Strings:
Now we need make a string that random.choice will choose a variable or quote from from
This will display nothing
random.choice:
Next we will make random.choice choose a random variable from the array, which was our string
This will display nothing
Magic Eight Ball:
CALCULATOR
Defining your own fuctions:
you can use this to make your own functions
This will display nothing
Math symbols:
The basic sybols for math in python are +, -, *, and /
This will diplay
While loop:
The while loop is used to make your program run a certain amount of times, or an unlimited amount of times
This wil endlessly display
if:
This command means if this is true, do this
This will display
you can do many more things. For example do not equal to
Calculator:
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"
Quote:Hello there
Variables:
Used to store information, variables can be very helpful
Code:
a = "hello"
Code:
print a
Quote:hello
raw_input:
This function is used to take input from the user of your program
Code:
raw_input("Type Something: ")
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: ")
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
Moduel Fuctions:
Different moduels have different fuctions
For this project, we will be using the choice fuction
Code:
random.choice
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']
random.choice:
Next we will make random.choice choose a random variable from the array, which was our string
Code:
random.choice(array)
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):
Math symbols:
The basic sybols for math in python are +, -, *, and /
Code:
x = 2 + 2
print x
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"
Quote:hi
if:
This command means if this is true, do this
Code:
x = 1
if x == 1:
print "hi"
Quote:hiThe 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