• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/31

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

31 Cards in this Set

  • Front
  • Back
Python Syntax Basics and Stumbling Blocks:
• Python case sensitive. getItem is distinct from getitem. •Statements execute one after another, unless you say otherwise.
• Python uses indentation to improve readability and mark program sections. Curly braces to delimit sections (as in Java or C++) are not used.
• Consistent indentation is required. Use either spaces or tabs to indent, not a mix of both!
•Start the first line of your file (top-level code) in column 1.
Python Syntax Basics and Stumbling Blocks:
• Compound statements (if, then statements, loops, etc) consist of a header, followed by a colon then followed by indented statements.
• \ is a special character in Python. When describing file locations and directories, use / or \\ to separate directories instead of \
• We will need to distinguish between files with the same name but different extensions, so make sure you turn off “hide file extensions” in Windows!
Interactive execution – the simplest way to execute Python code:
•Calculator Mode
•Type an expression and press Enter and the expression is immediately executed and the result printed.
In Wing-IDE the interactive prompt is the Python Shell window
Statements:
Statements are the most basic building block of Python code.
and include:
• an assignment statement • a print statement • a raw_input statement
Functions:
Functions are how we transform values in Python. For example, note that the raw_input statement returned a string or text value. What if we wanted a number instead? We use a function!
function -eval()
– converts a string to a number
sample assignment statement
x=7
sample print statement
print 'I am a test'
sample raw_input statement
name=raw_input("What is your name?)
The combination of eval and raw_input occurs so frequently that Python provides this combination as a single statement:
Input(“message”)
sample: x=input('type a number")
Comments:
Any text that appears after a hash mark (#) is interpreted as a comment. Comments are ignored by the Python system. Used only to communicate to a human reader.
Good Documentation Practices:
In code documentation in the form of comments is an essential part of programming.
Document the name of the program or function, what parameters it uses and what its outputs are as well as date of creation/modification and author.
Good documentation helps you as much as it helps other programmers!
Storing Data: values, names and data types
• Values are our actual data, and could be a number, text, list or any other data type.
• Names are symbolic references to values. You assign a name to a value with an assignment statement. Named values are often called variables.
• Data values come in different Types, such as Integer, String or List.
• Different data types have different Methods (special functions) that can be used to manipulate them.
Data Types:
Integers, Floating-point numbers (float), Boolean operators, Strings, sets, lists, tuples, dictionaries, classes
Integers
• Counting numbers you are accustomed to from mathematics, both positive and negative.
• No upper bound on the size of integers.
• On some systems, you might see the capital letter L following the output, indicating that the result is long.
Floating-point numbers (float)
Fractional numbers have a decimal point and a fractional part Can also be written in scientific notation
Boolean
• Represents a true or false value • True and False represent Boolean constants • Logical operators (and, or, not) work with Boolean valued expressions
String
Can be delimited by either single or double quotation marks. Wing-IDE prefers you to use single quotes.
Strings are immutable. \ is a special character. When describing file locations and directories, use / or \\ instead
of \. To include \ in a string, precede the string literal with r
for example, these two statements are equivalent: dir_name = ’c:/data’
dir_name = r’c:\data’ You will see the r notation in many of the examples in ESRI’s arcpy documentation.
Assignments:
• Python uses dynamic typing. This means that variable types do not need to be declared before the variable is used. The variable data type is set or reset whenever a value is assigned. For example:
>>>tap='abc'
>>>print tap
abc
>>>tap='2'
>>>print tap
2
initially creates the variable tmp of type string because of the type of data initially assigned to it. However, when reassigned an integer value, the data type switches to integer.
Operators:
• Binary operators recognized by Python: + — * ** / % << >> & | ^ < > <= >= == != <>
• Unary operators: +—~
• Binary and unary operators with text names: and or not in
• Precedence
• Order in which operators are evaluated • Can be controlled using parentheses
• Associativity
• Applies when two operators with the same precedence are used one after another
• Function call notation
• Name of the operation is given first, followed by a list of the arguments surrounded by parentheses
• We have seen this notation before with eval()
Functions:
y = function(arg1, arg2, ...)
• Arguments to a function are first calculated, and then the function is applied.
• Function call syntax is more flexible than the operator syntax.
• Function can take one or more arguments (or may take none at all!)
Other Functions:
• Let’s try some basic functions.
In Wing-IDE set x = 5 and try to set y equal to sin(x), cos(x) or sqrt(x).
• Python is extensible! • We need to load the math module to gain access to these functions.
>>>import math
>>>x=5
>>>print math.sin(x)
-0.95892...
Modules are how we extend Python:
>>> import math >>> x = 5 >>> print math.sin(x) >>> -0.958924274663
• A module is a library of functions used to provide a service
• Import statement
• Tells the Python system you want to use the services provided by a module
• Example: import math
• After the import statement, you can use the functions defined in the module
Dot notation in Python
• Dot (or class) notation
• A name can be qualified by the class or module in which it is defined. This is
necessary when the same name is used to represent different values in different
classes, functions, or modules within a program.
Dot notation can refer to the location of functions, i.e.
moduleName.functionName()
Or of a named value, ie.
modulename.valueName
• The module name can be omitted if you are referring to a function or variable that is defined in the current block of code. ( I really mean in the current namespace, but we will discuss scope and namespaces a little later in the course.)
Programs:
• Advantages to writing a Python program over interactive execution:
A Python program consists of a sequence of statements that have been created and organized to complete a specific task. This series of statements is sometimes referred to as “code” and the process of programming as “coding” or “creating code.”

• Allows execution of the same series of statements over and over
• Programmer creates a Python program that can be executed by somebody else, the user
• Debugging is easier
• The program is an artifact and can be shared
• Executed from the command line or via a Windows shortcut
The Software Development Process
• Step 1: Analyze the problem • Step 2: Create initial specifications
• Steps 3 and 4: Create and implement the design
• Step 5: Test and debug the program
• Step 6: Maintain the program
Lesson 1: Conditionals and Flow Control
Python executes the statements in a file from top to bottom unless we dictate otherwise. We do so with conditional statements (if/then) and flow control structures (for or while loops)
. Writing a program is really all about breaking a process down into discreet sequential steps and then writing statements that will perform those steps in the desired order.
Conditional Statements:
• Choose between two or more alternative possibilities depending upon the outcome of a test
• More than one statement can be controlled by a conditional
• If statements can be nested inside each other