Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Xi Wang Yang Zhang

2 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

3  Installing Python  https://www.python.org/ https://www.python.org/  Online Learning resources  How to Think Like a Computer Scientist  http://www.openbookproject.net/thinkcs/python/engli sh2e/ http://www.openbookproject.net/thinkcs/python/engli sh2e/  Code Academy  https://www.codecademy.com/tracks/python https://www.codecademy.com/tracks/python  Edx: Introduction to Computer Science and Programming Using Python  https://www.edx.org/ https://www.edx.org/

4 1. Python Shell  Windows: command prompt  Mac: terminal 2. Script 3. Integrated Development Environment (IDE)

5

6 1. Write a program in a text editor and save it as a.py file.

7 2. Run the script in a Python shell.

8

9  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 https://en.wikipedia.org/wiki/Comparison_of_integr ated_development_environments  Example: Enthought Canopy

10

11 1. Variables 2. Functions 3. Conditionals 4. Iteration  Slides and example codes can be downloaded from http://yang-zhang.weebly.com/teaching.html http://yang-zhang.weebly.com/teaching.html

12  Everything in a Python program belongs to a data type: >>> type(5) >>> type(1.23)

13 >>> type(“Hello, World!”) >>> type(‘Hello, Iowa!’) >>> type(‘5’) >>> type(True) >>> type(False)

14  Data types can be changed: >>> float(5) 5.0 >>> str(5) ‘5’ >>> int(1.9) 1 >>> int(‘5’) 5 >>> int(True) 1

15  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

16  Print statement shows the content of a variable: >>> print message What’s up? >>> print n 18 >>> print pi 3.14

17  Variables can be changed: >>> count = 10 >>> count = count – 1 >>> print count 9 >>> count -= 1 >>> print count 8

18  Python is a calculator: >>> x = 10 >>> y = 2 >>> z1 = 3 >>> z2 = 3.0

19 >>> x / y 5 >>> x / z1 3 >>> x /z2 3.3333333333333335

20  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

21  In Python, the syntax for a function definition is: def NAME(PARAMETERS): STATEMENTS Be cautious with indentation!

22  For example: >>> def myMean(a,b): return (a+b)/2 >>> myMean(3,5) 4

23  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

24  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'

25  There are only two Boolean values.  True  False  A Boolean expression returns a Boolean value. >>> 5 == 5 True >>> 5 == 6 False

26 x = 5 y = 6 >>> x == y False >>> x != y True >>> x > y False >>> x < y True What will happen if we type x = y?

27  Logical operators connect multiple Boolean expressions. >>> 3 > 0 and 3 < 5 True >>> 3 < 2 or 3 < 1 False >>> not(5 > 6) True

28  Conditional statements take a form like this: if BOOLEAN EXPRESSION: STATEMENTS >>> x = 5 >>> if x > 0: print 'x is positive.' x is positive.

29  Alternative Execution >>> def isEven(x): if x % 2 == 0: return True else: return False >>> isEven(5) False >>> isEven(6) True

30  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

31  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

32  Iteration is repeated execution of a set of statements. 1. The while statement 2. The for statement

33  The while statement >>> def countdown(n): while n > 0: print n n = n-1 print "Blastoff!" >>> countdown(3) 3 2 1 Blastoff!

34  The for statement >>>for x in 'Iowa': print x I o w a

35  Introduction to Python  Introduction to R  Stata Programming  Introduction to ArcGIS  Multilevel Modeling in R and Stata http://ppc.uiowa.edu/node/3608

36


Download ppt "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."

Similar presentations


Ads by Google