Python

From Halfface
Revision as of 20:00, 10 October 2011 by Ekaanbj (talk | contribs) (→‎list)
Jump to navigation Jump to search

links

http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.6
http://docs.python.org/tutorial

Reference

http://docs.python.org/ref/ref.html

basics

print "Hello, World!"

command called print followed by one argument,which is "Hello, World!". (referred to as a string of characters, or string) Command and its arguments are collectively referred to as a statement,

print "2 + 2 is", 2 + 2

The first argument is the string "2 + 2 is" and the second argument is the mathematical expression 2 + 2, which is commonly referred to as an expression. each of the arguments separated by a comma

sys.argv   arguments are passed to the script in the variable sys.argv
#          is used to start a comment

operations for numbers

Operation Symbol Example
Power (exponentiation) ** 5 ** 2 == 25
Multiplication * 2 * 3 == 6
Division / 14 / 3 == 4
Remainder (modulo) % 14 % 3 == 2
Addition + 1 + 2 == 3
Subtraction - 4 - 3 == 1

userinput

raw_input (returns a string)

user_reply = raw_input("Who goes there? ")

input (returns digits)

number = input("Type in a number: ")

type(number) (tells what a variable is.)

print "number is a", type(number)

while loop

print number interval. finish while loop with a ':' The while statement only affects the lines that are indented with whitespace.

start = input("Start number? ")
stop = input("Stop number? ")
a = start
while a < stop:
    print a
    a = a + 1
operator function
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal
!= not equal
<> another way to say not equal (old style, not recommended)

if statment

 if a > 5:  
   print a, ">", 5
 elif a <= 7:
   print a, "<=", 7
 else:
   print "Neither test was true"
  1. This Program Demonstrates the use of the == operator # using numbers
print 5 == 6 

false

functions

The key feature of this program is the def statement. def (short for define) starts a function definition. def is followed by the name of the function absolute_value. Next comes a '(' followed by the parameter n (n is passed from the program into the function when the function is called). The statements after the ':' are executed when the function is used. The statements continue until either the indented statements end or a return is encountered. The return statement returns a value back to the place where the function was called.

def absolute_value(n):
  if n < 0:
    n = -n
  return n

import

Sleep

import time
time.sleep(secs)

random

import random
number = random.randrange(1, 100, 1)

list

list with more than one value.

which_one = input("What month (1-12)? ")
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
if 1 <= which_one <= 12:
 print "The month is", months[which_one - 1]

Print a whole list.

demolist = ["life", 42, "the universe", 6, "and", 7]
print "demolist = ",demolist

demolist = ['life', 42, 'the universe', 6, 'and', 7]

append function is used to add a new item to the end of the list.

demolist.append("everything")

len returns how many items are in a list. The valid indexes (as in numbers that can be used inside of the []) of a list range from 0 to len - 1.

print "len(demolist) =", len(demolist)

len(demolist) = 7

The index function tells where the first location of an item is located in a list.

print "demolist.index(42) =", demolist.index(42)

print "demolist[1] =", demolist[1]

del command can be used to remove a given element in a list

del demolist[2]

The sort function sorts the list.

demolist.sort()

operations

example explanation
demolist[2] accesses the element at index 2
demolist[2] = 3 sets the element at index 2 to be 3
del demolist[2] removes the element at index 2
len(demolist) returns the length of demolist
"value" in demolist is True if "value" is an element in demolist
"value" not in demolist is True if "value" is not an element in demolist
demolist.sort() sorts demolist
demolist.index("value") returns the index of the first place that "value" occurs
demolist.append("value") adds an element "value" at the end of the list
demolist.remove("value") removes the first occurrence of value from demolist (same as del demolist[demolist.index("value")])