当前位置: 移动技术网 > IT编程>脚本编程>Python > python学习第七天

python学习第七天

2020年07月11日  | 移动技术网IT编程  | 我要评论

一、for+range

count = 0
while count < 3:
    print('='*10)
    print('步骤1')
    print('步骤2')
    print('步骤3')
    count += 1


for x in "a"*100:
    print('=' * 10)
    print('步骤1')
    print('步骤2')
    print('步骤3')

>>> range(1,5,2) # 起始位置,结束位置,步长
[1, 3]
>>>
>>> range(1,5) # 省略步长,默认为1
[1, 2, 3, 4]
>>>
>>> range(5) # 省略起始位置与步长,默认起始位置为0,步长为1
[0, 1, 2, 3, 4]

for x in range(0,5,1):  # [0,1,2,3,4]
    print(x)



for x in range(3):
    print('=' * 10)
    print('步骤1')
    print('步骤2')
    print('步骤3')

了解:python是解释型强类型动态语言

概括地说,编程语言的划分方式有以下三种

1、 编译型or解释型

#1.1 编译型#1.2 解释型(python属于解释型)

2、强类型or弱类型

#2.1 强类型语言(python属于强类型) 
  数据类型不可以被忽略的语言 即变量的数据类型一旦被定义,那就不会再改变,除非进行强转。 在python中,例如:name = 'egon',这个变量name在被赋值的那一刻,数据类型就被确定死了,是字符型,值为'egon'#2.2 弱类型语言:
  数据类型可以被忽略的语言 比如linux中的shell中定义一个变量,是随着调用方式的不同,数据类型可随意切换的那种。  

3、动态型or静态型

#3.1 动态语言(python属于动态语言)
  运行时才进行数据类型检查 即在变量赋值时,才确定变量的数据类型,不用事先给变量指定数据类型  

#3.2 静态语言
  需要事先给变量进行数据类型定义  

二、可变不可变类型

2.1、可变类型:

值可以改变,id不变,证明就是在改变原值

如:列表、字典

2.2、不可变类型:

值改变,id也变,证明是产生了新值,并没有改变原值,原值是不可改变的

如:int,float、字符串

三、数字类型及其常用操作:

3.1、整型(int)

(1)用途:年龄,个数,出生年,号码等

(2)定义方式

age = 18  # age = int(18)

int可以把纯数字的字符串转换成int类型

res = int("18")
# res = int("1.8")#报错
print(type(res))

了解(***)

print(bin(11))  # 0b1011
print(oct(11))  # 0o13
print(hex(11))  # 0xb

(3)常用操作和内置方法

算数运算和比较运算

3.2、浮点型(float)

(1)用途:身高、体重、薪资等

(2)定义方式

salary = 3.1  # salary = float(3.1)

float可以把浮点字组成的字符串转换成float类型

res = float("1.8")
print(type(res))

(3)常用操作和内置方法

算数运算和比较运算

总结:int和float都是只存一个值、不可变

四、字符串类型及其常用操作

(1)用途:记录描述性质的状态,例如名字、性别、国籍等

(2)定义方式:

在引号(’’ , “”, ‘’’ ‘’’, “”" “”")内包含一串字符串

s = "hello"  # s = str("hello")

str可以把任意类型转换成字符串类型

res=str([1,2,3])  # "[1,2,3]"
print(type(res))

(3)常用操作和内置方法

需要优先掌握的操作:

a、按照索引取值(正向取+反向取):只能取

s="hello world"
print(s[0],type(s))
print(s[-1])
运行结果:h <class 'str'>
        d

非法操作:
s[1]="E"  #不能改
print(s)

s[222]  #超出取值范围
print(s)

b、切片(顾头不顾尾,步长)=》属于拷贝操作

s = "hello world"
new_s=s[1:7]
print(new_s)
print(s)

new_s=s[1:7:2]  #1 3 5
print(new_s)
print(s)

new_s=s[:7:2]
new_s=s[::2]  # 0 2 4 6 8 10
              h l o w r  d
print(new_s)

new_s=s[::]  # 完整拷贝字符串,只留一个冒号就可以new_s=s[:]
print(new_s)

c、len

s=" \n hello world  \t  "
运行结果:19

res=print("sfd")
print(res) #print无返回值
运行结果:
sfd
None

d、成员运算in和not in

