Custom Search

Sunday, October 18, 2009

Python for beginners

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[/code]

YOUR FIRST PYTHON SCRIPT

The Print Command:
This simple but useful function displays text on the screen
[code]print "Hello there"[/code]
This will display:

Variables:
Used to store information, variables can be very helpful
[code]a = "hello"[/code]
a now equals "hello", so when you
[code]print a[/code]
You will see
[quote]hello[/QUOTE]

raw_input:
This function is used to take input from the user of your program
[code]raw_input("Type Something: ")[/code]
This will display
[QUOTE]Type Something: [/quote]
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: ")[/code]
This will display
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
[/code]

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[/code]
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[/code]
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'][/code]
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)[/code]
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)
[/code]

CALCULATOR

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

Math symbols:
The basic sybols for math in python are +, -, *, and /
[code]
x = 2 + 2
print x
[/code]
This will diplay
[quote]4[/QUOTE]

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"
[/code]
This wil endlessly display
[QUOTE]hi[/quote]

if:
This command means if this is true, do this
[code]
x = 1
if x == 1:
print "hi"
[/code]
This will display
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]!=[/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
[/code]

No comments:

Post a Comment