Python全栈开发之路 【第二篇】:Python基础之数据类型
本节内容
一、字符串
记住:
- 有序类型:列表,元组,字符串 ---> 都可迭代; 无序类型:字典,集合 ---> 不可迭代;
特性:不可修改
- class str(object):
- """
- str(object='') -> str
- str(bytes_or_buffer[, encoding[, errors]]) -> str
- Create a new string object from the given object. If encoding or
- errors is specified, then the object must expose a data buffer
- that will be decoded using the given encoding and error handler.
- Otherwise, returns the result of object.__str__() (if defined)
- or repr(object).
- encoding defaults to sys.getdefaultencoding().
- errors defaults to 'strict'.
- """
- def capitalize(self): # real signature unknown; restored from __doc__
- """
- 首字母变大写
- S.capitalize() -> str
- Return a capitalized version of S, i.e. make the first character
- have upper case and the rest lower case.
- """
- return ""
- def casefold(self): # real signature unknown; restored from __doc__
- """
- 把整个字符串的所有字符改为小写
- S.casefold() -> str
- Return a version of S suitable for caseless comparisons.
- """
- return ""
- def center(self, width, fillchar=None): # real signature unknown; restored from __doc__
- """
- 原来空格居中,不够用空格补全
- S.center(width[, fillchar]) -> str
- Return S centered in a string of length width. Padding is
- done using the specified fill character (default is a space)
- """
- return ""
- def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
- """
- 从一个范围内的统计某 str 出现次数
- S.count(sub[, start[, end]]) -> int
- Return the number of non-overlapping occurrences of substring sub in
- string S[start:end]. Optional arguments start and end are
- interpreted as in slice notation.
- """
- return 0
- def encode(self, encoding='utf-8', errors='strict'): # real signature unknown; restored from __doc__
- """
- 以encoding指定的编码格式对字符串编码,如果出错默认报一个ValueError,
- 除非errors指定的是 ignore 或 replace
- S.encode(encoding='utf-8', errors='strict') -> bytes
- Encode S using the codec registered for encoding. Default encoding
- is 'utf-8'. errors may be given to set a different error
- handling scheme. Default is 'strict' meaning that encoding errors raise
- a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
- 'xmlcharrefreplace' as well as any other name registered with
- codecs.register_error that can handle UnicodeEncodeErrors.
- """
- return b""
- def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
- """
- 检查字符串是否以 suffix 子字符串结束,start和end 表示范围,可选
- S.endswith(suffix[, start[, end]]) -> bool
- Return True if S ends with the specified suffix, False otherwise.
- With optional start, test S beginning at that position.
- With optional end, stop comparing S at that position.
- suffix can also be a tuple of strings to try.
- """
- return False
- def expandtabs(self, tabsize=8): # real signature unknown; restored from __doc__
- """
- 将字符串中包含的 \t 转换成tabsize个空格
- S.expandtabs(tabsize=8) -> str
- Return a copy of S where all tab characters are expanded using spaces.
- If tabsize is not given, a tab size of 8 characters is assumed.
- """
- return ""
- def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
- """
- 检查sub是否包含在字符串中,如果有 返回索引值,否则返回-1
- S.find(sub[, start[, end]]) -> int
- Return the lowest index in S where substring sub is found,
- such that sub is contained within S[start:end]. Optional
- arguments start and end are interpreted as in slice notation.
- Return -1 on failure.
- """
- return 0
- def format(self, *args, **kwargs): # known special case of str.format
- """
- 格式化输出
- 三种形式:
- 形式一:
- >>>print('{0}{1}{0}'.format('a','b'))
- aba
- 形式二:(必须一一对应)
- >>>print('{}{}{}'.format('a','b'))
- Traceback (most recent call last):
- File "<input>", line 1, in <module>
- IndexError: tuple index out of range
- >>>print('{}{}'.format('a','b'))
- ab
- 形式三:
- >>>print('{name} {age}'.format(age=18,name='hyp'))
- hyp 18
- S.format(*args, **kwargs) -> str
- Return a formatted version of S, using substitutions from args and kwargs.
- The substitutions are identified by braces ('{' and '}').
- """
- pass
- def format_map(self, mapping): # real signature unknown; restored from __doc__
- """
- 与 format 区别
- '{name}'.format(**dict(name='hyp'))
- '{name}'.format_map(dict(name='hyp'))
- S.format_map(mapping) -> str
- Return a formatted version of S, using substitutions from mapping.
- The substitutions are identified by braces ('{' and '}').
- """
- return ""
- def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
- """
- 跟find一样,当索引值不存在时,会产生一个异常
- S.index(sub[, start[, end]]) -> int
- Return the lowest index in S where substring sub is found,
- such that sub is contained within S[start:end]. Optional
- arguments start and end are interpreted as in slice notation.
- Raises ValueError when the substring is not found.
- """
- return 0
- def isalnum(self): # real signature unknown; restored from __doc__
- """
- 至少一个字符,且都是字母或数字才返回True
- S.isalnum() -> bool
- Return True if all characters in S are alphanumeric
- and there is at least one character in S, False otherwise.
- """
- return False
- def isalpha(self): # real signature unknown; restored from __doc__
- """
- 至少一个字符,且都是字母才返回True
- S.isalpha() -> bool
- Return True if all characters in S are alphabetic
- and there is at least one character in S, False otherwise.
- """
- return False
- def isdecimal(self): # real signature unknown; restored from __doc__
- """
- 字符串只包含十进制数字 则返回True
- S.isdecimal() -> bool
- Return True if there are only decimal characters in S,
- False otherwise.
- """
- return False
- def isdigit(self): # real signature unknown; restored from __doc__
- """
- 字符串只包含数字 则返回True
- S.isdigit() -> bool
- Return True if all characters in S are digits
- and there is at least one character in S, False otherwise.
- """
- return False
- def isidentifier(self): # real signature unknown; restored from __doc__
- """
- 字符串为关键字 则返回True
- S.isidentifier() -> bool
- Return True if S is a valid identifier according
- to the language definition.
- Use keyword.iskeyword() to test for reserved identifiers
- such as "def" and "class".
- """
- return False
- def islower(self): # real signature unknown; restored from __doc__
- """
- 字符串中至少包含一个区分大小写的字符,并且字符都是小写,则返回True,否则False
- S.islower() -> bool
- Return True if all cased characters in S are lowercase and there is
- at least one cased character in S, False otherwise.
- """
- return False
- def isnumeric(self): # real signature unknown; restored from __doc__
- """
- 字符串只包含数字字符,则返回True
- S.isnumeric() -> bool
- Return True if there are only numeric characters in S,
- False otherwise.
- """
- return False
- def isprintable(self): # real signature unknown; restored from __doc__
- """
- S.isprintable() -> bool
- Return True if all characters in S are considered
- printable in repr() or S is empty, False otherwise.
- """
- return False
- def isspace(self): # real signature unknown; restored from __doc__
- """
- 字符串只包含空格,则返回True
- S.isspace() -> bool
- Return True if all characters in S are whitespace
- and there is at least one character in S, False otherwise.
- """
- return False
- def istitle(self): # real signature unknown; restored from __doc__
- """
- 字符串是标题化(所有单词都以大写开头,其余字母小写),则返回True
- S.istitle() -> bool
- Return True if S is a titlecased string and there is at least one
- character in S, i.e. upper- and titlecase characters may only
- follow uncased characters and lowercase characters only cased ones.
- Return False otherwise.
- """
- return False
- def isupper(self): # real signature unknown; restored from __doc__
- """
- 字符串中至少包含一个区分大小写的字符,并且字符都是大写,则返回True,否则False
- S.isupper() -> bool
- Return True if all cased characters in S are uppercase and there is
- at least one cased character in S, False otherwise.
- """
- return False
- def join(self, iterable): # real signature unknown; restored from __doc__
- """
- 以字符串作为分隔符,插入到sub中所有的字符之间
- S.join(iterable) -> str
- Return a string which is the concatenation of the strings in the
- iterable. The separator between elements is S.
- """
- return ""
- def ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__
- """
- 返回一个左对齐的字符串,并使空格填充至长度为width的新字符串
- S.ljust(width[, fillchar]) -> str
- Return S left-justified in a Unicode string of length width. Padding is
- done using the specified fill character (default is a space).
- """
- return ""
- def lower(self): # real signature unknown; restored from __doc__
- """
- 转换字符串中所有大写字符为小写
- S.lower() -> str
- Return a copy of the string S converted to lowercase.
- """
- return ""
- def lstrip(self, chars=None): # real signature unknown; restored from __doc__
- """
- 去掉字符串左边的所有空格
- S.lstrip([chars]) -> str
- Return a copy of the string S with leading whitespace removed.
- If chars is given and not None, remove characters in chars instead.
- """
- return ""
- def maketrans(self, *args, **kwargs): # real signature unknown
- """
- Return a translation table usable for str.translate().
- If there is only one argument, it must be a dictionary mapping Unicode
- ordinals (integers) or characters to Unicode ordinals, strings or None.
- Character keys will be then converted to ordinals.
- If there are two arguments, they must be strings of equal length, and
- in the resulting dictionary, each character in x will be mapped to the
- character at the same position in y. If there is a third argument, it
- must be a string, whose characters will be mapped to None in the result.
- """
- pass
- def partition(self, sep): # real signature unknown; restored from __doc__
- """
- 找到字符串,把字符串分成一个3元组,如果子字符串不存在,则返回('原字符串','','')
- S.partition(sep) -> (head, sep, tail)
- Search for the separator sep in S, and return the part before it,
- the separator itself, and the part after it. If the separator is not
- found, return S and two empty strings.
- """
- pass
- def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
- """
- 把字符串中的old子字符串替换成new子字符串,
- 如果count指定,则替换不超过count次。
- S.replace(old, new[, count]) -> str
- Return a copy of S with all occurrences of substring
- old replaced by new. If the optional argument count is
- given, only the first count occurrences are replaced.
- """
- return ""
- def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
- """
- 从右边开始查找,与find类似
- S.rfind(sub[, start[, end]]) -> int
- Return the highest index in S where substring sub is found,
- such that sub is contained within S[start:end]. Optional
- arguments start and end are interpreted as in slice notation.
- Return -1 on failure.
- """
- return 0
- def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
- """"
- 从右边开始查找,与index类似
- S.rindex(sub[, start[, end]]) -> int
- Return the highest index in S where substring sub is found,
- such that sub is contained within S[start:end]. Optional
- arguments start and end are interpreted as in slice notation.
- Raises ValueError when the substring is not found.
- """
- return 0
- def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__
- """
- S.rjust(width[, fillchar]) -> str
- Return S right-justified in a string of length width. Padding is
- done using the specified fill character (default is a space).
- """
- return ""
- def rpartition(self, sep): # real signature unknown; restored from __doc__
- """
- 与partition()方法类似
- S.rpartition(sep) -> (head, sep, tail)
- Search for the separator sep in S, starting at the end of S, and return
- the part before it, the separator itself, and the part after it. If the
- separator is not found, return two empty strings and S.
- """
- pass
- def rsplit(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
- """
- S.rsplit(sep=None, maxsplit=-1) -> list of strings
- Return a list of the words in S, using sep as the
- delimiter string, starting at the end of the string and
- working to the front. If maxsplit is given, at most maxsplit
- splits are done. If sep is not specified, any whitespace string
- is a separator.
- """
- return []
- def rstrip(self, chars=None): # real signature unknown; restored from __doc__
- """
- 删除字符串末尾的空格
- S.rstrip([chars]) -> str
- Return a copy of the string S with trailing whitespace removed.
- If chars is given and not None, remove characters in chars instead.
- """
- return ""
- def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
- """
- 不带参数默认是以空格为分隔符切片字符串,
- 如果 maxsplit 参数有设置,则仅分隔 maxsplit 个字符串,返回切片后的子字符串拼接的列表
- S.split(sep=None, maxsplit=-1) -> list of strings
- Return a list of the words in S, using sep as the
- delimiter string. If maxsplit is given, at most maxsplit
- splits are done. If sep is not specified or is None, any
- whitespace string is a separator and empty strings are
- removed from the result.
- """
- return []
- def splitlines(self, keepends=None): # real signature unknown; restored from __doc__
- """
- 按照'\n'分隔,返回一个包含隔行作为元素的列表,
- 如果 keepends 参数指定,则返回前 keepends 行
- S.splitlines([keepends]) -> list of strings
- Return a list of the lines in S, breaking at line boundaries.
- Line breaks are not included in the resulting list unless keepends
- is given and true.
- """
- return []
- def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__
- """
- 检查字符串是否以 prefix 开头,是则返回True,
- start 和 end 可以指定检查范围
- S.startswith(prefix[, start[, end]]) -> bool
- Return True if S starts with the specified prefix, False otherwise.
- With optional start, test S beginning at that position.
- With optional end, stop comparing S at that position.
- prefix can also be a tuple of strings to try.
- """
- return False
- def strip(self, chars=None): # real signature unknown; restored from __doc__
- """
- 删除字符串前边和或变得所有空格,
- S.strip([chars]) -> str
- Return a copy of the string S with leading and trailing
- whitespace removed.
- If chars is given and not None, remove characters in chars instead.
- """
- return ""
- def swapcase(self): # real signature unknown; restored from __doc__
- """
- 翻转字符串中的大小写
- S.swapcase() -> str
- Return a copy of S with uppercase characters converted to lowercase
- and vice versa.
- """
- return ""
- def title(self): # real signature unknown; restored from __doc__
- """
- 返回标题化的字符串
- S.title() -> str
- Return a titlecased version of S, i.e. words start with title case
- characters, all remaining cased characters have lower case.
- """
- return ""
- def translate(self, table): # real signature unknown; restored from __doc__
- """
- S.translate(table) -> str
- Return a copy of the string S in which each character has been mapped
- through the given translation table. The table must implement
- lookup/indexing via __getitem__, for instance a dictionary or list,
- mapping Unicode ordinals to Unicode ordinals, strings, or None. If
- this operation raises LookupError, the character is left untouched.
- Characters mapped to None are deleted.
- """
- return ""
- def upper(self): # real signature unknown; restored from __doc__
- """
- 转字符串中所有小写字符为大写
- S.upper() -> str
- Return a copy of S converted to uppercase.
- """
- return ""
- def zfill(self, width): # real signature unknown; restored from __doc__
- """
- 返回长度为width的字符串,原字符串右对齐,前面用0填充
- S.zfill(width) -> str
- Pad a numeric string S with zeros on the left, to fill a field
- of the specified width. The string S is never truncated.
- """
- return ""
- def __add__(self, *args, **kwargs): # real signature unknown
- """ Return self+value. """
- pass
- def __contains__(self, *args, **kwargs): # real signature unknown
- """ Return key in self. """
- pass
- def __eq__(self, *args, **kwargs): # real signature unknown
- """ Return self==value. """
- pass
- def __format__(self, format_spec): # real signature unknown; restored from __doc__
- """
- S.__format__(format_spec) -> str
- Return a formatted version of S as described by format_spec.
- """
- return ""
- def __getattribute__(self, *args, **kwargs): # real signature unknown
- """ Return getattr(self, name). """
- pass
- def __getitem__(self, *args, **kwargs): # real signature unknown
- """ Return self[key]. """
- pass
- def __getnewargs__(self, *args, **kwargs): # real signature unknown
- pass
- def __ge__(self, *args, **kwargs): # real signature unknown
- """ Return self>=value. """
- pass
- def __gt__(self, *args, **kwargs): # real signature unknown
- """ Return self>value. """
- pass
- def __hash__(self, *args, **kwargs): # real signature unknown
- """ Return hash(self). """
- pass
- def __init__(self, value='', encoding=None, errors='strict'): # known special case of str.__init__
- """
- str(object='') -> str
- str(bytes_or_buffer[, encoding[, errors]]) -> str
- Create a new string object from the given object. If encoding or
- errors is specified, then the object must expose a data buffer
- that will be decoded using the given encoding and error handler.
- Otherwise, returns the result of object.__str__() (if defined)
- or repr(object).
- encoding defaults to sys.getdefaultencoding().
- errors defaults to 'strict'.
- # (copied from class doc)
- """
- pass
- def __iter__(self, *args, **kwargs): # real signature unknown
- """ Implement iter(self). """
- pass
- def __len__(self, *args, **kwargs): # real signature unknown
- """ Return len(self). """
- pass
- def __le__(self, *args, **kwargs): # real signature unknown
- """ Return self<=value. """
- pass
- def __lt__(self, *args, **kwargs): # real signature unknown
- """ Return self<value. """
- pass
- def __mod__(self, *args, **kwargs): # real signature unknown
- """ Return self%value. """
- pass
- def __mul__(self, *args, **kwargs): # real signature unknown
- """ Return self*value.n """
- pass
- @staticmethod # known case of __new__
- def __new__(*args, **kwargs): # real signature unknown
- """ Create and return a new object. See help(type) for accurate signature. """
- pass
- def __ne__(self, *args, **kwargs): # real signature unknown
- """ Return self!=value. """
- pass
- def __repr__(self, *args, **kwargs): # real signature unknown
- """ Return repr(self). """
- pass
- def __rmod__(self, *args, **kwargs): # real signature unknown
- """ Return value%self. """
- pass
- def __rmul__(self, *args, **kwargs): # real signature unknown
- """ Return self*value. """
- pass
- def __sizeof__(self): # real signature unknown; restored from __doc__
- """ S.__sizeof__() -> size of S in memory, in bytes """
- pass
- def __str__(self, *args, **kwargs): # real signature unknown
- """ Return str(self). """
- pass
字符串源码
常用字符串操作:
- # 1、casefold() : 将字符串的所有字符变小写。
- s = "HYPlcy"
- print(s.casefold()) # 结果:hyplcy
- # 2、count(sub[,start[,end]]) : 查找子字符串出现的次数
- str1 = "asdfgasdfgasert"
- print(str1.count("as"))
- # 3、index(sub[,start[,end]]) : 查找子字符串在该字符串中的位置,找到了返回第一个字符的索引值
- # 找不到会抛出异常。与find(sub[,start[,end]])类似,找不到返回-1.
- str2 = "Welcome hyp"
- print(str2.index("lc"))
- print(str2.index("lc",2,6))
- print(str2.find("lc",1,6))
- print(str2.find("lc",2,6))
- # 4、join() : 连接字符串
- str3 = '-'.join(['hyp','lcy','abc'])
- print(str3)
- # 5、replace(old,new[,count]) : 替换指定的字符串
- str4 = "hello hyp"
- print(str4.replace("hyp","lcy"))
- # 6、split(): 用于拆分字符串
- str5 = "h_y_p_l"
- print(str5.split('_'))
- # 7、capitalize(): capitalize方法判断字符串的首位是否为字母,是则 将该字母大写,其余字母小写
- str6 = "hyp"
- print(str6.capitalize())
- # 8、center(): 设置宽度,并将内容居中
- str7 = "hyp"
- print(str7.center(20))
- print(str7.center(20,"-"))
- # 9、startswith,endswith 以什么开头、结尾
- str8 = "hyplcy"
- print(str8.startswith("hy"))
- print(str8.endswith("cy"))
- # 10、format(): 格式化,将格式化的占位符替换为指定的值
- str9 = 'I am {name} ,{age}'
- print(str9.format(name = 'hyp',age = '18'))
- # 11、len、isnumeric、 isdigit、upper、swapcase(大小写互换)......
二、列表
- class list(object):
- """
- list() -> new empty list
- list(iterable) -> new list initialized from iterable's items
- """
- def append(self, p_object): # real signature unknown; restored from __doc__
- """ L.append(object) -> None -- append object to end """
- pass
- def clear(self): # real signature unknown; restored from __doc__
- """ L.clear() -> None -- remove all items from L """
- pass
- def copy(self): # real signature unknown; restored from __doc__
- """ L.copy() -> list -- a shallow copy of L """
- return []
- def count(self, value): # real signature unknown; restored from __doc__
- """ L.count(value) -> integer -- return number of occurrences of value """
- return 0
- def extend(self, iterable): # real signature unknown; restored from __doc__
- """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """
- pass
- def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
- """
- L.index(value, [start, [stop]]) -> integer -- return first index of value.
- Raises ValueError if the value is not present.
- """
- return 0
- def insert(self, index, p_object): # real signature unknown; restored from __doc__
- """ L.insert(index, object) -- insert object before index """
- pass
- def pop(self, index=None): # real signature unknown; restored from __doc__
- """
- L.pop([index]) -> item -- remove and return item at index (default last).
- Raises IndexError if list is empty or index is out of range.
- """
- pass
- def remove(self, value): # real signature unknown; restored from __doc__
- """
- L.remove(value) -> None -- remove first occurrence of value.
- Raises ValueError if the value is not present.
- """
- pass
- def reverse(self): # real signature unknown; restored from __doc__
- """
- 翻转
- L.reverse() -- reverse *IN PLACE* """
- pass
- def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
- """
- 排序
- L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
- pass
- def __add__(self, *args, **kwargs): # real signature unknown
- """ Return self+value. """
- pass
- def __contains__(self, *args, **kwargs): # real signature unknown
- """ Return key in self. """
- pass
- def __delitem__(self, *args, **kwargs): # real signature unknown
- """ Delete self[key]. """
- pass
- def __eq__(self, *args, **kwargs): # real signature unknown
- """ Return self==value. """
- pass
- def __getattribute__(self, *args, **kwargs): # real signature unknown
- """ Return getattr(self, name). """
- pass
- def __getitem__(self, y): # real signature unknown; restored from __doc__
- """ x.__getitem__(y) <==> x[y] """
- pass
- def __ge__(self, *args, **kwargs): # real signature unknown
- """ Return self>=value. """
- pass
- def __gt__(self, *args, **kwargs): # real signature unknown
- """ Return self>value. """
- pass
- def __iadd__(self, *args, **kwargs): # real signature unknown
- """ Implement self+=value. """
- pass
- def __imul__(self, *args, **kwargs): # real signature unknown
- """ Implement self*=value. """
- pass
- def __init__(self, seq=()): # known special case of list.__init__
- """
- list() -> new empty list
- list(iterable) -> new list initialized from iterable's items
- # (copied from class doc)
- """
- pass
- def __iter__(self, *args, **kwargs): # real signature unknown
- """ Implement iter(self). """
- pass
- def __len__(self, *args, **kwargs): # real signature unknown
- """ Return len(self). """
- pass
- def __le__(self, *args, **kwargs): # real signature unknown
- """ Return self<=value. """
- pass
- def __lt__(self, *args, **kwargs): # real signature unknown
- """ Return self<value. """
- pass
- def __mul__(self, *args, **kwargs): # real signature unknown
- """ Return self*value.n """
- pass
- @staticmethod # known case of __new__
- def __new__(*args, **kwargs): # real signature unknown
- """ Create and return a new object. See help(type) for accurate signature. """
- pass
- def __ne__(self, *args, **kwargs): # real signature unknown
- """ Return self!=value. """
- pass
- def __repr__(self, *args, **kwargs): # real signature unknown
- """ Return repr(self). """
- pass
- def __reversed__(self): # real signature unknown; restored from __doc__
- """ L.__reversed__() -- return a reverse iterator over the list """
- pass
- def __rmul__(self, *args, **kwargs): # real signature unknown
- """ Return self*value. """
- pass
- def __setitem__(self, *args, **kwargs): # real signature unknown
- """ Set self[key] to value. """
- pass
- def __sizeof__(self): # real signature unknown; restored from __doc__
- """ L.__sizeof__() -- size of L in memory, in bytes """
- pass
- __hash__ = None
list源码
创建列表
- names = ['hyp',"lcy",'me']
通过下标访问列表中的元素,下标从0开始计数
- >>> names[0]
- 'hyp'
- >>> names[2]
- 'me'
- >>> names[-1]
- 'me'
- >>> names[-2] #还可以倒着取
- 'lcy'
切片:取多个元素
- >>> names = ["hyp","lcy","me","you","he","she"]
- >>> names[1:4] #取下标1至下标4之间的数字,包括1,不包括4
- ["lcy","me","you"]
- >>> names[1:-1] #取下标1至-1的值,不包括-1
- ["lcy","me","you","he","she"]
- >>> names[0:3]
- ["hyp","lcy","me"]
- >>> names[:3] #如果是从头开始取,0可以忽略,跟上句效果一样
- ["hyp","lcy","me"]
- >>> names[3:] #如果想取最后一个,必须不能写-1,只能这么写
- ["you","he","she"]
- >>> names[3:-1] #这样-1就不会被包含了
- ["you","he"]
- >>> names[0::2] #后面的2是代表,每隔一个元素,就取一个
- ["hyp","me","he"]
- ]>>> names[::2] #和上句效果一样
- ["hyp","me","he"]
追加
- >>> names
- ['hyp','lcy','me','he','she']
- >>> names.append("我是新来的")
- >>> names
- ['hyp','lcy','me','he','she', '我是新来的']
插入
- >>> names
- ['hyp','lcy','me','he','she']
- >>> names.insert(2,"强行从前面插入")
- >>> names
- ['hyp', 'lcy', '强行从me前面插入','he','she','我是新来的']
修改
- >>> names
- ['Alex', 'Tenglan', '强行从Eric前面插入', 'Eric', 'Rain', '从eric后面插入试试新姿势', 'Tom', 'Amy', '我是新来的']
- >>> names[2] = "该换人了"
- >>> names
- ['Alex', 'Tenglan', '该换人了', 'Eric', 'Rain', '从eric后面插入试试新姿势', 'Tom', 'Amy', '我是新来的']
删除
- >>> del names[2]
- >>> names
- ['Alex', 'Tenglan', 'Eric', 'Rain', '从eric后面插入试试新姿势', 'Tom', 'Amy', '我是新来的']
- >>> del names[4]
- >>> names
- ['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', '我是新来的']
- >>>
- >>> names.remove("Eric") #删除指定元素
- >>> names
- ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', '我是新来的']
- >>> names.pop() #删除列表最后一个值
- '我是新来的'
- >>> names
- ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy']
扩展
- >>> names
- ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy']
- >>> b = [1,2,3]
- >>> names.extend(b)
- >>> names
- ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]
拷贝
- >>> names
- ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]
- >>> name_copy = names.copy()
- >>> name_copy
- ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]
统计
- >>> names
- ['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3]
- >>> names.count("Amy")
- 2
排序&翻转
- >>> names
- ['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3]
- >>> names.sort() #排序
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: unorderable types: int() < str() #3.0里不同数据类型不能放在一起排序了,擦
- >>> names[-3] = ''
- >>> names[-2] = ''
- >>> names[-1] = ''
- >>> names
- ['Alex', 'Amy', 'Amy', 'Tenglan', 'Tom', '', '', '']
- >>> names.sort()
- >>> names
- ['', '', '', 'Alex', 'Amy', 'Amy', 'Tenglan', 'Tom']
- >>> names.reverse() #反转
- >>> names
- ['Tom', 'Tenglan', 'Amy', 'Amy', 'Alex', '', '', '']
获取下标
- >>> names
- ['Tom', 'Tenglan', 'Amy', 'Amy', 'Alex', '', '', '']
- >>> names.index("Amy")
- 2 #只返回找到的第一个下标
三、元组
创建元组
- tuple1 = (1,2,3,4,5,6)
访问元组的方式与列表一样
- tuple1 = (1,2,3,4,5,6)
- print(tuple1[1])
- print(tuple1[3:])
- print(tuple1[2:4])
分片
- tuple1 = (1,2,3,4,5,6)
- tuple2 = tuple1[:] # 复制
- temp = () # 创建空元组
- temp1 = (1,) # 只有一个元素,在它后面加上逗号(,)
四、字典 (无序)
字典是Python语言中唯一的映射类型。
- class dict(object):
- """
- dict() -> new empty dictionary # 空字典
- dict(mapping) -> new dictionary initialized from a mapping object's
- (key, value) pairs
- dict(iterable) -> new dictionary initialized as if via:
- d = {}
- for k, v in iterable:
- d[k] = v
- dict(**kwargs) -> new dictionary initialized with the name=value pairs
- in the keyword argument list. For example: dict(one=1, two=2)
- """
- def clear(self): # real signature unknown; restored from __doc__
- """ D.clear() -> None. Remove all items from D. """
- pass
- def copy(self): # real signature unknown; restored from __doc__
- """
- 复制字典
- D.copy() -> a shallow copy of D
- """
- pass
- @staticmethod # known case
- def fromkeys(*args, **kwargs): # real signature unknown
- # 创建并返回一个新字典,有两个参数,一:字典的键;二,可选,传入键的值,默认是 None
- """ Returns a new dict with keys from iterable and values equal to value. """
- pass
- def get(self, k, d=None): # real signature unknown; restored from __doc__
- # 更宽松的访问字典项,当键不存在时,不会报错,返回None.
- """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
- pass
- def items(self): # real signature unknown; restored from __doc__
- # 返回字典中所有的键值对(项)
- """ D.items() -> a set-like object providing a view on D's items """
- pass
- def keys(self): # real signature unknown; restored from __doc__
- """ D.keys() -> a set-like object providing a view on D's keys """
- pass
- def pop(self, k, d=None): # real signature unknown; restored from __doc__
- """
- 给定键弹出对应的值
- D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
- If key is not found, d is returned if given, otherwise KeyError is raised
- """
- pass
- def popitem(self): # real signature unknown; restored from __doc__
- """
- 随机弹出一个项
- D.popitem() -> (k, v), remove and return some (key, value) pair as a
- 2-tuple; but raise KeyError if D is empty.
- """
- pass
- def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
- """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
- pass
- def update(self, E=None, **F): # known special case of dict.update
- """
- 更新字典
- D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
- If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
- If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
- In either case, this is followed by: for k in F: D[k] = F[k]
- """
- pass
- def values(self): # real signature unknown; restored from __doc__
- """ D.values() -> an object providing a view on D's values """
- pass
- def __contains__(self, *args, **kwargs): # real signature unknown
- """ True if D has a key k, else False. """
- pass
- def __delitem__(self, *args, **kwargs): # real signature unknown
- """ Delete self[key]. """
- pass
- def __eq__(self, *args, **kwargs): # real signature unknown
- """ Return self==value. """
- pass
- def __getattribute__(self, *args, **kwargs): # real signature unknown
- """ Return getattr(self, name). """
- pass
- def __getitem__(self, y): # real signature unknown; restored from __doc__
- """ x.__getitem__(y) <==> x[y] """
- pass
- def __ge__(self, *args, **kwargs): # real signature unknown
- """ Return self>=value. """
- pass
- def __gt__(self, *args, **kwargs): # real signature unknown
- """ Return self>value. """
- pass
- def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
- """
- dict() -> new empty dictionary
- dict(mapping) -> new dictionary initialized from a mapping object's
- (key, value) pairs
- dict(iterable) -> new dictionary initialized as if via:
- d = {}
- for k, v in iterable:
- d[k] = v
- dict(**kwargs) -> new dictionary initialized with the name=value pairs
- in the keyword argument list. For example: dict(one=1, two=2)
- # (copied from class doc)
- """
- pass
- def __iter__(self, *args, **kwargs): # real signature unknown
- """ Implement iter(self). """
- pass
- def __len__(self, *args, **kwargs): # real signature unknown
- """ Return len(self). """
- pass
- def __le__(self, *args, **kwargs): # real signature unknown
- """ Return self<=value. """
- pass
- def __lt__(self, *args, **kwargs): # real signature unknown
- """ Return self<value. """
- pass
- @staticmethod # known case of __new__
- def __new__(*args, **kwargs): # real signature unknown
- """ Create and return a new object. See help(type) for accurate signature. """
- pass
- def __ne__(self, *args, **kwargs): # real signature unknown
- """ Return self!=value. """
- pass
- def __repr__(self, *args, **kwargs): # real signature unknown
- """ Return repr(self). """
- pass
- def __setitem__(self, *args, **kwargs): # real signature unknown
- """ Set self[key] to value. """
- pass
- def __sizeof__(self): # real signature unknown; restored from __doc__
- """ D.__sizeof__() -> size of D in memory, in bytes """
- pass
- __hash__ = None
dict源码
字典的定义与特性
定义:{key1:value1,key2:value2}
特性:
- 1、key-value 结构
- 2、key必须可hash,且必须为不可变数据类型,必须唯一
- 3、可存放任意多个值,可修改,可以不唯一
- 4、无序
字典的创建
- person = {"name": "hyp", "age": 18}
- person1 = dict(name='lcy', age=19)
- person2 = dict({"name": "sfv", "age": 20})
- person3 = dict((['name','hyp'],['lcy',20]))
- {}.fromkeys(seq,100) # 不指定100,默认为None
- # 注意
- >>> dic = {}.fromkeys(['k1','k2'],[])
- >>> dic
- {'k1': [], 'k2': []}
- >>> dic['k1'].append(1)
- >>> dic
- {'k1': [1], 'k2': [1]}
常见操作
- 键、值、键值对
- 1、dic.keys() 返回一个包含字典所有key的列表
- 2、dic.values() 返回一个包含字典所有value 的列表
- 3、dic.items() 返回一个包含所有(键、值)元组的列表
- 4、dic.iteritems()、dic.iterkeys()、dic.itervalues() 与它们对应的非迭代方法一样,不同的是它们返回一个迭代子,而不是一个列表。
- 新增
- 1、dic['new_key'] = 'new_value';
- 2、dic.setdefault(key,None) , 如果字典中存在key 键,就不做修改,没有就添加,有返回值。
- 查看
- 1、dic['key']
- 2、dict.get(key,default = None)
- 循环
- 1、for k in dic
- 2、for k in dic.keys()
- 3、for k in dic.items()
- 长度
- len(dic)
五、集合 (无序,可遍历)
集合的工厂函数
- class set(object):
- """
- set() -> new empty set object
- set(iterable) -> new set object
- Build an unordered collection of unique elements.
- """
- def add(self, *args, **kwargs): # real signature unknown
- """
- Add an element to a set.
- This has no effect if the element is already present.
- """
- pass
- def clear(self, *args, **kwargs): # real signature unknown
- """ Remove all elements from this set. """
- pass
- def copy(self, *args, **kwargs): # real signature unknown
- """ Return a shallow copy of a set. """
- pass
- def difference(self, *args, **kwargs): # real signature unknown
- """
- 相当于 s1-s2
- Return the difference of two or more sets as a new set.
- (i.e. all elements that are in this set but not the others.)
- """
- pass
- def difference_update(self, *args, **kwargs): # real signature unknown
- """ Remove all elements of another set from this set. """
- pass
- def discard(self, *args, **kwargs): # real signature unknown
- """
- 与 remove 功能相同,删除元素不存在时不会抛出异常;
- Remove an element from a set if it is a member.
- If the element is not a member, do nothing.
- """
- pass
- def intersection(self, *args, **kwargs): # real signature unknown
- """
- 相当于 s1&s2
- Return the intersection of two sets as a new set.
- (i.e. all elements that are in both sets.)
- """
- pass
- def intersection_update(self, *args, **kwargs): # real signature unknown
- """ Update a set with the intersection of itself and another. """
- pass
- def isdisjoint(self, *args, **kwargs): # real signature unknown
- """ Return True if two sets have a null intersection. """
- pass
- def issubset(self, *args, **kwargs): # real signature unknown
- """
- 相当于 s1 <= s2
- Report whether another set contains this set. """
- pass
- def issuperset(self, *args, **kwargs): # real signature unknown
- """
- 相当于 s1 >= s2
- Report whether this set contains another set. """
- pass
- def pop(self, *args, **kwargs): # real signature unknown
- """
- Remove and return an arbitrary set element.
- Raises KeyError if the set is empty.
- """
- pass
- def remove(self, *args, **kwargs): # real signature unknown
- """
- Remove an element from a set; it must be a member.
- If the element is not a member, raise a KeyError.
- """
- pass
- def symmetric_difference(self, *args, **kwargs): # real signature unknown
- """
- 相当于 s1^s2
- Return the symmetric difference of two sets as a new set.
- (i.e. all elements that are in exactly one of the sets.)
- """
- pass
- def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
- """ Update a set with the symmetric difference of itself and another. """
- pass
- def union(self, *args, **kwargs): # real signature unknown
- """
- 相当于 s1|s2
- Return the union of sets as a new set.
- (i.e. all elements that are in either set.)
- """
- pass
- def update(self, *args, **kwargs): # real signature unknown
- """ Update a set with the union of itself and others. """
- pass
set源码
三个特征:
确定性、互异性(去重)、无序性
关系运算
| ---> 并集,- :差集, ^ ---> 对称差集
包含关系
- in,not in :判断某元素是否在集合内
- ==,!= :判断两个集合是否相等
常用操作
元素的增加
- add() :单个元素的增加
- update():对序列的增加,可同时传入多个参数
元素的删除
方法一:
元素不在原集合中时
- set.discard(x) 不会抛出异常
- set.remove(x) 会抛出KeyError 错误
- pop():由于集合是无序的,pop 返回的结果是不能确定的,且当集合为空时,调用 pop 会抛出 KeyError 错误;
- clear():清空集合
六、字符编码和转码
ASCII码
ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。
由于计算机是美国人发明的,因此,最早只有127个字母被编码到计算机里,也就是大小写英文字母、数字和一些符号,这个编码表被称为ASCII
编码,比如大写字母 A
的编码是65
,小写字母 z
的编码是122
。后128个称为扩展ASCII码。
那现在我们就知道了上面的字母符号和数字对应的表是早就存在的。
在这里,每一位0或者1所占的空间单位为bit(比特),这是计算机中最小的表示单位
每8个bit组成一个字节,这是计算机中最小的存储单位(毕竟你是没有办法存储半个字符的)orz~
GBK和GB2312
显然,对于我们来说能在计算机中显示中文字符是至关重要的,然而刚学习的ASCII表里连一个偏旁部首也没有。所以我们还需要一张关于中文和数字对应的关系表。之前我们已经看到了,一个字节只能最多表示256个字符,要处理中文显然一个字节是不够的,所以我们需要采用两个字节来表示,而且还不能和ASCII编码冲突,所以,中国制定了GB2312编码,用来把中文编进去。
你可以想得到的是,全世界有上百种语言,日本把日文编到Shift_JIS里,韩国把韩文编到Euc-kr里,
各国有各国的标准,就会不可避免地出现冲突,结果就是,在多语言混合的文本中,显示出来会有乱码。
Unicode
因此,Unicode应运而生。Unicode把所有语言都统一到一套编码里,这样就不会再有乱码问题了。
Unicode标准也在不断发展,但最常用的是用两个字节表示一个字符(如果要用到非常偏僻的字符,就需要4个字节)。现代操作系统和大多数编程语言都直接支持Unicode。
现在,捋一捋ASCII编码和Unicode编码的区别:
ASCII编码是1个字节,而Unicode编码通常是2个字节。
字母A用ASCII编码是十进制的65,二进制的01000001;
字符0用ASCII编码是十进制的48,二进制的00110000;
汉字“中”已经超出了ASCII编码的范围,用Unicode编码是十进制的20013,二进制的01001110 00101101。
你可以猜测,如果把ASCII编码的A用Unicode编码,只需要在前面补0就可以,因此,A的Unicode编码是00000000 01000001。
新的问题又出现了:如果统一成Unicode编码,乱码问题从此消失了。但是,如果你写的文本基本上全部是英文的话,用Unicode编码比ASCII编码需要多一倍的存储空间,在存储和传输上就十分不划算。
UTF-8
所以,本着节约的精神,又出现了把Unicode编码转化为“可变长编码”的UTF-8编码。UTF-8编码把一个Unicode字符根据不同的数字大小编码成1-6个字节,常用的英文字母被编码成1个字节,汉字通常是3个字节,只有很生僻的字符才会被编码成4-6个字节。如果你要传输的文本包含大量英文字符,用UTF-8编码就能节省空间:
搞清楚了ASCII、Unicode和UTF-8的关系,我们就可以总结一下现在计算机系统通用的字符编码工作方式:
在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码。
用记事本编辑的时候,从文件读取的UTF-8字符被转换为Unicode字符到内存里,编辑完成后,保存的时候再把Unicode转换为UTF-8保存到文件。
Python全栈开发之路 【第二篇】:Python基础之数据类型的更多相关文章
- Python全栈开发记录_第二篇(文件操作及三级菜单栏增删改查)
python3文件读写操作(本篇代码大约100行) f = open(xxx.txt, "r", encoding="utf-8") 不写“r”(只读)默认是只 ...
- python 全栈开发之路 day1
python 全栈开发之路 day1 本节内容 计算机发展介绍 计算机硬件组成 计算机基本原理 计算机 计算机(computer)俗称电脑,是一种用于高速计算的电子计算机器,可以进行数值计算,又可 ...
- Python 全栈开发【第0篇】:目录
Python 全栈开发[第0篇]:目录 第一阶段:Python 开发入门 Python 全栈开发[第一篇]:计算机原理&Linux系统入门 Python 全栈开发[第二篇]:Python基 ...
- Python全栈开发记录_第一篇(循环练习及杂碎的知识点)
Python全栈开发记录只为记录全栈开发学习过程中一些难和重要的知识点,还有问题及课后题目,以供自己和他人共同查看.(该篇代码行数大约:300行) 知识点1:优先级:not>and 短路原则:a ...
- Python全栈开发之路 【第四篇】:Python基础之函数
本节内容 函数def: 1.位置参数,默认参数 2.位置参数,关键参数 3.如果参数中出现 *users,传递的参数就可以不再是固定的个数, 传过来的所有元素进行打包成元组 *args,**kwarg ...
- Python全栈开发之路 【第七篇】:面向对象编程设计与开发(1)
本节内容 一.编程范式 编程指的是写程序.敲代码,就是指程序员用特定的语法.数据结构和算法编写的代码,目的是来告诉计算机如何执行任务的. 在编程的世界里最常见的两大流派是:面向过程与面向对象.“功夫的 ...
- Python全栈开发之路 【第一篇】:Python 介绍
本节内容 一.Python介绍 python的创始人为荷兰人——吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本 ...
- Python全栈开发之路 【第八篇】:面向对象编程设计与开发(2)
一.继承与派生 什么是继承? 继承指的是类与类之间的关系,是一种什么是什么的关系,继承的功能之一就是用来解决代码重用问题. 继承是一种创建新的类的方式,在python中,新建的类可以继承一个或多个父类 ...
- Python全栈开发之路 【第六篇】:Python基础之常用模块
本节内容 模块分类: 好处: 标准库: help("modules") 查看所有python自带模块列表 第三方开源模块: 自定义模块: 模块调用: import module f ...
随机推荐
- c/c++链队列
链队列 链队列就是简化了的单链表 nodequeue.h #ifndef __NODEQUEUE__ #define __NODEQUEUE__ #include <stdio.h> #i ...
- 两层c:forEach循环嵌套
jsp中两级菜单如何用c:forEach输出 items 要被循环的信息 否 无 begin 开始的元素(0=第一个元素,1=第二个元素) 否 0 end 最后一个元素(0=第一个元素,1=第二个元素 ...
- Microsoft SQL Server sa 账户 登录错误18456
分析:在安装Sql server 2012的时候,服务器身份验证没有选择“SQL Server 和 Windows身份验证模式(S)”,导致SQL Server身份验证方式被禁用. 操作: 以Wind ...
- 学习flying logic
之前在知乎上结识的朋友吴笛,他的qq空间里分享了 flying logic的一些用途,我想到可以规划和团队的目标,这点让我感到很兴奋,分享学习这个软件. 学习之前,我应当把软件中的单词学明白.现在就 ...
- Docker+Nextcloud快速部署个人网盘
各位大佬好,,,萌新顾北清又回来更新了,今天要快速部署一个人网盘. 有多快呢,,,5分钟吧,因为我们使用Docker部署. Docker基础可以看看我之前的博文.(点这里点这里) 那么,,,开始吧. ...
- mysql 数据备份与数据导入到出
一.数据备份 #1. 物理备份: 直接复制数据库文件,适用于大型数据库环境.但不能恢复到异构系统中如Windows. #2. 逻辑备份: 备份的是建表.建库.插入等操作所执行SQL语句,适用于中小型数 ...
- mysql 数据可视化操作---Navicat安装及简单使用
,一.安装 下载地址:https://pan.baidu.com/s/1bpo5mqj 安装方法:https://www.cnblogs.com/clschao/articles/10022040.h ...
- hdfs fsck命令查看HDFS文件对应的文件块信息(Block)和位置信息(Locations)
关键字:hdfs fsck.block.locations 在HDFS中,提供了fsck命令,用于检查HDFS上文件和目录的健康状态.获取文件的block信息和位置信息等. fsck命令必须由HDFS ...
- Codeforces Round #546 (Div. 2) C. Nastya Is Transposing Matrices
C. Nastya Is Transposing Matrices time limit per test 1 second memory limit per test 256 megabytes i ...
- 【弱省胡策】Round #5 Count
[弱省胡策]Round #5 Count 太神仙了. \(DP\)做法 设\(f_{n,m,d,k}\)表示\(n*m\)的矩阵,填入第\(k\)个颜色,并且第\(k\)个颜色最少的一列上有\(d\) ...