s=" \n hello world  \t  "
print("s" not in s)#推荐 yu语义明确,推荐使用
print(not  "s" in s)#不推荐
print("s"  in s)
运行结果:
True
False

e、移除空白strip

s=" \n hello world  \t  "
res=s.strip()
print(res)
print(s)#没有改变原字符串
运行结果:
hello world

去除左右两边的非空白字符
print("**+=-%^#****he**llo**%^#**+=**".strip("*+=-%^#"))

应用案例:
name = input("your name>>> ").strip()  # name = "egon "
pwd = input("your pwd>>> ").strip()

if name == "egon" and pwd == "123":
    print('login successful')
else:
    print('user or password error')

f、切分split:把字符串按照某一个分隔符切成一个列表

user_info= "egon_dsb:123:18:3.1"
res=user_info.split(":")
print(res)
print("-".join(res))
运行结果:
['egon_dsb', '123', '18', '3.1']
egon_dsb-123-18-3.1

g、循环

for i in "hello":
    print(i)
运行结果:
h
e
l
l
o

需要掌握的操作:

a、strip、lstrip、rstrip

print("***hello***".strip("*"))
print("***hello***".lstrip("*"))
print("***hello***".rstrip("*"))
运行结果:
hello
hello***
***hello

b、lower、upper、swapcase

msg = "AbCDEFGhigklmn"
res = msg.lower()
res = msg.upper()
print(res)

res=msg.swapcase()
print(res)
运行结果:
abcdefghigklmn
ABCDEFGHIGKLMN
aBcdefgHIGKLMN

c、startswith、endswith

msg = "sb is lxx sb"
print(msg.startswith("sb"))
print(msg.endswith("b"))
print(msg.endswith("c"))
运行结果:
True
True
False

d、split、rsplit

userinfo="egon:123:18"
print(userinfo.split(":"))
print(userinfo.split(":",1))
print(userinfo.rsplit(":",1))
运行结果:
['egon', '123', '18']
['egon', '123:18']
['egon:123', '18']

e、join

纯字符串组成的列表
l = ["aaaa", "bbb", "ccc"]

# res=l[0]+":"+l[1]+":"+l[2]
res = ":".join(l)
print(res, type(res))

f、replace

msg = "***egon hello***"
res=msg.replace('*','').replace(' ','')
res=msg.strip('*').replace(' ','')
print(res)
运行结果:
egonhello
egonhello

s="lxx hahah wocale lxx sb 666"
res=s.replace('lxx','sb')
res=s.replace('lxx','sb',1)
print(res)
print(s)
运行结果:
sb hahah wocale sb sb 666
sb hahah wocale lxx sb 666
lxx hahah wocale lxx sb 666

g、format的三种玩法

1、%s的方式

name="egon"
age=18
res1="my name is %s my age is %s"%(name,age)
print(res1)

2、format的方式

name="egon"
age=18
res1="my name is {} my age is {}".format(name,age)
print(res1)
运行结果:
my name is egon my age is 18

res1="{0}{1}{0}".format(name,age)
print(res1)
运行结果:
egon18egon

res1="my name is {name} my age is {age}".format(age=18,name="egon")
运行结果:
my name is egon my age is 18

h、f’'

name="egon"
age=18
res1=f'my name is {name} my age is {age}'
print(res1)
运行结果:
my name is egon my age is 18


了解:f搭配{}可以执行字符串中的代码

res=f'{len("hello")}'
print(res)
运行结果:5

f包含的字符串可以放到多行
name="egon"
age=18
res1=f'my name is {name} '\
     f'my age is {age}'
print(res1)
运行结果:
my name is egon my age is 18

{}内不能有\以及#
print(f'my name is {{egon}}')
print('胜率是 %s%%' %70)
运行结果:
my name is {egon}
胜率是 70%

i、isdigit:判断字符串是否由纯数字组成

print("adsf123".isdigit())
print("123".isdigit())
print("12.3".isdigit())
运行结果:
False
True
False

应用案例:
age = input('>>>: ') # age = "        18     "
if age.isdigit():
    age=int(age)
    if age > 18:
        print('猜大了')
    elif age < 18:
        print('猜小了')
    else:
        print('猜对了')
else:
    print('必须输入数字,小垃圾')

本文地址:https://blog.csdn.net/xiaoyurainzi/article/details/107255887

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网