Download Intro to Programming

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
RE
INTRODUCTION TO
PROGRAMMING
Where we hope to teach the fundamentals of programming
and create connecti ons between those elements and
previous experience with Mathematica
BASIC PROGRAMMING
PROGRAMMING LANGUAGES
 Needed as human language is ambiguous
 Computer code composed of:
 Syntax – structure of code
 Semantics - meaning of code
High level
 Python
 C++
 Perl
 Java
 Ruby
Low level
 Machine
 Assembly
 Forth
C
 G-code
M a c h in e I n s t r u c t i o n s :
load the number from memory
location 2001 into the CPU
load the number from memory
location 2002 into the CPU
add the two numbers in the CPU
store the results in location 2003
Python Input:
c=a+b
TRANSLATING HIGH LEVEL LANGUAGES
Translation methods:
1. Compiler
takes high-level code and translated into machine language
2. Interpreter
simulates a computer that understands high-level language
Main Differences
Compiler
Interpreter
 Occur s only once & may be run
many times af ter
 Tend to run faster
 Used ever y time program runs
 Lend to more flexible
programming
PYTHON
“A r e yo u s u g g e s t i n g c o c o n u t s m i g r a te ? ”
“ N o t a t a l l . T h ey c o u l d b e c a r r i e d ”
“ W h a t ? A s wa l l o w c a r r y i n g a c o c o n u t ? ”
“ I t c o u l d g r i p i t by t h e h u s k ! ”
“It's not a question of where he grips it! It's a simple question
o f we i g h t r a t i o s ! A f i v e o u n c e b i r d c o u l d n o t c a r r y a o n e p o u n d
coconut.”
BASIC INFORMATION
Creator: Guido Van Rossum
Where: Netherlands
When: late 1980s
Download Options:








Python 3.3.2
Python 2.7.5
IronPython—running on .Net
Jython—running on Java vir tual machine
PyPy—fast Python implementation with a JIT compiler
Stackless Python—branch of Cpython
Python(x,y)—scientific-oriented Python
…and so on
SNY TAX & BASIC SEMANTICS
>>>
#
print
def name():
Prompt
Used to mark off the start of a comment
returns a desired output to the shell
Used create a new function
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Imported NumPy 1.6.2, SciPy 0.11.0, Matplotlib 1.1.1 + guidata 1.5.1, guiqwt 2.2.1
Type "scientific" for more details.
>>> #>>> is a Python prompt indicating that it is ready for a command.
>>> #A complete command is called a sentence.
>>> print "Hello, World"
Hello, World
>>> def hello():
print “Hello”
>>> hello():
Hello
LOOPS
Definite
 Executed definite no. of times
 For Loop:
for <var> in <sequence>:
<body>
for i in range (10):
print i
Indefinite/Conditional
 Iterates until certain
conditions are met
 While loop:
while <condition>:
<body>
 Danger of infinite loops
 Break out by pressing Ctrl+c
 Ctrl+Alt+Delete if nonresponsive
 If all else fails, restart
computer manually
DECISION STRUCTURES
 If statement:
if <condition>:
<body>
 If/else statement:
if <condition>:
<statement>
else :
<statement>
 If/elif/else statement
if <condition>:
<statement>
elif <condition>:
<statement>
else :
<statement>
Python
Meaning
<
Less than
<=
Less than or equal to
==
Equal to
>=
Greater than or equal to
>
Greater than
!=
Not equal to
EXCEPTION HANDLING
try:
<body>
Except <ErrorType>:
<handler>
def quadSolver(a,b,c):
try:
discrim=math.sqrt(b*b-4*a*c)
root1=(-b+discRoot)/(2*a)
root2=(-b-discRoot)/(2*a)
print "\nThe solutions are:", root1,"&", root2
except ValueError:
print “\nNo real roots.”
ADDITIONAL RESOURCES
Python Resources:
 Code Academy: Python
 Pythonanywhere
 Python Programming: An Introduction to Computer Science
John M. Zelle
Other Coding Resources:
 Code Academy