Download Running Python Program - Yang Zhang

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
no text concepts found
Transcript
Xi Wang
Yang Zhang
1. Easy to learn
2. Clean and readable codes
3. A lot of useful packages, especially for web
scraping and text mining
4. Growing popularity
 Installing Python
 https://www.python.org/
 Online Learning resources
 How to Think Like a Computer Scientist
 http://www.openbookproject.net/thinkcs/python/engli
sh2e/
 Code Academy
 https://www.codecademy.com/tracks/python
 Edx: Introduction to Computer Science and
Programming Using Python
 https://www.edx.org/
1. Python Shell
 Windows: command prompt
 Mac: terminal
2. Script
3. Integrated Development Environment (IDE)
1. Write a program in a text editor and save it as a
.py file.
2. Run the script in a Python shell.
 An IDE normally consists of a source code editor,
build automation, and a debugger.
 Python IDEs
 https://en.wikipedia.org/wiki/Comparison_of_integr
ated_development_environments
 Example: Enthought Canopy
1. Variables
2. Functions
3. Conditionals
4. Iteration
 Slides and example codes can be downloaded from
http://yang-zhang.weebly.com/teaching.html
 Everything in a Python program belongs to a data
type:
>>> type(5)
<type ‘int’>
>>> type(1.23)
<type ‘float’>
>>> type(“Hello, World!”)
<type ‘str’>
>>> type(‘Hello, Iowa!’)
<type ‘str’>
>>> type(‘5’)
<type ‘str’>
>>> type(True)
<type ‘bool’>
>>> type(False)
<type ‘bool’>
 Data types can be changed:
>>> float(5)
5.0
>>> str(5)
‘5’
>>> int(1.9)
1
>>> int(‘5’)
5
>>> int(True)
1
 A variable is a name that refers to a value.
 The assignment statement creates new variables
and gives them values:
>>> message = “What’s up?”
>>> n = 18
>>> pi = 3.14
 Print statement shows the content of a variable:
>>> print message
What’s up?
>>> print n
18
>>> print pi
3.14
 Variables can be changed:
>>> count = 10
>>> count = count – 1
>>> print count
9
>>> count -= 1
>>> print count
8
 Python is a calculator:
>>> x = 10
>>> y = 2
>>> z1 = 3
>>> z2 = 3.0
>>> x / y
5
>>> x / z1
3
>>> x /z2
3.3333333333333335
 A function is a named sequence of statements that
performs a desired operation.
 Python has many useful built-in functions. For
example:
>>> type(5)
Int
>>> abs(-10)
10
>>> max(1,2,3)
3
 In Python, the syntax for a function definition is:
def NAME(PARAMETERS):
STATEMENTS
Be cautious with indentation!
 For example:
>>> def myMean(a,b):
return (a+b)/2
>>> myMean(3,5)
4
 Default parameter values
>>> def discArea(r, pi=3):
return pi*(r**2)
>>> discArea(2)
12
>>> discArea(2, 3.14)
12.56
>>> discArea(r=2, pi=3.14)
12.56
 A module is a neatly packaged set of functions.
Some come with Python, others need to be
installed.
 For example, to know your current working
directory:
>>> import os
>>> os.getcwd()
'C:\\Users\\Yang'
>>> from os import getcwd
>>> getcwd()
'C:\\Users\\Yang'
 There are only two Boolean values.
 True
 False
 A Boolean expression returns a Boolean value.
>>> 5 == 5
True
>>> 5 == 6
False
x=5
y=6
>>> x == y
False
>>> x != y
True
>>> x > y
False
>>> x < y
True
What will happen if we type x = y?
 Logical operators connect multiple Boolean
expressions.
>>> 3 > 0 and 3 < 5
True
>>> 3 < 2 or 3 < 1
False
>>> not(5 > 6)
True
 Conditional statements take a form like this:
if BOOLEAN EXPRESSION:
STATEMENTS
>>> x = 5
>>> if x > 0:
print 'x is positive.'
x is positive.
 Alternative Execution
>>> def isEven(x):
if x % 2 == 0:
return True
else:
return False
>>> isEven(5)
False
>>> isEven(6)
True
 Conditionals can be chained:
>>> x = 5
>>> y = 6
>>> if x < y:
print x, "is less than", y
elif x > y:
print x, "is greater than", y
else:
print x, "and", y, "are equal"
5 is less than 6
 Conditionals can be nested:
>>> if x == y:
print x, "and", y, "are equal"
else:
if x < y:
print x, "is less than", y
else:
print x, "is greater than", y
5 is less than 6
 Iteration is repeated execution of a set of
statements.
1. The while statement
2. The for statement
 The while statement
>>> def countdown(n):
while n > 0:
print n
n = n-1
print "Blastoff!"
>>> countdown(3)
3
2
1
Blastoff!
 The for statement
>>>
for x in 'Iowa':
print x
I
o
w
a
 Introduction to Python
 Introduction to R
 Stata Programming
 Introduction to ArcGIS
 Multilevel Modeling in R and Stata
http://ppc.uiowa.edu/node/3608