31
Introduction to Python Basics of the Language

Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Embed Size (px)

Citation preview

Page 1: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Introduction to Python

Basics of the Language

Page 2: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Install Python• Find the most recent distribution for your

computer at:http://www.python.org/download/releases/

• However, for this class, download and execute Python MSI installer for your platform– For version 2.7

• This version works with your Python textbook!

– You may need to be logged in as administrator to your computer to run the installation

Page 3: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Modes of Interaction with Python• You can interact with the Python interpreter through two built-in modes:

– Basic (command line)– IDLE (may need to download other software!)

• Command line mode:– You can find and double click the Python.exe executable (e.g., python27) in the

downloaded folder

– Make sure to right click it, and then create a shortcut to it, and pin it to the taskbar!

– Click the icon on your taskbar to start the command line

You will get the >>> Python promptType exit at the command prompt to get out of it!>>> exit //or ctrl z

– Note: you can use the Home, Pg up, Pg dn, and End keys, to scroll through previous entries, and repeat them by pressing the Enter key!

Page 4: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Basic Command Line

Page 5: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

IDLE

• This is the integrated development environment for Python

• Involves interactive interpreter with editing and debugging capabilities

• You can find and double click the Pythonw.exe executable (e.g., python27) in the downloaded folder

• Pin it to the taskbar

Page 6: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Python IDLE Shell Window

• The IDLE (Python shell window) is more convenient than the basic python command line

• Provides automatic indentation and highlights the code

• Can use the Home, Pg up, Pg dn, and End keys to search the buffer in your session

Page 7: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Learn Basic parts of Python• Like other languages, Python has many data types– These can be manipulated with operators, functions, and

methods

• We can also make our own types (classes) and instantiate and manipulate them with operators, functions, and our own methods

• It also has conditional and iterative control, e.g.:– if-elif-else– while loop– for loop

Page 8: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Python Naming Style• Function name is lower case

def add (): #adds numbers; returns the result; ends with :

• Variable name is lower case (are case sensetive)inputFieldNamefc #holds a feature class as in: fc=“Roads.shp”Note: variables Road and road are different!

• Class name is UpperCamelCaseClass StrikeSlipFault

• Constant is all caps, e.g. PI, SPREADING_RATE

• Indentation: 4 spaces per level, do not use tabs– IDLE does it for you!

Page 9: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Data types - Practice

10/3 = 3, but 10/3.0 = 3.33333The // operator is for integer division (quotient without remainder). 10//3 = 3 and 4//1.5 = 2, and 13//4 = 3

Page 10: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Use Built in functions

Page 11: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Strings

Page 12: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Strings …

Page 13: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Strings …

Page 14: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Formatting strings with thestring modulus operator %

Page 15: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Booleans

Page 16: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

List

x [:-2] -> [‘hydroxide’, ‘phosphate’, ‘carbonate’] # up to, but not including index -2, i.e., gets rid of the last twox [2:] -> [‘carbonate’, ‘oxide’, ‘silicate’] # values from position 2 to the end (inclusive)

Page 17: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

List Index

P Y T H O N

0 1 2 3 4 5-6 -5 -4 -3 -2 -1

6

lang = [‘PYTHON’] # the whole ‘lang’ listlang [-2] = ‘O’ # at -2lang [2:5] = ‘THO’ # between 2 and 5, not including 5lang [:3] = ‘PYT’ # up to 3, not including 3lang [-2:-4] = ‘TH’ # from -4 and -2lang [-3:] = ‘HON’ # from -3 to the end

Page 18: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Lists have mixed types

• Lists are like Java arrays

Page 19: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

List’s Built-in Functions

Page 20: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Sets: unordered collection of objects

Page 21: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Sets are unordered collection of objects and can be changedDuplicates are removed, even when you add themThe ‘in’ keyword checks for membership in the set

Sets

Page 22: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

if-elif-else

>>> if condition1: # use colon after any statement!body1 # do this if condition is true. Notice the indentation

elif condition2: # notice the colonbody2 # otherwise do this

… # put more elifs if needed else condition:

bodyn # otherwise, if no condition was true except this, do bodyn

Page 23: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

The While Loop

>>> while (condition): # NOTE: condition expression evaluates to true or falsebody # If condition evaluates to true (x<y in the example), it will

executeelse: # optional, rarely used!

post-code # If condition evaluates to false, post-code will execute

• Note: When typing in the IDLE, press enter after (x=x+2) (while the statement is highlighted), and then press backspace key to go to the beginning of line, then press enter to execute the loop!

Page 24: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

While loop …

The r and n are said to be local variables because they are declared inside the function.If you want them to be available to other functions outside, we can declare them to be global, for example:

global r = 1

Page 25: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

The For Loop

• It iterates in order over the items in the iterable sequence (e.g., list, tuple, string)

for item in sequence: # starts from the first element of the sequencebody # if there is an item, body will execute; it goes to the second item

else: # is optional (rarely used as in while loop!)post-code

Page 26: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

The For Loop

• The loop searches the list one item at a time. For each item in the list it checks to see if it is an integer, if not an integer, it skips it, and then checks to see if it divides by eight without a remainder, if it divides by 8, then it prints the message and gets out of the loop!

Page 27: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Loop with specific indices

Page 28: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Function Definition

def function_name (arg1, arg2, …) # arg means argument or parameter!body

# Sometimes we pass a known value for one of the arguments:def function_name (arg1, arg2=default2, arg3) # e.g., power (x, y=3)

body

Page 29: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Function …

Page 30: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

input () function

Page 31: Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:

Built in Numeric Functions

• abs, divmod, float, hex, long, max, min, oct, pow, round

# there are a lot more for trigonometry, etc.

• ‘none’ is a special data type for empty value– e.g., when a function does not return a value– It is used as a placeholder