L L Line CSE 420 Computer Games Lecture #4 Working with Data.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
Computer and Programming
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Guide to Programming with Python
INTRODUCING OUR LANGUAGE: C# CHAPTER Topics  The Features of C# –C# is an Interpreted Language –C# is Managed Code –C# is Strongly Typed –C# is.
Lists Introduction to Computing Science and Programming I.
Introduction to Python
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
Game Programming © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line L.
Python.
Game Programming © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line L.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
An Introduction to Textual Programming
Introduction to Python
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Built-in Data Structures in Python An Introduction.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
For loops in programming Assumes you have seen assignment statements and print statements.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Few More Math Operators
String and Lists Dr. José M. Reyes Álamo.
Whatcha doin'? Aims: To start using Python. To understand loops.
EGR 2261 Unit 10 Two-dimensional Arrays
Formatting Output.
Tuples and Lists.
Programming Fundamentals
Containers and Lists CIS 40 – Introduction to Programming in Python
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Barb Ericson Georgia Institute of Technology Dec 2009
MATLAB: Structures and File I/O
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Introduction to Python
Python I/O.
File Handling Programming Guides.
Bryan Burlingame 03 October 2018
Creation, Traversal, Insertion and Removal
Guide to Programming with Python
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Data Structures – 1D Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
String and Lists Dr. José M. Reyes Álamo.
Coding Concepts (Data- Types)
Variables, Data Types & Math
CS190/295 Programming in Python for Life Sciences: Lecture 3
Intro to Computer Science CS1510 Dr. Sarah Diesburg
SSEA Computer Science: Track A
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Introduction to Computer Science
Intro to Computer Science CS1510 Dr. Sarah Diesburg
L L Line CSE 420 Computer Games Lecture #3 Introduction to Python.
L L Line CSE 420 Computer Games Lecture #5 Control Structures.
Class code for pythonroom.com cchsp2cs
Arrays.
Introduction to Computer Science
Presentation transcript:

L L Line CSE 420 Computer Games Lecture #4 Working with Data

Objectives Recognizing different variable types Converting between variable types Creating a basic list List-slicing Adding and removing list items Using for loops to traverse lists Using ranges Testing code with the debugger Building a complete program Lecture #4 Working with Data

Programs Work with Different Types of Data Text is converted to a series of numbers, which are eventually converted to binary code. Integers are numbers without trailing decimal values. They’re efficient but imprecise. Floating point numbers are numbers with decimal points. Lecture #4 Working with Data

Trouble with Numbers Examine baddAdd.py 3 + 7 = 37? Python accepts raw_input data as text. The + (plus sign) concatenates (combines) text values. It actually sees 3 concat 7. So Python gladly combines the two strings, giving 37. Lecture #4 Working with Data

Operator Overloading The plus sign adds numbers. It's also used to combine strings. The string manipulation is called concatenation. For example, 'good ' + 'morning' becomes 'good morning'. Python concatenates strings but adds numbers. Sometimes it guesses wrong. Lecture #4 Working with Data

Converting Strings to Numbers You sometimes have to tell Python what type of data it's dealing with. The int() function is perfect for this: View intAdd.py for a partial fix to the baddAdd.py problem. >>> x = raw_input("give me a number: ") >>> y = int(x) + 10 >>> print y Lecture #4 Working with Data

Another Variable Problem Even with integers, Python sometimes gets confused: 10/4 is 2.5. It definitely isn't 2. Something went wrong again. >>> print 10/4 2 Lecture #4 Working with Data

Integers and Math Integers are values without decimal points. If you don't include a decimal value, Python assumes you're making an integer. Math on integers (in Python) results in integer values. Lecture #4 Working with Data

Floating-Point Values Computers can only approximate real numbers. The most common approximation is called a float (for floating-point real number). Python has a double (double-precision floating point). In Python, all floats are really doubles. Lecture #4 Working with Data

