python入门——流程控制之for循环、数字类型、字符串类型

流程控制之for循环

  1、什么是for循环

    循环就是重复做某件事,for循环是python提供第二种循环机制

  2、为何要有for循环

    理论上for循环能做的事情,while循环都可以做之所以要有for循环,是因为for循环在循环取值(遍历取值)比while循环更简

  3、如何用for循环

    语法:

    for 变量名 in 可迭代对象:# 可迭代对象可以是:列表、字典、字符串、元组、集合

      代码1
   代码2
   代码3
   ...

  4 for基本使用之循环取值

  4.1列表循环取值

l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
for x in l: # x='lxx_dsb'
print(x)

  4.2字典循环取值

dic={'k1':111,'k2':2222,'k3':333}
for k in dic:
    print(k,dic[k])

  4.3字符串循环取值

msg="you can you up,no can no bb"
for x in msg:
    print(x)

  5 总结for循环与while循环的异同

    相同之处:都是循环,for循环可以干的事,while循环也可以干、

    不同之处:while循环称之为条件循环,循环次数取决于条件何时变为假 for循环称之为"取值循环",循环次数取决in后包含的值的个数

  6 for循环控制循环次数:range()

    in后直接放一个数据类型来控制循环次数有局限性:当循环次数过多时,数据类型包含值的格式需要伴随着增加

  PS:终止for循环只有break一种方案

数字类型

  int类型:age = 10 # age=int(10)

 类型转换
纯数字的字符串转成int
res=int('100111')
print(res,type(res))

  十进制转换

# 10进制 -> 二进制
# 11 - > 1011
# 1011-> 8+2+1
print(bin(11)) # 0b1011

# 10进制 -> 八进制
print(oct(11)) # 0o13

# 10进制 -> 十六进制
print(hex(11)) # 0xb
print(hex(123)) # 0xb

  二进制转换

# 二进制->10进制
print(int('0b1011',2)) # 11

# 二进制->8进制
print(int('0o13',8)) # 11

# 二进制->16进制
print(int('0xb',16)) # 11

  float类型转换

res = '3.1'
print(res,type(res))
res=float("3.1")
print(res,type(res))

