Variables, Lists, and Objects

Slides:



Advertisements
Similar presentations
Generics and the ArrayList Class
Advertisements

Lists Introduction to Computing Science and Programming I.
Road Map Introduction to object oriented programming. Classes
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Games and Simulations O-O Programming in Java The Walker School
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Lists in Python.
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Built-in Data Structures in Python An Introduction.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
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.
String and Lists Dr. José M. Reyes Álamo.
Sections 10.1 – 10.4 Introduction to Arrays
CS1022 Computer Programming & Principles
CS 100: Roadmap to Computing
Data Types and Structures
Lists in Lisp and Scheme
Type Conversion, Constants, and the String Object
Array.
Arrays, For loop While loop Do while loop
Type Conversion, Constants, and the String Object
Bryan Burlingame 03 October 2018
Lists in Python.
More about Numerical Computation
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Arrays in Java What, why and how Copyright Curt Hill.
Data Structures – 1D Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction To Programming Information Technology , 1’st Semester
ARRAYS 1 GCSE COMPUTER SCIENCE.
String and Lists Dr. José M. Reyes Álamo.
Consolidation and Review
Decision Structures and Indefinite Loops
Introduction to Dictionaries
Structures, Unions, and Typedefs
MSIS 655 Advanced Business Applications Programming
Programming Assignment #5
Differences between Java and C
Homework #5 — Monte Carlo Simulation
Elements of a Python Program
More About Functions Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Debuggers and Debugging
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Simple Graphics Package
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
More elements of Python programs
Notes on pyplot Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Note on Program Design Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Notes on Homework #6 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Arrays ICS2O.
CHAPTER 4: Lists, Tuples and Dictionaries
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Java Programming Language
Introduction to Computer Science
Comparing Python and Java
Numpy, pylab, matplotlib (follow-up)
Class code for pythonroom.com cchsp2cs
Introduction to Computer Science
Chapter 4: Loops and Iteration
Introduction to Classes and Objects
Presentation transcript:

Variables, Lists, and Objects Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Before we start Chapter 3 is about Computing with Numbers Talked about numeric data types and conversion Table 3.2 is a useful list of math library functions §3.3 illustrates the factorial function How it is constructed Useful example of counting a for-loop from highest index to lowest Be sure to read thru this chapter so that … … you know where to look when you need to know this stuff during programming CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects “A variable is used to give a name to a value so that we can refer to it at other points in a program.” p. 16 E.g., ints, floats, etc. An assignment is the act of associating a value with that name The association of name and value may change whenever the assignment statement is executed CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables (continued) Variable may refer to other kinds of things beyond simple values Lists Objects Functions Every variable in Python “knows” its type i.e., the kind of thing it currently refers to Unlike many other programming languages Type of a variable is fixed at time program is written! Cannot be changed Only things of right type can be assigned to variable Java, C, C++, etc. CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Questions? CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects A collection of values (and other things) enclosed in square brackets, separated by commas [1, 2, 3, 5, 7, 11, 13, 17, 19, 23] May be all of the same type … Examples … or of different types May be assigned to a variable May be arguments and results of functions! CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Accessing Lists L = [1, 2, 3, 4] Individual list items referred to as List_Name[item_#] Examples List items indexed from zero Error if indexing off end of list Individual list entries may be assigned to List_Name[item_#] = expression Lists may change in size Add elements to beginning, middle, or end Delete elements CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Lists (continued) May be used as control of for-loop for i in [1, 2, 3, 5, 7, 11, 13, 17, 19]: for j in L: The empty list E = [] Many operations on lists What operation needed for HW #2? A list in Python is a little bit like an array in C or other programming languages … … more powerful conceptually … less efficient computationally Where a list had been previously assigned to L Your time is much more valuable than your computer’s time! CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Thinking about Lists A list is (essentially) a collection of variables represented sequentially under one name Each element of a list knows the type of the thing it refers to Just like a variable Each element of a list can be changed independently of other elements List and each element retains value until changed (or forgotten) Not like a variable! What do we mean by “forgotten”? CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Questions? CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Reading assignment:– Chapter 4 Needed for HW #3 And everything else we do in this course! Object: computational abstraction that includes Data Methods (a. k. a. Functions) P. 81:– Objects “know” stuff Objects “do” stuff Objects can refer to other objects Objects can interact with other objects An object may be assigned to a variable Or, equivalently, to an element of a list CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Objects (continued) Objects are organized into Classes All objects in a class have the same kind of information All objects in a class have the same set of methods (a.k.a. functions) Classes are discussed in Chapter 10 Including how to define new classes How to construct objects of a class How to define methods of a class How to define data elements of a class object Probably will not get to this chapter in this course CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Object Notation If X is an object of class C, … … and if class C has a method/function named f, … … then the notation X.f() means “Apply the function f (and its arguments) to the object X” I.e., f may cause the object X to DO something Or f may cause the object X to change what it knows CS-1004, A-Term 2014 Variables, Lists, and Objects

Object Notation (continued) Already encountered similar notation E.g., appending to a list L = [] for x in range(10): L.append(sin(x)) E.g., access members and member functions of a package from matplotlib import pyplot pyplot.plot( <arguments> ) … that is associated with this object, list, class, or package Call the function with this name … CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Creating Objects If C is a class, then the constructor function for that class is also named C V = C(arg1, arg2, …) creates a new object of class C and assigns it to variable V All of the methods associated with class C are now available for object V Examples CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Assigning Objects V = SomeClass(<arguments> ) W = V causes W and V to now refer to the very same object Not to copies of each other, but exactly the same object Incidentally, The same is true for lists. If L is a list, M = L causes M to refer to the same list, not a copy of the list Changes to one are reflected in the other! CS-1004, A-Term 2014 Variables, Lists, and Objects

Variables, Lists, and Objects Questions? CS-1004, A-Term 2014 Variables, Lists, and Objects

Why introduce Objects so early in the course? Because graphics.py is an “object-oriented” package Simple enough to introduce now A chance to do some cool stuff in your first course in programming! CS-1004, A-Term 2014 Variables, Lists, and Objects