comp 171: data types john barr. review - what is computer science? problem solving recognizing...

16
COMP 171: Data Types John Barr

Upload: adela-turner

Post on 04-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

COMP 171: Data Types

John Barr

Page 2: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Review - What is Computer Science?

Problem Solving

Recognizing Patterns

If you can find a pattern in the way you solve different instances of a problem, you can create an algorithm so the computer can solve the problem for you.

Page 3: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Review - What is Computer Science?

An Algorithm

Step-by-step procedure to solve the problem from beginning (start state) to end (end state)

Sequential

Some steps can repeat

High level: only list important details

Page 4: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Values

Fundamental building block in a programming language

Values are the “things” that we save and manipulate

There are only a few basic types of values …but we can make our own types of

values

Page 5: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Data Types in Python

Integers whole numbers, positive or negative (5, 42, -7)

FloatsDecimal numbers (3.14159, 88.3 -5.7)

Stringssequences of characters (‘dog’, “cat”, ‘’’rat’’’,

‘17’, “23.5” )

classes and types are the same thing (for now)

Can use one two or three quotes

Page 6: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Variables A way to name a place in memory for data Legal Names:

No spaces Can’t start with a number

start with letter or underscore Can’t use keywords

middle of "Variables, Expressions and Statements" chapter for list

e.g., class, int Don’t use symbols

$, %, # illegal

Page 7: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Variables

Takes a value, and assigns it a variable

age = 18 #puts the int 18 into the variable age

name = "Joe" #puts the string "Joe" into the variable name

Can be reused age = 87 changes the value in the variable age

Page 8: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Variables

What’s currently in a variable Interpreter: just type variable name and

returnProgram: use the print() function

Page 9: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Input

Takes a string in from the keyboard always a string, even if you enter a number

temp = input() #gets a string and saves into variable temp

Page 10: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Input

Problem: input always returns a string. What if we want to get a number?

age = input(“Enter your age”) type(age)

Must convert to the type we want int (string), float(string), str(int or float)

Page 11: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Debugging – finding errors / faults

syntax errors

runtime errors

semantic errors

Page 12: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Comments

Programs can get very long and convoluted.

Sometimes we’d like to put in some words to explain what we’re doing

python comments start with the # character. everything on the line after the # is ignored

Page 13: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Practice

Write a program that creates a variable for your name and a variable for your age, and prints a sentence like this:

Toby is 32

Page 14: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Practice

Add code to calculate and print your age in decades:

That's 3.2 decades

Page 15: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Practice

Get first name from the keyboard

Page 16: COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve

Practice

Get age from the keyboard