Web to py enterprise web framework - p 5

TYPES 25 For user-defined classes, str and repr can be defined/redefined using the special operators str and repr . These are briefly described later on; for more, refer to the official Python documentation [38]. repr always has a default value. Another important characteristic of a Python string is that, like a list, it is an iterable object. 1 2 3 4 5 6 7 for i in 'hello': print i h e l l o list The main methods of a Python list are append, insert, and delete: 1 2 3 4 5 6 7 8 9 10 a = [1, 2, 3] print type(a). | TYPES 25 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 1 2 3 4 1 2 3 4 5 6 For user-defined classes str and repr can be defined redefined using the special operators str and __repr These are briefly described later on for more refer to the official Python documentation 38 . repr always has a default value. Another important characteristic of a Python string is that like a list it is an iterable object. for i in hello print i h e l l o list The main methods of a Python list are append insert and delete a 1 2 3 print type a type list 8 2 7 del a 0 print a 2 7 3 8 print len a 4 Lists can be sliced print a 3 2 7 3 print a 1 7 3 8 print a -2 3 8 and concatenated a 2 3 b 5 6 print a b 2 3 5 6 A list is iterable you can loop over it a 1 2 3 for i in a print i 1 2 3 26 THE PYTHON LANGUAGE The elements of a list do not have to be of the same type they can be any type of Python object. tuple A tuple is like a list but its size and elements are immutable while in a list they are mutable. If a tuple element is an object the object attributes are mutable. A tuple is delimited by round brackets. 1 a 1 2 3 So while this works for a list 1 a 1 2 3 2 a 1 5 3 print a 4 1 5 3 the element assignment does not work for a tuple 1 a 1 2 3 2 print a 1 32 4 a 1 5 5 Traceback most recent call last 6 File stdin line 1 in module 7 TypeError tuple object does not support item assignment The tuple like the list is an iterable object. Notice that a tuple consisting of a single element must include a trailing comma as shown below 1 a 1 2 print type a 3 type int 4 a 1 5 print type a 6 type tuple Tuples are very useful for efficient packing of objects because of their immutability and the brackets are often optional 1 a 2 3 hello 2 x y z a 3 print x 42 5 print z 6 hello diet A Python dictionary is a hash table that maps a key object to a value object. For example TYPES 27 1 2 3 4 5 6 7 8 9 1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 a k v k2 3 a k v a k2 3 k True .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.