Doing Floating Math If the value has a decimal point, Python automatically makes it a float. If any value is a float, Python makes a float result: >>> print 10 / 4.0 2.5 >>> print 10.0 / 4 Lecture #4 Working with Data

Using the float() Function You can also use the float() function to convert any value to a float: View calc.py for a complete example. >>> print float("4") 4.0 >>> print float(5) 5.0 Lecture #4 Working with Data

Storing Information in Lists Many programs will have large amounts of data. Data can be stored in lists. Python lists are similar to arrays in other languages. Lists have interesting features in Python. Lecture #4 Working with Data

A List Example View inventory.py: A list is surrounded by square braces([]). Items are separated by commas (,). >>> inventory = [ "toothbrush", "suit of armor", "latte espresso", "crochet hook", "bone saw", "towel"] Lecture #4 Working with Data

Extracting Values from a List It’s just like string slicing. Remember, indices come between elements. Also, index starts at zero: >>> print inventory[1:3] ['suit of armor', 'latte espresso'] >>> print inventory[5] towel >>> print inventory[-3] crochet hook Lecture #4 Working with Data

Changing a List You can change the value of a specific element: You can append a new value to the end of a list: You can remove values from the list: More list methods: >>> help("list") inventory[3] = "doily" inventory.append("kitchen sink") inventory.remove("kitchen sink") Lecture #4 Working with Data

Looping Through a List Often, you'll want to work with all the elements in your list. Python has a nice looping mechanism for this kind of thing. See superHero.py. Lecture #4 Working with Data

Introducing the for Loop See superHero.py: >>> heroes = [ "Buffalo Man", "Geek Boy", "Wiffle-Ball Woman" ] >>>for hero in heroes: print "Never fear,", hero, "is here." Never fear, Buffalo Man is here Never fear, Geek Boy is here Never fear, Wiffle-Ball Woman is here Lecture #4 Working with Data

How superHero Works Python creates a normal variable called hero. Non-array variables are sometimes called scalars. The code repeats once per element in the list. Each time through, hero has a new value. Lecture #4 Working with Data

The Python for Loop Requires a list or similar structure. Requires a scalar. Assigns each element to the scalar in turn. Similar to foreach in other languages. The for line ends in a colon (:). Subsequent line(s) are indented. Lecture #4 Working with Data

Indentation and Python In many languages, indentation is purely a matter of style. Python uses indentation to determine how things are organized. You must indent all lines of a loop. Sloppy indentation won’t run. Lecture #4 Working with Data

Using the Debugger The debugger can show exactly what's going on. It's very useful when things go wrong. In the main console window (not the text editor), choose debugger from debug menu. Run program. Lecture #4 Working with Data

Controlling the Debug Console View superHero.py in debug mode. The Debug console shows current position in the program. Use Step to move one step at a time. Watch the progress. Check variables in the Locals window. Use the Quit button to finish the program. Lecture #4 Working with Data

Creating a Range Sometimes you want something to happen a certain number of times. You could make a list of numbers. Technically it's a tuple, not a list, but that discussion can wait. That's what the range() function does: Create a range of 10 values from 0 to 9. >>> print range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Lecture #4 Working with Data

Range() Examples The range() function can take up to three arguments. range(a): Make range from 0 to a-1 range (a, b): Make range from a to b-1 range (a, b, c): Make range from a to b-1, skipping c values each time >>> print range(2,5) [2, 3, 4] >>> print range(5, 30, 5) [5, 10, 15, 20, 25] Lecture #4 Working with Data

Using range() with a Loop The range() function makes it easy to create loops that happen any number of times (like a traditional for loop): >>> for i in range(3): print "now on lap %d" % i now on lap 0 now on lap 1 now on lap 2 Lecture #4 Working with Data

Summary You should now understand Different types of data Handling different types of data in Python Lecture #4 Working with Data

Next Lecture Control Structures Lecture #4 Working with Data

References Andy Harris, “Game Programming, The L Line, The Express Line to Learning”; Wiley, 2007 Lecture #4 Working with Data