- Python数据类型
- python的运算符
- Python的循环与判断语句
- python练习
- Python作业
一. Python的数据类型
1. 整型(int)
<1>. 赋值
1 num1 = 123 # 变量名 = 数字 2 num2 = 456 3 num3 = int(123) # 另外一种赋值的方法 4 print(num1) 5 print(num2)
6 print(num3)
<2>. int类的额外功能
def bit_length(self): # real signature unknown; restored from __doc__
#--------------这个功能主要是计算出int类型对应的二进制位的位数--------------------------------------- """ int.bit_length() -> int Number of bits necessary to represent self in binary. >>> bin(37) '0b100101' >>> (37).bit_length() 6 """ return 0
例子:
num = 128 # 128的二进制10000000,它占了一个字节,八位 print(num.bit_length()) 显示: 8
<3>. int的赋值
每一个赋值int类都要重新开辟一段地址空间用来存储,而不会改变原来地址空间的值。
num1 = 123 num2 = num1 num1 = 456 print(num1) print(num2) 显示: 456 123
原理:
第一步赋值:开辟了一个空间存入123 ,变量为num1。
第二步赋值:先指向num1,然后通过地址指向了123。
第三部赋值:重新开辟一个地址空间用来存入456,num2的指向不变。
2. 布尔型(bool)
<1>. 真 True
<2>. 假 Flase
3. 字符型(char)
<1>. 索引
对于字符而言索引指的是通过下标找出其对应的字符。准确来说字符其实就是单个字符的集合。每个单个字符对应一个索引值,第一个字符对应的是0。
str = 'hello huwentao.' print(str[1]) # 因为下标是从零开始的,因此答案是e 显示: e
<2>. 切片
切片其实就是选取字符中的某一段。
# str[1:-1:2] 第一个数代表起始位置,第二个数代表结束位置(-1代表最后),第三个数代表步长(每隔2个字符选择一个) str = 'hellohuwentao.' print(str[1]) print(str[1:]) print(str[1:-1:2]) 显示结果: e ellohuwentao. elhwna
<3>. 长度
#长度用的是len这个函数,这个长度可能和c有点不太一样,是不带换行符的。所一结果就是14 str = 'hellohuwentao.' print(len(str)) 显示: 14
<4>. 循环
上面讲过,字符串其实就是一连串的字符的集合,因此它可以成为可迭代的,可迭代的都可以用for循环
# print里面的end指的是每输出一行,在最后一个字符后面加上引号内的内容,此处为空格,默认为换行符 str = 'hellohuwentao.' for i in str: print(i, end=' ') 显示: h e l l o h u w e n t a o .
<5>. 额外的功能
因为char的功能太多,因此里面有的部分功能没有讲
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 center(self, width, fillchar=None): # real signature unknown; restored from __doc__ #-----------------居中,width代表的是宽度,fillchar是代表填充的字符''.center(50,'*')-------------------- """ 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__ #-----------------和列表的count一样,计算出和sub一样的字符的个数,start,end带表起始和结束的位置-------------- """ 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__ #---------------代表指定的编码,默认为‘utf-8’,如果错了会强制指出错误------------------------------------- """ 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__ #------------------判断endswith是不是以suffix这个参数结尾-------------------------------------------- """ 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__ #-----------可以理解为扩展tab键,后面的tabsize=8代表默认把tab键改成8个空格,可以修改值----------------------- """ 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 #-----------------指的是格式,也就是在字符中有{}的,都会一一替换 str = 'huwegwne g{name},{age}' print(str.format(name = '111',age = '22')) 结果:huwegwne g111, 22 ------------------------------------------ """ 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 index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ #-------------和find一样,只是如果没有查找到会报错----------------------------------------------------- """ S.index(sub[, start[, end]]) -> int Like S.find() but raise ValueError when the substring is not found. """ return 0 return False def join(self, iterable): # real signature unknown; restored from __doc__ #------------------通过字符来连接一个可迭代类型的数据 li = ['alec','arix','Alex','Tony','rain'] print('*'.join(li)) 结果:alec*arix*Alex*Tony*rain --------------------------------- """ 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__ #----------------和center类型,只不过它是向左对齐,而不是居中-------------------------------------------- """ 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__ #-------------------和strip类似,用来删除指定的首尾字符,默认为空格-------------------------------------- """ 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 partition(self, sep): # real signature unknown; restored from __doc__ #-----------------------------partition() 方法用来根据指定的分隔符将字符串进行分割。 """ 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__ #-------------------替换,用新的字符串去替换旧的字符串------------------------------------------------- """ 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 Like S.rfind() but raise ValueError when the substring is not found. """ return 0 def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__ #----------------和center类型,只不过它是向右对齐,而不是居中-------------------------------------------- """ 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__ """ 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__ #----------------------------和split相似,只不过是从右向左分离--------------------------------------- """ 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__ #--------------------------------方法用于把一个字符串分割成字符串数组默认是以空格隔离---------------------- """ 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__ """ 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字符开头---------------------------------------------- """ 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 ""