Types and operations

Types and operations

  • Introducing Python objects types: The Python Conceptual hierarchy; Built-in types; Core data types; Numbers; Strings; Lists; Dictionaries; Tuples; Files; Other core types.

Python core data types: numbers, strings, lists, dictionaries, tuples, files and sets. Why are they called core data types?: part of the Python language itself, import module and specific syntax. Definition and types of 'immutable': cannot be changed after it is created, like numbers, strings and tuples, but you can create new ones. Definition and types of 'sequence': Positionally ordered collection of objects; strings, list and tuples; common sequence operation as indexing, concatenation and slicing. Definition and types of 'mapping': maps keys to associated values; access to data stored by key.

  • Numeric types: Numeric type basic; Numbers in action; Other numeric types; numeric extensions.

What tools can you use to find a number's square root, as well as its square?: importing math module '**'. How can you truncate and round a floating-point number?: the int() and math.trunc(), round(N, digits), math.floor(N). How can you convert an integer to a floating-point number?: float(I).

  • Dynamic typing interlude: Missing declaration statements; Shared references; Dynamic typing.

How variables and objects are associated by references; garbage collection shared references to objects can affect multiple variables.

  • String fundamentals: String basics; String literals; Strings in actions; string methods; String formatting expressions; Formatting methods calls; General type categories.

Find method to search a list?:methods are type-specific, single data type. Slice expression in a list?: generic and apply to many types, when you slice you get back a new list. Changing a string in python?: creating new string by concatenating, slicing, formatting, replacing. Extract words from a sentence: slicing and using indexes. Use string module instead of string method calls: it's deprecated.

  • List and dictionaries: Lists (positionally ordered collections of arbitrary objects, freely nested and grown and shrunk on demand); Dictionaries (stores items by key, mutable also). Mutable so support in-place change operations (append call or assign new keys).

2 ways to build a list containing 5 integer zeros. 2 ways to build a dict with 2 keys. 4 operations that change a list object in place: append, sort, insert, pop, del. 4 operations that change a dict object in place: update, d.pop(key). Why might you use a dict instead of a list?: when data is labeled for lookup quicker.

[0]*5; L.append(0); ([0 for i in range(5)])

dict(a=0, b=0) or dict([('a', 0), ('b', 0)]) or dict.fromkeys('ab', 0) or {k:0 for k in 'ab'}

  
  • Tuples, files and everything else: Tuples (sequence operation, immutable); Files (open function for read and write data; pickle, json, struct modules); Core types; Built-in gotchas.

How large a tuple is?: len built-in function. Modify a tuple: they are immutable, you have to generate a new one. What module might you use to store py object without converting them to string?: pickle, struct (binary format), json (string per the json format) module.How might you go about copying all parts of a nested structure at once?: import copy module an call copy.deepcopy(c). When does Python consider an object true?: nonzero number or a nonempty collection object.

Última actualización

¿Te fue útil?