Download Introduction to CSC 231, Python, PyCharm

Document related concepts
no text concepts found
Transcript
CSC 231: Introduction to
Data Structures
Dr. Curry Guinn
Quick Info
• Dr. Curry Guinn
– CIS 2045
– [email protected]
– www.uncw.edu/people/guinnc
– 962-7937
– Office Hours: MTWR: 11:00am-12:00pm
and by appointment
– Teaching Assistant: Paul Murray (hours TBD)
Today
•
•
•
•
•
What is this course?
The Course webpage/Syllabus
How to get an ‘A’
What are Data Structures?
Introduction to PyCharm and some Python
refresher (variables, lists, loops, input/output)
• Hands-on
What is CSC 231?
• Data Structures
– THE Gateway Course
• Gateway to most 300 level CSC courses
• Gateway to our graduate CSC courses
• Not just a gateway course at UNCW – it is the
pivotal course in all undergraduate computer
science programs
Jobs
• When Google interviews, what do they
ask?
– http://grouplens.org/blog/preparing-for-agoogle-technical-interview/
Why Is It So Important?
• How you organize and access information
on computers is fundamental
• Across disciplines within computer science
the same sorts of operations arise again
and again
• This course investigates the best practices
of past scientists trying to solve those
puzzles
How To Approach This Course
Your future CSC courses and your future
career in IT/Computers depends on
mastering this material.
How Do I Earn an ‘A’?
How Do I Earn an ‘A’?
How Do I Master This Material?
• Read the book.
• Come to every lecture/lab.
• Write programs.
– Turn in assignments on time.
– Work independently
– Late assignments lose 10 points per day
• Make use of office hours
– Send me email early and often.
Course and Grading Criteria
Quizzes
10%
1st Midterm Exam
15%
2nd Midterm Exam
15%
Final
35%
Homework
25%
-10 pts. per day
late up to 5 days.
Then ‘F’ for the
course
Let’s Visit the Course Webpage
• You’ll want to bookmark this page on your
computer as you will visit it every day
• You can easily find it by going to my home
page and clicking on the CSC 231 link on
my main page
• http://people.uncw.edu/guinnc/courses/Spr
ing17/231/csc231.htm
– Note that the “Schedule” link near the top of
the page is quite important
The Required Text
• Problem Solving with Algorithms and Data
Structures using Python, by Miller and Ranum,
second edition. Publisher: Franklin Beedle.
• This is available free online at
http://interactivepython.org/courselib/static/pythonds
/index.html
• All the downloads (source code, etc.)
http://www.pythonworks.org/pythonds
PyCharm IDE
• PyCharm IDE - Requires Java and Python 3 to be installed on
your machine. Follow the steps below.
– Download and install the Java Development Kit (jdk), named
Java Platform (JDK) 8u111 or something similar, from here.
– Download and install Python 3.x.0 from here
– Download and install PyCharm from here.
• The Free Community Edition does not require a license
– Configure PyCharm to use the Python 3 interpreter.
The Goal of the Course
Understand and Implement Data Structures
That Have Found to Be Important and
Particularly Useful
An example: Arrays (Python’s implementation
of a list)
Why are arrays useful?
Understanding Data Structures
• What can you do with arrays?
• How do you find something in an array?
• How do we know which algorithm for
finding something in an array is “better”?
• What does “better” mean in this context?
– Analysis of algorithms
Understanding Data Structures
• When are arrays bad?
• Which leads us to linked lists, queues, and
stacks
Understanding Data Structures
• Which leads us to sorting
• Some sorting algorithms depend on
recursion so we’ll need to study that too
Understanding Data Structures
• Linked lists are not so good for searching
and sorting
• Which leads us to binary trees
• Are binary search trees always better than
a linked list?
Understanding Data Structures
• Which leads us to studying how to balance
binary trees
• Are there other useful tree structures?
Understanding Data Structures
• Linear (or sequential) search takes O(n) time
• Binary search takes log(n) time
• Hashing search takes 1 time!!!
• I wonder if there are any drawbacks?
The Basics of Python
The Lecture Portion of Today’s
Python Review
•
•
•
•
•
•
•
•
The Python Interpreter, IDEs
Strings
Variables
Lists
Conditionals
Loops
Input/output
Functions
The Python Interpreter
• Interactive interface to Python
• A simple IDE that is included with
Python is IDLE
• Enter an expression, and Python
evaluates it:
>>> 3*(7+2)
27
24
Non-interactive mode
• Python can also run on a file
% python myfile.py
27
– Where the file 'myfile.py' contains the single line:
print 3*(7+2)
• idle
• interactive GUI for interpreter, editing files and
debugging
• PyCharm is an awesome IDE.
Strings
• A string is a sequence of letters (called
characters).
• In Python, strings start and end with single
or double quotes.
>>> “foo”
‘foo’
>>> ‘foo’
‘foo’
Defining strings
• Each string is stored in the computer’s
memory as a list of characters.
>>> myString = “GATTACA”
myString
Accessing single characters
•
You can access individual characters by using indices in square brackets.
>>> myString = “GATTACA”
>>> myString[0]
‘G’
>>> myString[1]
‘A’
>>> myString[-1]
‘A’
Negative indices start at the end
>>> myString[-2]
of the string and move left.
‘C’
>>> myString[7]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: string index out of range
Accessing substrings
>>> myString = “GATTACA”
>>> myString[1:3]
‘AT’
>>> myString[:3]
‘GAT’
>>> myString[4:]
‘ACA’
>>> myString[3:5]
‘TA’
>>> myString[:]
‘GATTACA’
Special characters
• The backslash is used to
introduce a special character.
>>> "He said, "Wow!""
File "<stdin>", line 1
"He said, "Wow!""
^
SyntaxError: invalid
syntax
>>> "He said, 'Wow!'"
"He said, 'Wow!'"
>>> "He said, \"Wow!\""
'He said, "Wow!"'
Escape
sequence
\\
Meaning
Backslash
\’
Single quote
\”
\n
Double
quote
Newline
\t
Tab
More string functionality
>>> len(“GATTACA”)
7
>>> “GAT” + “TACA”
‘GATTACA’
>>> “A” * 10
‘AAAAAAAAAA
>>> “GAT” in “GATTACA”
True
>>> “AGT” in “GATTACA”
False
← Length
← Concatenation
← Repeat
← Substring test
String methods
• In Python, a method is a function that is
defined with respect to a particular object.
• The syntax is
<object>.<method>(<parameters>)
>>> dna = “ACGT”
>>> dna.find(“T”)
3
String methods
>>> "GATTACA".find("ATT")
1
>>> "GATTACA".count("T")
2
>>> "GATTACA".lower()
'gattaca'
>>> "gattaca".upper()
'GATTACA'
>>> "GATTACA".replace("G", "U")
'UATTACA‘
>>> "GATTACA".replace("C", "U")
'GATTAUA'
>>> "GATTACA".replace("AT", "**")
'G**TACA'
>>> "GATTACA".startswith("G")
True
>>> "GATTACA".startswith("g")
False
String summary
Basic string operations:
S = "AATTGG"
s1 + s2
s2 * 3
s2[i]
s2[x:y]
len(S)
int(S) # or use float(S)
# assignment - or use single quotes ' '
# concatenate
# repeat string
# index character at position 'i'
# index a substring
# get length of string
# turn a string into an integer or floating point decimal
Methods:
S.upper()
S.lower()
S.count(substring)
S.replace(old,new)
S.find(substring)
S.startswith(substring), S. endswith(substring)
Printing:
print(var1,var2,var3)
print("text",var1,"text“)
# print multiple variables
# print a combination of explicit text (strings) and variables
Variables
• A variable is a name for a value.
– Use "=" to assign values to variables.
>>> first_name = 'John'
>>> last_name = 'Smith'
>>> first_name + ' ' + last_name
'John Smith'
– Variable names are case sensitive
– Variable names include only letters,
numbers, and “_”
– Variable names start with a letter or “_”
– Any variable can hold any value (no
typing)
35
Lists
• A list is an ordered set of values
– Lists are written [elt0,
elt1, …, eltn-1]
>>> [1, 3, 8]
>>> ['the', 'king', 'of', ['spain', 'france']]
>>> []
>>> [1, 2, 'one', 'two']
– lst[i] is the ith element of lst.
– Elements are indexed from zero
>>> words = ['the', 'king', 'of', 'spain']
>>> words[0]
'the'
>>> words[2]
'of'
36
Indexing Lists
>>> determiners = ['a', 'b', 'c', ['d', 'e']]
>>> determiners[0]
# 0th element
'a'
# N-2th element
>>> determiners[-2]
'c'
>>> determiners[-1][0]
# sublist access
'd'
>>> determiners[0:2]
# elements in [0, 2)
['a', 'b']
>>> determiners[2:]
# elements in [2, N)
['c', ['d', 'e']]
[
'a' ,
'b' ,
0
1
2
-3
-2
-4
'c' ,
['d', 'e']
3
-1
37
]
Operations on Lists
>>> determiners = ['the', 'an', 'a']
>>> len(determiners)
3
>>> determiners + ['some', 'one']
['the', 'an', 'a', 'some', 'one']
>>> determiners
['the', 'an', 'a']
>>> determiners.index('a')
2
>>> [1, 1, 2, 1, 3, 4, 3, 6].count(1)
3
38
Operations on Lists 2:
List Modification
>>> determiners
['the', 'an', 'a']
>>> del determiners[2]
# remove the element at 2
>>> determiners.append('every')
# insert at the end of the list
>>> determiners.insert(1, 'one') # insert at the given index
>>> determiners
['the', 'one', 'an', 'every']
>>> determiners.sort()
# sort alphabetically
>>> determiners
['an', 'every', 'one', 'the']
>>> determiners.reverse()
# reverse the order
['the', 'one', 'every', 'an']
39
Truth Values
• Every expression has a truth value
• 0 is False, all other numbers are True.
• “” is False, all other strings are True
• [] is False, all other lists are True
>>> 5 == 3+2
True
# == tests for equality
>>> 5 != 3*2
True
# != tests for inequality
>>> 5 > 3*2
False
# >, <, >=, <= test for ordering
>>> 5 > 3*2 or 5<3*2
True
# or, and combine truth values
40
Control Flow
● if statement tests if a condition is
true
– If so, it executes a body
– Otherwise, it does nothing
>>> if len(determiners) > 3:
{
body
...
del determiners[3]
...
print 'deleted the determiner at index 3'
– Indentation is used to mark the body.
– Note the “:” at the end of the if line.
41
Control Flow 2
● if-else statement
>>> if len(sentence) > 3:
...
print sentence
>>> else:
...
print 'too short'
● if-elif statement
>>> if x<3:
...
print x*3
>>> elif x<6:
...
print x*2
>>> else:
...
print x
42
Control Flow 3
while statement
>>> while x<1000:
...
x = x*x+3
for statement
>>> for n in [1, 8, 12]:
...
print n*n+n
range()
>>> for n in range(0, 10):
...
print n*n
Control Flow 4
• break statement
– quits the current loop (for, while)
>>> for x in range(0, 10):
...
print x
...
if x > 5:
...
break
● continue statement
● skips to the next iteration of the loop
44
Working with Files
• To read a file:
>>> for line in open('corpus.txt', 'r').readlines()
...
print line
• To write to a file:
>>> outfile = open('output.txt', 'w')
>>> outfile.write(my_string)
>>> outfile.close()
• Example:
>>> outfile = open('output.txt', 'w')
>>> for line in open('corpus.txt', 'r').readlines():
...
outfile.write(line.replace('a', 'some'))
>>> outfile.close()
45
Functions
• A function is a reusable part of a
program.
• Functions are defined with def
>>> def square(x):
...
return x*x
>>> print square(8)
64
• Optional arguments:
>>> def power(x, exp=2):
# exp defaults to 2
...
if x <= 0: return 1
...
else: return x*power(x, exp-1)
46
The Guided Lab of Today’s
Python Review
1. Turn and face your computer
2. Start PyCharm
3. Under Tools menu, select
– Run Python Console
4. Let’s try a few simple things to make sure
we understand how to work in the
interpreter
Some simple interpreter
interactions
• Do some math
– What is 2 + 2?
– What is 10 / 3? What if I want integer
division?
– 10//3
– How do I compute 210?
– How do I take log2(1024)?
– What does import do?
• What is the difference between “import math” and
“from math import *”?
Some simple assignment and
strings at the interpreter
• Assign a variable to a string
• Find the length of the string
• What is the 4th character?
– Don’t forget that indices start at 0. What is the
index of the 4th character?
• Determine the substring of length 3 that
starts at index 1.
• Is “cat” inside of your string?
• Does your string start with “a”?
Creating and Interpreting a
Python file inside PyCharm
• Under File – select New Project.
• Give your project a name in the dialog
box.
– Notice that dialog box tells you where the
project will be placed on your computer
– You can change that if you want
• Perhaps make it your flash drive
• Or your timmy drive  Preferred
– All user files are periodically deleted from
these lab machines (essentially, every time
it reboots)
Creating a .py file
• In the Project pane, right click on your project name and
choose New Python file.
– Give it a name
– You do not need to add the .py extension
• It will do it automatically
• In the editor window that pops up, type in some python
code
• Save by clicking on the archaic floppy disk icon
• Run – Run
– And then select which file you want to run
Running at the console
• In windows explorer, navigate to the folder that
contains your newly created .py file.
– If you have forgotten where it is, look at the
title bar of PyCharm
• In explorer, shift-RightClick on the folder and
select “Open Command Window Here”.
• Now type “python test.py” or if that doesn’t work
• C:\python34\python test.py
PyCharm is drop-and-drag
• You can drag files into your Python
projects
• And also copy them out of those projects
Let’s do something complicated
• Generate a file (called “numbers.txt”)
containing somewhere between 500 and
1000 random integers between -100 and
100, inclusive.
• Useful:
–
–
–
–
–
–
import random
howMany = random.randrange(500, 1001)
number = random.randrange(-100, 101)
outputfile = open("numbers.txt", "w")
for count in range(0, howMany):
outputfile.write("%s\n" % number)
Lab/Homework 1assignment
• Call your program ComputeAverage
– Read in a file (called “numbers.txt”) of integers and
compute some stats
– Find and print the average
– Find and print the minimum
– Find and print the maximum
– Compute and print the median
– Don’t use any of the built-in statistics methods like
min, max, or sum. You can use sort to compute the
median.
• Submit this program on Blackboard under the
appropriate link
Useful things for Homework 1
input = open("numbers.txt", "r")
for line in input:
x = int(line)
For Next Class, Thursday
• Blackboard Quiz 1 due Tuesday, Jan 17
• No late Blackboard quizzes
– No make up quizzes
• Homework 1 is also due next Tuesday, Jan 17
• Blackboard Quiz 2 due Thursday, Jan 19