day03 python Boolean data type numeric strings

day03 python
 
A basic data type
    1.int
a= 8
a_length = a.bit_length () # This method required length of binary digits 
print(a_length)
 
    2.bool
        Only two values ​​True and False
        It relates to the type of conversion
str()
int()
bool () # all air is False: 0 "" () [] {} None
 
    3.str generally do not store large amounts of data
        '' '' '' '' '' "" "" "" String is enclosed, is immutable data type, regardless of operating, the same source string. The result is a new string
        Index: from 0 started [n] can be obtained each character exceeds the index values ​​being given: Number of supported from behind -1
        Slice: [Starting position: End position: step] include the beginning, not including the end, supporting a negative step
s = 'bajie'
print (s [0: 500]) # exceeds index corresponds not write
print(s[0:])  
print(s[:])
print (s [: 500]) # cut from the beginning to the end, to take all
 
s = 'bajieaishuishui'
s.capitalize()
print(s)
 
bajieaishuishui # regardless of operating, the source string constant. The result is a new string
 
s = 'bajie ai sHuisHui'
 
print (s.capitalize ()) # capitalize the first letter
print (s.lower ()) # string lowercase
print (s.upper ()) # string capitalize
print (s.swapcase ()) # invert case
print (s.casefold ()) # string to lowercase, to support some of the letters Eastern Europe
print (s.title ()) # the first letter of each word capitalized, the rest lowercase words
print (s.center (30, '#')) # string centered with '#' sign filled
print (s.strip ()) # removed and the blank spaces on both sides of the string, \ t \ n space
print (s.lstrip ()) # remove left blank
print (s.rstrip ()) # remove the right blank
print (s.replace ( 'bajie', 'wukong')) # replace certain string
print (s.replace ( 'bajie', '')) # remove something string
print (s.replace ( 'i', 'I', 2)) # replaced when defining the number of the replacement, the default -1 for all
print (s.split ()) # cut, the result is a list, the list is loaded with strings, as if a long knife and wood, the list will leave two empty strings
 
s1 = 'bajie \ taishuihui'
print (s1.expandtabs ()) # change the string \ length t, the default is 8 spaces
    
    Formatted output
s = 'my name is %s, my age is %s, I like %s.' % ('bajie', '500', 'shuishui')
print(s)
s = 'my name is {}, my age is {}, I like {}.'.format('bajie', '500', 'shuishui')                            #{}占位符, 不区分数据类型
print(s)
s = 'my name is {0}, my age is {1}, I like {2}.'.format('bajie', '500', 'shuishui')                         #指定位置, 从 0 开始
print(s)
s = 'my name is {name}, my age is {age}, I like {like}.'.format(name='bajie', age='500', like='shuishui')   #指定变量具体参数, 比较准确, 但是这样比较麻烦
print(s)
    
    查找判断
s = 'bajieaishuishui'
 
print(s.startswith('bajie'))    #判断是不是以什么开头
print(s.endswith('shui'))       #判断是不是以什么结尾
#以下三个都可以进行索引查找指定范围
print(s.count('i'))             #统计什么出现的次数
print(s.find('i'))              #查找什么出现的位置, 查到第一个就算完, 返回索引, 如果没有返回 -1
print(s.index('i'))             #和find基本一样, 区别是没有找到会报错 ValueError
    
    条件判断
s = '123456'
 
print(s.isdigit())      #判断字符串中是不是都由数字组成的
print(s.isalpha())      #判断字符串中是不是都由字母组成的
print(s.isalnum())      #判断字符串中是不是由 数字 和 字母 组成的
print(s.isnumeric())    #判断字符串中是不是都由数字组成的(支持中文中的大写)'五十六千百万萬佰'
    
    求什么的长度
len('bajie')    #统计字符串中字符的长度
    
    迭代: 一个一个地往外拿
s = 'bajie'
for n in range(len(s)):        #遍历
    print(s[n])
for item in s:                 #迭代: 把可迭代的每一个元素赋值给前面的变量
    print(item)
 
    4.list 用来存放大量数据
    5.tuple 只读列表
    6.dict 字典
    7.set 不重复
 
 
    练习
'''
求质数
求100以内的质数(n % (2~n-1)如果没除开, 就是质数)
'''
for i in range(1,101):
    for j in range(2,i):
        if i%j == 0:
            break
    else:
        print(i)
 
 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/aiaii/p/12088282.html