Download Introduction to Python

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Solar micro-inverter wikipedia , lookup

Transcript
Introduction to Python
Sajal Desai
What is Python?
• Versitile, simple, high level language
• No linking and compilation
• “By the way, the language is named after
the BBC show “Monty Python’s Flying
Circus” and has nothing to do with reptiles.
Making references to Monty Python skits
in documentation is not only allowed, it is
encouraged!”
Using the Interpreter
•
•
•
•
•
On Tux, /usr/local/bin/python
quit() or crtl+d to quit
>>> waiting for next command
... continuation lines
# denotes everything trailing on the rest of
the line is commented
Starting Python
python
Python 2.6 (#1, Feb 28 2007, 00:02:06) Type "help", "copyright", "credits" or
"license" for more information. >>>
Some Math!
•
•
The interpreter acts as a simple calculator:
– >>> 7/3
• 2
‘=‘ assigns the value to a variable
– >>> width = 20
– >>> height = 5 * 9
– >>> width * height
• 900
• simultaneously:
– x = y= z = 0
• **Note** Variables must be defined before used!!!
• Integer math, double math, float math all work the same as they do
in C++
• _ is the value of the last variable
Strings!
•
•
•
•
Single or double quotes
\ to display escape characters
– “doesn\’t’
• doesn’t
To continue input across a number of lines:
– use the \ character:
• hello = “Hullo? This is a super long string\
hullo hullo.\
all of this is still going into the variable hello.
– Alternatively use triple quotes “””
• Hello = “””
This is a super super long string
but it is being stored in hello.
“””
“\n” denotes newline, and still must be embedded in a string
Strings (cont.)
•
Use r to have “raw” input
– string = r”Super duper long string\n\n\n”
• “Super duper long string\n\n\n”
•
•
•
•
Concatenated with +
Repeated with *
Strings are indexed, 0 is the start
For substrings, use slice notation
– string[0:6]
• Super d
– string [:3]
• Supe
– string [1:]
• per duper long string\n\n\n
– string[-1]
• n
– string [:-5]
• Super duper long string\
•
**NOTE** Strings cannot be changed
Lists
Comma separated, do not have to be the same
type
– a = [hi, bye, 6]
•
•
•
•
Can be indexed, can use slice, concatenated
Can change
Can use slice to assign, even resize
Can nest strings
– a = [hi, bye, 6, b]
– b = [1,2,3]
if statements
• >>> x = int (raw_input(“Enter: “))
Enter: 42
>>> int x < 0:
x=0
print “Negative changed to 0”
elif x == 0:
print x;
else:
print “X is more than 0”
For statements
• For statements, unlike C++
• Iterates over a sequence of items (think
lists), not over an arithmetic progression
(i++)
• Not wise to modify the list being interated,
make a copy of the list
for x in a[:]:
….stuff….
range()
• Builds a list of arithmetic progressions
– range(10)
• [0,1…10]
• Some useful examples:
– range(5,10)
• [5…10]
– range (0,10,3)
• [0,3,6,9]
– range (-10, -100, -30)
• [-10,-40,-70]
Functions
• def
– function definition keyword
• def fib(n):
– contains def name(list of parameters):
– everything the function contains must be
indented
Functions (cont.)
• Default arguments
–
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
•
can be called any of these ways:
–
–
ask_ok(“Do you really want to quit”)
ask_ok(“Do you really want to quit, 2”)
• Keyword Arguments
–
def parrot(voltage, state='a stiff', action='voom', ='Norwegian Blue'):
•
can be called:
– parrot(1000)
– parrot(action = 'VOOOOOM', voltage = 1000000)
– parrot('a thousand', state = 'pushing up the daisies')
– parrot('a million', 'bereft of life', 'jump')
• DocStrings
– The first line after defining a function is a comment
– Use to write a short description of the function
Conventions
• 4-space indentations
• Lines should not exceed 79 characters
• Use blank lines to separate functions and
classes
• Comment on separate lines
• Use DocStrings
• Use spaces around operators, after commas
• Use CamelCase for classes,
lower_case_with_underscores for functions
Source
• http://docs.python.org/tutorial/index.html