字符串类型(详细内容推荐小猿取经

  1 str的类型转换

  str可以把任意其他类型都转成字符串

  2 str的使用内置方法

  3.1 按索引取值(正向取+反向取) :只能取 不能改值 

msg='hello world'

# # 正向取 # print(msg[0]) # print(msg[5]) # # 反向取 # print(msg[-1])

  3.2 切片:索引的拓展应用,从一个大字符串中拷贝出一个子字符串[x:x:x]

  顾头不顾尾

msg='hello world'
res=msg[0:5] #x
print(res)
print(msg)
 步长
msg='hello world'
res=msg[0:5:2] # 0 2 4
print(res) # hlo

# 反向步长(了解)
res=msg[5:0:-1]
print(res) #" olle"
msg='hello world'
res=msg[::-1] # 把字符串倒过来
print(res)
# dlrow olleh

  3.3 测长度

msg='hello world'
print(len(msg))
# 11

  3.4 成员运算in和not in

# 判断一个子字符串是否存在于一个大字符串中
print("alex" in "alex is sb")
print("alex" not in "alex is sb")

  3.5 移除字符串左右两侧的符号strip

  默认去掉的空格

  strip只取两边,不去中间

msg='      egon      '
res=msg.strip()
print(msg) # 不会改变原值
print(msg.strip())
print(res) # 是产生了新值

msg='****egon****'
print(msg.strip('*'))
msg='====****egon****+++'
print(msg.strip('*=+'))

  3.6 切分split:把一个字符串按照某种分隔符进行切分,得到一个列表

  默认分隔符是空格
info='egon:18:male'
res=info.split(':',1)  # 指定分隔符 指定次数
print(res)

  3.7 循环

info='egon:18:male'
for x in info:
    print(x)

  4 拓展

  4.1 strip lstrip rstrip 去符号 左侧符号 右侧符号

msg='***egon****'
print(msg.strip('*'))
print(msg.lstrip('*'))
print(msg.rstrip('*'))

  4.2 lower upper 小写 大写

msg='AbbbCCCC'
print(msg.lower())   # 全变小写
print(msg.upper())   # 全变大写

  4.3 startswith,endswith # 判断对应的值是否以这个开头/结尾

print("alex is sb".startswith("alex"))
print("alex is sb".endswith('sb'))

  4.4 split,rsplit:将字符串切成列表

info="egon:18:male"
print(info.split(':',1)) # ["egon","18:male"]
print(info.rsplit(':',1)) # ["egon:18","male"]

  4.5 join : 把列表变成字符串

l=['egon', '18', 'male']
res=l
res=''.join(l)
print(res)
res="-".join(l) # 按照某个分隔符号,把元素全为字符串的列表拼接成一个大字符串
print(l)
print(res)

# egon18male
# ['egon', '18', 'male']
# egon-18-male

  4.6 replace 新的字符替换字符串中旧的字符,可以指定修改的个数

msg="you can you up no can no bb"
print(msg.replace("you","YOU",))
print(msg.replace("you","YOU",1))

  4.7 isdigit 判断字符串是否由纯数字组成

print('123'.isdigit())
print('12.3'.isdigit())

  4.8 find,rfind,index,rindex 返回要查找的字符串在大字符串中的起始索引

msg='hello egon hahaha'
# 找到返回起始索引
print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
print(msg.find('egon'))
print(msg.index('e'))
print(msg.index('egon'))
# 找不到
print(msg.find('xxx')) # 返回-1,代表找不到
print(msg.index('xxx')) # 抛出异常 报错

  4.9 count 查找单词个数

msg='hello egon hahaha egon、 egon'
print(msg.count('egon'))   # 单词的个数   2,3不属于单词

# 3

  4.10 center,ljust,rjust,zfill 填充

print('egon'.center(50,'*'))
print('egon'.ljust(50,'*'))
print('egon'.rjust(50,'*'))
print('egon'.zfill(10))

# ***********************egon***********************
# egon**********************************************
# **********************************************egon
# 000000egon

  4.11 expandtabs 设置空格数

msg='hello\tworld'    \t为空格 默认是四格
print(msg.expandtabs(2)) # 设置制表符代表的空格数为2
# hello word

  4.12 captalize,swapcase,title

print("hello world egon".capitalize())  # 第一个字母的第一个单词大写
print("Hello worLd EGon".swapcase()) # 大小写互换
print("hello world egon".title()) # 每个字母开头大写

# Hello world egon
# hELLO WORlD egON
# Hello World Egon

  4.12 is系列

print('abc'.islower())  #判断是否全是小写  True
print('ABC'.isupper()) #判断是否全是大写 True 
print('Hello World'.istitle()) # 判断每个字母开头是否大写 True
print('123123aadsf'.isalnum()) # 字符串由字母或数字组成结果为True True
print('ad'.isalpha()) # 字符串由由字母组成结果为True True
print('     '.isspace()) # 字符串由空格组成结果为True True
print('print'.isidentifier()) #isidentifier() 方法用于判断字符串是否是有效的 Python 标识符,可用来判断变量名是否合法。 True
print('age_of_egon'.isidentifier()) # True 如果字符串是有效的 Python 标识符返回 True,否则返回 False。
print('1age_of_egon'.isidentifier()) # False   

  4.12.2 isdigit:可以判断bytes和unicode类型,这也是最常见的数字应用场景如果要判断中文数字或罗马数字,则需要用到isnumeric。

num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='四' #中文数字
num4='Ⅳ' #罗马数字

# isdigit只能识别:num1、num2
print(num1.isdigit()) # True
print(num2.isdigit()) # True
print(num3.isdigit()) # False
print(num4.isdigit()) # False