Advanced RS
Advanced RS
GIS CUSTOMIZATION
(MET607)
Lecture No. 2
PYTHON BASICS
The Python programming language has a wide range of syntactical (syntax)
constructions, standard library functions, and Interactive Development
Environment (IDE) features.
You just need to learn enough to write some handy little programs.
You will, however, have to learn some basic programming concepts before
you can do anything.
Like a wizard (a step-by-step guide) in training, you might think these concepts
seem mysterious and tedious, but with some knowledge and practice, you’ll be
able to command your computer like a magic wand and perform incredible
feats.
PYTHON BASICS
This lecture has a few examples that encourage you to type into the interactive
shell, also called the REPL (Read-Evaluate-Print Loop), which lets you run (or
execute) Python instructions one at a time and instantly shows you the
results.
Using the interactive shell is great for learning what basic Python instructions
do, so give it a try as you follow along.
You’ll remember the things you do much better than the things you only read.
It makes sense that Python wouldn’t understand these expressions: you can’t
multiply two words, and it’s hard to replicate an arbitrary string a fractional
number of times.
ASSIGNMENT STATEMENTS
You’ll store values in variables with an assignment statement.
An assignment statement consists of a variable name, an equal sign
(called the assignment operator), and the value to be stored.
If you enter the assignment statement spam = 42, then a variable
named spam will have the integer value 42 stored in it.
Think of a variable as a labeled box that a value is placed in, as you
can see in the following figure.
ASSIGNMENT STATEMENTS
For example, enter the following into the interactive shell:
ASSIGNMENT STATEMENTS
A variable is initialized (or created) the first time a value is stored in it ➊.
After that, you can use it in expressions with other variables and values ➋.
When a variable is assigned a new value ➌, the old value is forgotten, which
is why spam evaluated to 42 instead of 40 at the end of the example.
This is called overwriting the variable. Enter the following code into the
interactive shell to try overwriting a string:
ASSIGNMENT STATEMENTS
Just like the following box, the spam variable in this example stores 'Hello'
until you replace the string with 'Goodbye'.
VARIABLE NAMES
A good variable name describes the data it contains. Imagine that you moved to
a new house and labeled all of your moving boxes as Stuff.
But in your programs, a descriptive name will help make your code more
readable.
Though you can name your variables almost anything, Python does have some
naming restrictions. Table on next slide has examples of legal variable names.
You can name a variable anything as long as it obeys the following three rules:
It can be only one word with no spaces.
It can use only letters, numbers, and the underscore (_) character.
It can’t begin with a number.
VARIABLE NAMES
VARIABLE NAMES
Variable names are case-sensitive, meaning that spam, SPAM, Spam, and sPaM are
four different variables.
Though Spam is a valid variable you can use in a program, it is a Python convention
to start your variables with a lowercase letter.
Consistency with the style guide is important. But most importantly: know when to
be inconsistent—sometimes the style guide just doesn’t apply. When in doubt, use
your best judgment.
Python ignores comments, and you can use them to write notes or remind yourself
what the code is trying to do. Any text for the rest of the line following a hash mark
(#) is part of a comment.
Sometimes, programmers will put a # in front of a line of code to temporarily remove
it while testing a program. This is called commenting out code, and it can be useful
when you’re trying to figure out why a program isn’t working. You can remove the #
later when you are ready to put the line back in.
Python also ignores the blank line after the comment. You can add as many blank
lines to your program as you want. This can make your code easier to read, like
paragraphs in a book.
The line print('Hello, world!') means “Print out the text in the string 'Hello, world!'.”
When Python executes this line, you say that Python is calling the print() function and
the string value is being passed to the function. A value that is passed to a function
call is an argument. Notice that the quotes are not printed to the screen. They just
mark where the string begins and ends; they are not part of the string value.
This function call evaluates to a string equal to the user’s text, and the line of
code assigns the myName variable to this string value.
You can think of the input() function call as an expression that evaluates to
whatever string the user typed in. If the user entered 'Al', then the expression
would evaluate to myName = 'Al'.
If you call input() and see an error message, like NameError: name 'Al' is not
defined, the problem is that you’re running the code with Python 2 instead of
Python 3.
The print() function isn’t causing that error, but rather it’s the expression you
tried to pass to print().
Python gives an error because the + operator can only be used to add two
integers together or concatenate two strings. You can’t add an integer to a
string, because this is ungrammatical in Python. You can fix this by using a
string version of the integer instead, as explained in the next section.
Because str(29) evaluates to '29', the expression 'I am ' + str(29) + ' years old.'
evaluates to 'I am ' + '29' + ' years old.', which in turn evaluates to 'I am 29 years
old.'. This is the value that is passed to the print() function.
Now you should be able to treat the spam variable as an integer instead of a
string.
The int() function is also useful if you need to round a floating-point number
down.
SUMMARY
You can compute expressions with a calculator or enter string concatenations
with a word processor.
You can even do string replication easily by copying and pasting text.
But expressions, and their component values—operators, variables, and
function calls—are the basic building blocks that make programs.
Once you know how to handle these elements, you will be able to instruct
Python to operate on large amounts of data for you.
It is good to remember the different types of operators (+, -, *, /, //, %, and
** for math operations, and + and * for string operations) and the three data
types (integers, floating-point numbers, and strings) introduced in this lecture.
SUMMARY
We discussed about different functions as well.
The print() and input() functions handle simple text output (to the screen) and
input (from the keyboard).
The len() function takes a string and evaluates to an int of the number of
characters in the string.
The str(), int(), and float() functions will evaluate to the string, integer, or
floating-point number form of the value they are passed.
In the next lecture, you’ll learn how to tell Python to make intelligent decisions
about what code to run, what code to skip, and what code to repeat based on
the values it has. This is known as flow control, and it allows you to write
programs that make intelligent decisions.
PRACTICE QUESTIONS
1. Which of the following are operators, and which are values?
PRACTICE QUESTIONS
3. Name three data types.
4. What is an expression made up of? What do all expressions do?
5. This lecture introduced assignment statements, like spam = 10. What is the
difference between an expression and a statement?
6. What does the variable bacon contain after the following code runs?
THANK YOU