Download presentation
Presentation is loading. Please wait.
Published byAdela Turner Modified over 9 years ago
1
COMP 171: Data Types John Barr
2
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.
3
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
4
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
5
Data Types in Python Integers whole numbers, positive or negative (5, 42, -7) Floats Decimal numbers (3.14159, 88.3 -5.7) 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
6
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
7
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
8
Variables What’s currently in a variable Interpreter: just type variable name and return Program: use the print() function
9
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
10
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)
11
Debugging – finding errors / faults syntax errors runtime errors semantic errors
12
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
13
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
14
Practice Add code to calculate and print your age in decades: That's 3.2 decades
15
Practice Get first name from the keyboard
16
Practice Get age from the keyboard
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.