ch 5 python

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

using split to extract a list of words in a sentence

>>> sentence = "this example has five words" >>> words=sentence.split() >>> words ['this', 'example', 'has', 'five', 'words'] >>> for index in range (len(words)): >. words[index] = words[index].upper() >>>words ['THIS', 'EXAMPLE', 'HAS', 'FIVE', 'WORDS']

you can create a list using a range

>>> first = [1,2,3,4] >>> second=list(range(1,5)) >>>first [1,2,3,4] >>> second [1,2,3,4]

concatenation + adn equality == work same here

>>> fisrt+[5,6] [1,2,3,4,5,6] >>> first==second true

using for loop to print list

>>> for number in [1,2,3,4]: print (number, end=" ") 1 2 3 4

the funtion len and the subscript operator [] work just as they do for strigs

>>> len(first) 4 >>> first[0] 1 >>> first[2:4} [3,4]

list processing involves replacing each element with the result of applying some operation to that element

>>> numbers =[2,3,4,5] >>>numbers [2,3,4,5] >>> for index in range(len(numbers)): > numbers [index]=numbers[index]**2 >>>numbers [4,9,16,25]

when an element is a number or string, that literal is included in the resulting list

when the element is a variable or any other expression its value is included in the list >>>import math >>> x=2 >>> [x,math.sqrt(x)] [2, 1.4142135623730951] >> [x+1] [3]

list

allows the programmer to manipulate a sequence of data values of any types

if data are immutable strings, aliasing can save on memory

assignment creates an alias to the same object rather than a reference to a copy of the object; to prevent aliasing you can create a new object and copy of the contents of the original to it. example shown on page 142

in a list the first item has the index of 0, and the index of the last item is the length of the list minus 1

each of the items in a list is ordered by position

a list is a sequence of data values called items or elements; an item can be of any type

ex of lists: shopping list; to do list; roster

method insert

expects an integer index and new element as arguments; when index is less than length of list, this method places new element before existing element at the index, after shifting elements to right by one position ; when index is greater than or equal to the length of the list, new element is added to end of list ex: >>> example=[1,2] >>>example [1,2] >>> example.insert(1,10) >>>example [1,10,2] >>> example.insert(3,25) >>> example [1,10,2,25]

the == operator

has no way of distinguishing between two types of relations

sorting a list

natural ordering - arrange elements in numeric or alphabetical order; a list of numbers in ascending order and a list of names in alphabetical order are sorted lisdts; when elemtns cna be related by comparing them for less than and greater than as well as equality they can be sorted; the list method sort mutates a list by arranging elements in ascending order; ex: >>>example = [4,2,10,8] >>> example [4,2,10,8] >>>example.sort() >>>example [2,4,8,10]

dictionary

organizes data values by association with other data values rather than by sequential position

dictionaries

organizes information by association, not position; data structures organized by association are called tables or association lists; in python a dictionary associates a set of keys wit vales; (ex: in webster's dictionary , the keys comprise the set of words, where the associated data values are their definitions)

finding the median

page 144

object identify 143 (example)

property of an object that makes it possible for it to be the same thing at different points in time even though the vales of its attributes might change; the is operator can be used to test for this; it returns true if two operands refer to same object, returns false if operands refer to different objects.

lists and dictionaries

provide powerful ways to organize data in useful and interesting applications

dictionary literals: python dictionary is written as a

sequence of key /value pairs separated by commas; these pairs are called entries; the entire sequence of entries is enclosed in curly braces; a colon separates a key and its value ex: a phone book: {"Savannah":"428-2340", "Nathaniel":"238-5959"}

for the most part any time you see that using a list whose structure will not change,

you can and should use a tuple instead

squaring numbers

146

a list in a list:

[[5,9],9541,78]]

inserting and removing elements in a list 1. L.append(element) 2. L.extend(aList) 3. L.insert(index, element) 4. L.pop() 5. L. pop(index)

1. adds element to the end of L 2. adds the elements of a list to the end of L 3. inserts element at index if index is less than the length of L. otherwise inserts element at end of L 4. removes and returns element at the end of L 5. removes and return element at index

1. L[<an integer expression>] 2. L[<start>:<end>] 3. L1 + L2 4. print (L) 5. len(L) 6. list(range(<upper>)) 7. ==, !=, <, >, <=, >= 8. for <variable> in L: <statement> 9. <any value> in L

