mutable [], [1,2,3], ['abc', 53, yes] Other: NoneNone - used to signify the absence of a value Filesopen('data.txt', 'r'), open('sales.txt', 'a') type_variable = constructor(compatible items)"> mutable [], [1,2,3], ['abc', 53, yes] Other: NoneNone - used to signify the absence of a value Filesopen('data.txt', 'r'), open('sales.txt', 'a') type_variable = constructor(compatible items)">
Download presentation
Presentation is loading. Please wait.
Published byWalter Hodges Modified over 8 years ago
1
Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions -> operators -> expressions objects -> data model
2
Data Model -> data is an object that has: a memory location a type a value (could be “empty”) -> type defines: the possible values for that type the operations that the type supports the object’s mutability mutable -> values can change in place immutable -> values can not be changed in place
3
Data Types TYPE (constructor) EXAMPLES Numbers: Boolean (bool)True, False Integer (int)-99, 0, 156 Floating point (float)-34.65, 0.0, 123.67 Sequences: Range (range) range(stop); range(start, stop[, step]) - generates a sequence of numbers Strings (str)"", '123', "Bob's", '''abc's and "yes" ''' Lists (list) -> mutable [], [1,2,3], ['abc', 53, yes] Other: NoneNone - used to signify the absence of a value Filesopen('data.txt', 'r'), open('sales.txt', 'a') type_variable = constructor(compatible items)
4
Numbers integer - have no fractional part - unlimited precision int_variable = int(compatible items) float - have a fractional part - precision depends on system float_variable = float(compatible items) boolean - True, False
5
Numeric Tools Built-in Functions abs(number)return absolute value of number Math Library math.sqrt(number)return square root of number math.pimathematical constant, 3.141592..., to available precision … others as required Random Library random.randint(a, b)return a random integer n such that a <= n <= b random.random()return a random float n such that 0.0 <= n < 1.0 random.uniform(a, b)return a random float n such that a <= n <= b
6
Sequences -items have a left-to-right positional order -index numbering starts at 0 from left -index numbering starts at -1 from right name = "Fred" print(name[0]) fruit = ["apple", "orange", "kiwi"] print(fruit[-1]) data = range(10,101,10) print(data[1])
7
Sequences type_variable = constructor(compatible items) string_variable = str(compatible items) - returns the printable version of items list_variable = list(compatible items) range_variable = range(compatible items)
8
Common Sequence Operations OperationResult x in seq x not in seq True if x is equal to item in seq; ** False if x is equal to item in seq item1 + item2concatenation *** item * nconcatenate n copies of item *** (repetition) seq[ i ]item i of seq, numbering starts at 0 seq[ i:j ] seq[ i:j:k ] slice of seq from i to j slice of seq from i to j with step k len(seq)length of seq Methods seq.index(item)index of the first occurrence of item in seq seq.count(item)number of occurrences of item in seq ** If seq is str, can be used for substring testing, e.g. “gl” in “glow”. *** Not valid for range type.
9
Mutable Sequence Operations OperationResult seq[ i ] = itemreplace contents at position i of seq with item seq[ i:j ] = s seq[ i:j:k ] = s replace slice of seq from i to j (step k) with contents of s del seq[ i:j ] del seq[ i:j:k ] delete elements in the slice from seq Methods seq.append(item)appends item to the end of seq seq.extend(s)appends contents of s to the end of seq seq.insert(i,item)insert item at position i in seq item = seq.pop(i)return and remove element at position i from seq seq.remove(item)find item in seq and remove it (first one)
10
File Operations OperationResult fvn = open(name, 'r')-opens name (str) for reading -file must exist -file pointer points to start of file -returns file variable name fvn = open(name, 'w')-opens/creates name (str) for writing -if file exists, it is overwritten -file pointer points to start of file -returns file variable name fvn = open(name, 'a')-opens/creates name (str) for append -if file exists, add to end of file -file pointer points to end of file -returns file variable name
11
File Operations MethodsResult fvname.close()flushes output buffer to disk and closes the file line = fvn.readline()return next line including newline; file pointer points to next line fvname.seek(n) fvname.seek(0) move to the nth character in file - position file pointer to start of file
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.