基础数据类型(一)

  • 整型
  • 布尔值
  • 字符串
  • 字符串方法
  • for循环

整型

32位机长度为    -2**31~2**31-1

64位机长度为    -2**63~2**63-1

 python2 中由long长整形python3中没有

python2中/结果是整数,python3中结果含有.0

运算有:+ - * /     //整除     **取幂     %取余

布尔值

int——>str
int转化为str,直接+引号或者str(11)
str——>int
s=' 6 '
print(int(s))#有空格也可以强转
str转化为int类型,该字符串中必须全都是数字,int('11') int——>bool bool(0)为False,非零都为True bool——>int int(True)==1 int(False)==0 str——>bool bool('')==False,空字符串为False bool(' ')==True,非空字符串为True bool——>str True或者False,直接+引号或者str(True),str(False)

 字符串

1.字符串的拼接

2.字符串只能与数字相乘

字符串提取

索引
               s='alex'
左到右           0123
右到左          -4-3-2-1
print(s[0])
print(s[-1])


切片
s='你好,少年,我是alex'
从左到右     s[0:3] ——>你好,
从右到左     s[-4:]——>alex

起始位置 冒号 结尾位置
1.顾头不顾尾
2.要想去到结尾,不写即可




切片+步长   

s[0:3:2]——>你,
s[::] 步长不写默认为1
      拓展;id(s[:])==id(s)
s[::2]取s字符串,隔2个取一个
    

 字符串通用方法:

# s='alEx'
# print(s.upper())    #字符串全部大写
# s='AleX'
# print(s.lower())    #字符串全部小写

# 验证码
# code='o98K'
# my_code=input('the code is(o98k):')
# if code.upper()==my_code.upper():
#     print('right')
# else:
#     print('error')

# s='aLeX'
# print(s.swapcase()) #字符串大小写转化
# s='aLex Egon meEt'
# print(s.title())    #字符串中每个单词首字母大写

# s='egon meet.'
# print('_'.join(s))  #每个字符加_
join方法是迭代添加\

eg:
要想显示'taibai_wusir_alex'
li=['taibai','wusir','alex']
res='_'.join(li)
print(res)


# s=' alex'
# print(s.endswith('x'))  #判断以什么为结尾,返回一个bool值
# print(s.startswith(' a'))#判断以什么打头

# s=' alex\n '
# print(s.strip())    #去掉字符串两边的空格或者转义字符
# s='alex'
# print(s.split('l')) #字符串分割

# s='alex'
# print(s.replace('a','b'))   #代替
s.replace(' ','')#可以把一个字符串中的空格都给去掉‘ abc’ ‘abc ’ ‘ a bc ’ # s
='alEx eee' # print(s.capitalize()) #字符串首字母大写 # s='alex' # # print(s.find('0')) # print(s.index('0')) #find和index都是找出索引,find找不到返回-1,index找不到报错 # s='aalen' # print(s.count('a')) #返回字符串中a的个数 # format方法 # s='alex,{}{}' # print(s.format('IT',18)) #按顺序索引 # # s='alex,{1}{0}' # print(s.format('it',18)) #按索引传入 # # s='alex,{a},{b}' # print(s.format(a='it',b='18')) #按照关键字传入 s='alex中文' print(s.isalpha()) #判断字符串中字母和或汉字 s='alex中文!' print(s.isalpha()) #含有特殊字符是False s='11' print(s.isdigit()) #判断字符串全为数字

s='aaaAAA1223!!!'
s_len=len(s)
count=0
up=0
low=0
num=0
sy=0
while count<s_len:
if s[count].isupper():    #判断字母是否大写
up+=1
elif s[count].islower():    #判断是否小写
low+=1
elif s[count].isdigit():    #判断是否为数字
num+=1
else:                #否则为特殊字符
sy+=1
count+=1
print('up=%d'%up)
print('low=%d'%low)
print('num=%d'%num)
print('sy=%d'%sy)
 

 长度

len()方法

s='alex'
s1=len(s)
n=0
while n<s1:
    print(s[n])
    n+=1

a
l
e
x

 for循环

s='alex'
# 利用for循环打印
# for 变量i in 可迭代对象:
for i in s:
    print(i)

  除了int bool 其他的数据类型都可以for循环

posted @ 2018-12-29 16:25  烧刘病  阅读(187)  评论(0编辑  收藏  举报
回到页首