Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables, Lists, and Objects

Similar presentations


Presentation on theme: "Variables, Lists, and Objects"— Presentation transcript:

1 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

2 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

3 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

4 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

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

6 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

7 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

8 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

9 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

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

11 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

12 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

13 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

14 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

15 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

16 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

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

18 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


Download ppt "Variables, Lists, and Objects"

Similar presentations


Ads by Google