Download cybergis-1-2-hello-world

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
Python Fundamentals: Hello World!
Eric Shook
Department of Geography
Kent State University
The Python Programming Language

Python is an Open Source programming language

It is easy to use and easy to learn how to program

Python website: https://www.python.org/

Additional links:


Documentation: https://docs.python.org/3/

Tutorial: https://docs.python.org/3/tutorial/index.html

Reference Manual: https://docs.python.org/3/reference/index.html
Many online resources:

Google will help
A Python Statement
mystring = "Hello World!"
Statement Breakdown
mystring = "Hello World!"
Variable
Variables can hold values
(think back to math class).
They can be referred by a
name such as x, name or
this_is_a_variable
Statement Breakdown
mystring = "Hello World!"
Assignment
The equals sign assigns
the value:
"Hello World!"
to the variable:
mystring
Statement Breakdown
mystring = "Hello World!"
Value
Variable values can be:
strings such as "hello"
or
numbers such as 123 or 3.14
Statement Breakdown
mystring = "Hello World!"
Statement
A statement is a single command that
tells the computer (Python interpreter)
to do something.
Try it
helloworld.py
mystring = "Hello World!"
Command-line Terminal
$ nano helloworld.py
$ python helloworld.py
Try it
helloworld.py
mystring = "Hello World!"
Command-line Terminal
$ nano helloworld.py
Nothing?
$ python helloworld.py
Print Function Statement Breakdown
print(mystring)
Function call
Arguments
A function is a defined
piece of Python code
that can be called upon
such as a 'print' function
that prints information to
the screen:
function_name(arguments)
Arguments also known as
parameters are passed into
functions. This parameter is
a variable, but strings and
numbers can also be
arguments.
(myvar, "string 1" , 2345, 3.14)
Try it
helloworld.py
mystring = "Hello World!"
print(mystring)
Command-line Terminal
$ nano helloworld.py
Success!
$ python helloworld.py
Hello World!
Case Sensitivity
Python is case-sensitive meaning that
uppercase and lowercase letters may
change the meaning of commands
print
Print
PRINT
pRINT
PrInT
Only 'print'
can be used
to print information
to the screen
http://en.wikipedia.org/wiki/Case_sensitivity
Bugs
Not these bugs
These are
bugs and errors
that you will see
$ python helloworld.py
File "helloworld.py", line 4
^
SyntaxError: invalid syntax
Common Bugs
Attention to detail is vital when programming, because the
smallest error and cause hard-to-find bugs. Each of the lines
below contain one error. Can you spot them all?
mystring="Hello World!")
print(Hello World")
Print(mystring)
mystring="He said "hi" how are you"
Common Bugs Fixed
Attention to detail is vital when programming, because the
smallest error and cause hard-to-find bugs. Each of the lines
below contain one error. Can you spot them all?
mystring="Hello World!")
print("Hello World")
print(mystring)
mystring="He said 'hi' how are you"
Basic Data Types
Integer:
Floating point:
2, 10, -5, 1323455
3.14, 2.1111, -1.0
String:
"Hello World!", "a", "I am tweeting #twitter"
Boolean:
True, False
None
This is a special type that uses the word
None to literally mean there is nothing
here. None is different than "", 0, and 0.0,
which indicate a value for the other types.
Expressions
Variables and numbers can be combined in mathematical
expressions that will be evaluated or calculated. Mathematical
expressions combined with logical statements enable Python
to be used for spatial data processing
Expression
Value
x=2+2
the variable x is equal to the value 4
y=x*3
if x is equal to 4 then the value of y is 12
x=x+2
if x is equal to 4 then the new value of x is 6
x=4–2
the variable x is now equal to the value 2
x=4/2
the variable x is equal to the value 2
Comments
i=0 # This is a comment
# i=1
print "i=",i # Write the value of i
Comments
Comments allow programmers to
leave plain text explanations of
code. Comments are started with
a hash tag (#) and everything after
the '#' is ignored by Python.
The Result
i=0
Try It
1. Create a Python program with 5 variables. Each variable
should be a different basic type
2. Print the value of each variable
3. Try printing a string and a variable like this:
x=5
print("x=",x)
4. Create 3 mathematical expressions and print out the result.
Check to make sure the result is correct.
5. Create 2 variables that have values that are strings. Add the
variables together and print out the result. Is it what you
expect? If the result is unexpected look in the manual to
understand the result.
6. Add a comment to your program. Run it again to see if