• No results found

Exploring Algorithms with Python

N/A
N/A
Protected

Academic year: 2021

Share "Exploring Algorithms with Python"

Copied!
14
0
0

Loading.... (view fulltext now)

Full text

(1)

Exploring Algorithms with Python

Dr. Chris Mayfield

Department of Computer Science James Madison University

(2)

What is Python?

From Wikipedia:

I General-purpose, high-level programming language

I One line of code translates to many CPU instructions

I Design philosophy emphasizes code readability

I Syntax is minimal; indenting is part of the language

I Supports multiple programming paradigms

I Imperative, object-oriented, procedural, functional, . . .

Other features:

I Interpreted and interactive; very easy to learn

I Extensive standard library (“batteries included”)

(3)

Who uses Python?

I Google (various projects)

I NASA (several projects)

I NYSE (one of only three languages “on the floor”)

I Industrial Light & Magic (everything)

I Yahoo! (Yahoo mail & groups)

I RealNetworks (function and load testing)

I RedHat and Ubuntu (Linux installation tools)

I LLNL, Fermilab (steering scientific applications)

(4)

Installing Python

Note: We will be using Python version 2.7

(the default that currently ships with Mac/Linux)

Linux / Mac: it’s built in!

I Just run python (or idle) from the terminal

Windows:

I Go tohttp://www.python.org/download/

I Python 2.7.8 Windows X86-64 Installer

(5)
(6)

Algorithm representation

Section 5.2 presents four basicprimitives:

1. Assignment

I name←expression 2. Decisions

I if conditionthen activity

I if conditionthen activityelseactivity 3. Loops

I whileconditiondoactivity 4. Functions

(7)

Example using all four

def g r e e t i n g ( h o u r ): if h o u r < 12: p r i n t " G o o d m o r n i n g ! " e l s e: p r i n t " G o o d a f t e r n o o n ! " # c o u n t up to 5 : 00 PM now = h o u r w h i l e now < 18: p r i n t " It ’ s now ", now , " o ’ c l o c k . " now = now + 1 p r i n t " Ti m e to eat ! " # t e s t it out g r e e t i n g (9)

g r e e t i n g ( 1 5 ) NOTE: Files are executed from top to bottom

I The first statement defines a function

(8)

How does Python compare to other languages?

(9)

Python vs Java

Python

I Dynamically typed

(bind names to values)

I Concise: “expressing much

in a few words; implies clean-cut brevity, attained by excision of the superfluous”

# o p e n the f i l e d . txt

m y F i l e = o p e n(" d . txt ")

Java

I Statically typed

(must declare variables)

I Verbose: “abounding in

words; using or containing more words than are necessary” i m p o r t j a v a . io .*; ... // o p e n the f i l e d . txt B u f f e r e d R e a d e r m y F i l e ; m y f i l e = new B u f f e r e d R e a d e r ( new F i l e R e a d e r (" d . txt ")); http://pythonconquerstheuniverse.wordpress.com/category/java-and-python/

(10)

Tips for CS 139/149 students

If you are currently learning Java. . .

// J a v a e x a m p l e int x ; x = 1; if ( x > 0) { S y s t e m . out . p r i n t l n ( " x is p o s i t i v e ! "); } # P y t h o n e x a m p l e x = 1 if x > 0: p r i n t " x is p o s i t i v e ! "

I Forget about declarations

I Forget about semicolons

I But think about colons

I Forget about parentheses

I Forget about braces

(11)
(12)

CS is a creative activity

“Computational thinking is a way humanssolve problems; it is not trying to get humans to think like computers. Computers are dull and boring; humans are clever and imaginative. We humans make computers exciting. Equipped with computing devices, we use our cleverness totackle problemswe would not dare take on before the age of computing and build systems with functionality limited only by our imaginations;”

Jeannette M. Wing, “Computational Thinking,” CACM 2006

(13)

Algorithm discovery

Section 5.3 is a fantastic read:

I The art of problem solving

I Don’t follow steps; take the initiative I Trying examples helps you understand I Let your subconscious work for you

I Getting a foot in the door

I Starting is often the hardest part I Extend knowledge, reverse engineer I Resist the temptation to use formulas!

(14)

Loop control

You don’t need to read all of Section 5.4

I Focus on loop control, pages 224–228

I In particular, Figure 5.7 (components)

# w h i l e l o o p e x a m p l e c o u n t = 3 # i n i t i a l i z e w h i l e c o u n t > 0: # t e s t p r i n t ’ H e l l o ’ c o u n t = c o u n t - 1 # m o d i f y # for l o o p e x a m p l e

for i in r a n g e( 3 ) : # all in one p r i n t ’ H e l l o ’

References

Related documents

[7] found that one can distinguish between three categories of scientific findings, each of which is fed by a distinctive strand and tradition of research: evidence on the

Medium and long latency discharges are rarely seen in the agonist Rf muscle which is stretched actively at the beginning of the pendulum and antagonist Bf muscle which is

While this study will act as an enabler of cultural intelligence, the specific measures that are necessary for the mitigation of high uncertainty avoidance, high power

This study has demonstrated differences in patient, tumour and treatment characteristics as well as in 30-day and/or in-hospital mortality between Sweden and the Nether- lands

While the optimal selection of technology, structure and operating conditions of these subsystems are to be determined with algorithms that are able to handle complicated non-linear

In a survey conducted in 2004, 61% of New Mexicans believed that a permanent, stable source of public funding should be set aside “to protect unique natural lands, wildlife

We thought it would be a good idea to include a sample working script within the pages of this book to emulate what a script might look like at the three crucial stages of