Data Structures and Algorithms Chapter 1
Data Structures and Algorithms Chapter 1
Book:
Problem Solving with Algorithms and Data
Structures By Brad Miller, David Ranum
Chapter 1: Introduction
Objectives
• To review the ideas of computer science,
programming, and problem-solving.
• To understand abstraction and the role it plays
in the problem-solving process.
• To understand and implement the notion of an
abstract data type.
• To review the Python programming language.
Introduction
• Problems
• Problem Solutions
• Algorithms
• Programs
Problems
Decidable Undecidable
Tractable Intractable
Python Introduction
• Python is a modern, easy-to-learn, object-
oriented programming language.
• It has a powerful set of built-in data types and
easy-to-use control constructs.
• Since Python is an interpreted language, it is
most easily reviewed by simply looking at and
describing interactive sessions.
Every piece of data is object
• In python everything is an object
• What is an object:
• It has data (state) and methods (the interface)
• A class defines how an instance of a object
behaves
• Actual data items are called objects
• “an object is a instance of a class”
Built in Atomic types
• Two numeric types:
• int ( holds whole numbers, no limits)
• float (holds floating point numbers – 64 bit )
• Standard operators + - * /
Note that / on two integers will result in float
(2.7 python does integer divide)
• % does modulus as in C++ and java
• // will always do integer divide in python 3+
• ** does power (exponentiation) i.e. 2**8
The None type
• A variable may be assigned the special value
None to indicate it does not have a value
• None evaluates to False
Lets look at examples
•+-
• / in 3.4 create float result
• // does integer divide
• % does modulus
• ** does power
• and or True False not
boolean type and compare ops
• Boolean type has values True and False
• You can use the operators not and or
False or True
Not (False and True)
• You can use all the comparison ops
> >= < <= == and != as in C++ and java
• but you can't use && or || or ! for boolean
operators
Assignment Op
• = is the assignment operator
• a variable is created when its name is used the
first time on the left of a = operator
• the variable will point to the data that is the result
of the expression on the right side.
• It is the data (object) that has the value and type,
not the variable
• Example:
amount = 45.55 # amount variable is created,
and it refers to a float object with value 45.55
assignment with existing
variable
• When a variable already exists, and you assign
a new value to it, then the variable will now
point to the new value.
x = 199 x is created and it is assigned to point to int
object with data 00
theSum
Each assignment creates
a new memory location
for new object
after 1
theSum = 1 type int
theSum
then after 2
theSum = 2 type int
theSum
Each assignment creates
a new memory location
for new object
after 1
theSum = 1 type int
theSum
then after 2
theSum = 2 type int
theSum
then after "abc"
theSum = "abc" type str
theSum
Precedence table from lowest to highest priority
Operator Description
lambda Lambda expression
if – else Conditional expression
or Boolean OR
and Boolean AND
not x Boolean NOT
in, not in, is, is not, Comparisons, including
<, <=, >, >=, !=, == membership tests and identity tests
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
<<, >> Shifts
+, - Addition and subtraction
*, /, //, % Multiplication, division, remainder
+x, -x, ~x Positive, negative, bitwise NOT
** Exponentiation
x[index], x[index:index], Subscription, slicing,
x(arguments...), x.attribute call, attribute reference