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 different instances of a problem, you can create an algorithm so the computer can solve the problem for you.
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
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
Data Types in Python Integers whole numbers, positive or negative (5, 42, -7) Floats Decimal numbers ( , ) Strings sequences of characters (‘dog’, “cat”, ‘’’rat’’’, ‘17’, “23.5” ) classes and types are the same thing (for now) Can use one two or three quotes
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
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
Variables What’s currently in a variable Interpreter: just type variable name and return Program: use the print() function
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
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)
Debugging – finding errors / faults syntax errors runtime errors semantic errors
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
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
Practice Add code to calculate and print your age in decades: That's 3.2 decades
Practice Get first name from the keyboard
Practice Get age from the keyboard