1. subscript used to access an element at the given index position 2. slices for a sublist. returns new list 3. list concatenation returns new list consisting of elements in two operands 4. prints literal representation of list 5. returns number of elements in list 6. returns a list containing integers in range 0 through upper number-1 7. compares elemetns at corresponding positions in operand lists; returns true if all results are true, other wise false 8. iterates through list, binding variable to each element 9. returns true if value is in the list or false otherwise.

the return statement

147-148

main function

148-149

adding keys, replacing values, assessing values, removing keys

154-155

traversing a dictionary

155-156

dictionary operations and what it does table

156

hexadecimal

156-157

finding the mode in a list of values

157-158

in operator

>>>3 in [1,2,3] true >>>0 in [1,2,3] false

print a list

>>>print([1,2,3,4] [1,2,3,4]

mutators. 141

a method used to change the value of an attribute of an object; examples are list methods: insert, append, extend, pop , and sort; a mutator method usually returns no value of interest to caller; python automatically returns special value None when a method does not explicitly return a value

alias

a name that refers to the same memory location as another name

aliasing

a situation in which two or more names in a program can refer to the same memory location

ex of list literals:

[1951, 1969, 1984]. #list of integers ["apples", "oranges", "cherreies"] #list of strings [] empty list

side effect

a change in a variable that is the result of some action taken in a program usually from within a method; example on pages 141-142

structural equivalence

a criterion of equality between two distinct object in which one or more of their attributes are equal

a string is immutable, its structure and contents cannot be changed

a list is changeable, it is mutable; elements can be inserted, removed, or replaced

literal string values are written as sequences of characters enclosed by quote marks

a list literal is written as a sequence of data values separated by commas; the entire sequence is enclosed in square brackets ([ ])

method append

simplified version of insert; expects new element as argument and adds new element to end of list; extend method performs simular operation, but adds elements of list argument to end of list; the + poerator builds and returns a brand new list containing elements of two operands, where as append and extend modify list object on which methods are called ex: >>> example = [1,2] >>>example.append(3) >>>example [1,2,3] >>> example.extend([11,12,13]) >>example [1,2,3,11,12,13] >>> example+[14,15] >>> example [1,2,3,11,12,13,14,15]

a parameter is the name used in the function definition for an argument that is passed to the function when it is called

the number and positions of the arguments of a function call should match the number and positions of the parameters in that function's definition

replacing a element in a list

the subscript operator is used to replace an element at a given position >>> example = [1,2,3,4] >>>example [1,2,3,4] >>>example[3]=0 >>>example [1,2,3,0]

tuple

type of sequence that resembles a list, except unlike a list, a tuple is immutable; you can indicate a tuple literal in python by enclosing elements in parentheses instead of brackets; ex: >>> fruits = ("apple", "banana") >>>meats= ("fish", "poultry") >>> food=meats+fruits >>>food ('fish', 'poultry', 'apple', 'banana') >>>veggies=["celery", "beans"] >>> tuple (veggies) ('celery', 'beans')

searching a list

use method index; index raises an exception when target element is not found; you must use in operator to test for presence and then index method if test returns true; ex: aList=[34,45,67] target=45 if target in aList: >. print(aList.index(target)) else: >. print (-1)

method pop

used to removed element at a position; if position is not specified, pop rempves and returns last element; if position is specified, pop removes element at that position and returns it; ex: >>> example [1,2,10,11,12,13] >>> example() 13 >>> example [1,2,10,11,12] >>> example.pop(0) 1 >>> example [2,10,11,12]

boolean functions 148

usually test its argument for the presence or absence of some property; returns true if property is present or false otherwise; the odd function is a boolean function. example on 148


संबंधित स्टडी सेट्स

Applied Marketing Management Quiz 2

View Set

Chapter 3: Genetics, Conception, Fetal Development, and Reproductive Technology by Durham and Chapman, Chapter 15 Physiological and Behavioral Responses of the Neonate, Chapter 16 Discharge Planning and Teaching, Chapter 17 High-Risk Neonatal Nursing...

View Set

Chapter 34. Making It Real: Mozart and Classical Opera, Chapter 33. Disrupting the Conversation: Beethoven and the Symphony in Transition, Chapter 32. Personalizing the Conversation: Beethoven and the Classical Sonata, Music Chapter 31, Music Chapter...

View Set

Unit 1: Foundations of US Democracy

View Set

Chapter 36- Management of patients with musculoskeletal disorders

View Set