python 入门基础2

#数字类型

基本使用 int() , float()

 

字符串转换成数字:

a = '123'
i = int(a)

# int() 只能转换不包含任意其他字符的纯数字的字符串

 2 定义方式

age=18 #age=int(18)

print(type(age))

int('abadf') #报错

int('10.1') #报错

int('101') #int只能将字符串中包含纯数字转成整型

 

# 十进制转成其他进制

print(bin(12))

print(oct(12)) #12 =>1*(8**1) + 2*(8**0)

print(hex(16))

 

# 进制转换(了解**)

# 其他进制转成十进制

print(int(10,2))

print(int(10,8))

print(int(10,16))

 

## 字符串类型

#1、按索引取值(正向取+反向取) :只能取

 

str1 = 'this is sh'
print(str1[0])
print(str1[1])
print(str1[2])
str1 = 'this is sh'

print(str1[-1])
print(str1[-2])

2、切片(顾头不顾尾,步长)

 

str1 = 'this is sh'

print(str1[0:5])
print(str1[0:5:2])

#3、长度len

str1 = 'this is sh'

print(len(str1))

4、成员运算in和not in: 判断一个子字符串是否存在于一个大的字符串中

print('alex' in 'alex is dsb')
print('dsb' in 'alex is dsb')
print('sb' in 'alex is dsb')
print('xxx' not in 'alex is dsb') # 推荐
print(not 'xxx' in 'alex is dsb')

#5、去掉字符串左右两边的字符strip,不管中间的

## strip 返回的结果是一个列表!!!!!!

user='      egon       '
user='   x   egon       '
user="*******egon********"
user=" **+*  */***egon*  **-*****"
print(user.strip("* +/-"))

user=input('>>>: ').strip()
if user == "egon":
    print('用户名正确')

切分split:针对按照某种分隔符组织的字符串,可以用split将其切分成列表,进而进行取值

m = 'a|b|c|d'
res = m.split('|')
print(res)

结果:

['a', 'b', 'c', 'd']

#1、strip,lstrip,rstrip

print('*******egon*******'.lstrip('*'))  #去掉左边的'*'
print('*******egon*******'.rstrip('*'))  #去掉右边的'*'
print('*******egon*******'.strip('*'))   #去掉两边的'*'

#2、lower,upper

msg = 'process finished with exit code'

print(msg.startswith('p')) #返回结果 True

print(msg.endswith('de'))  #返回结果 True

print(msg.endswith('abc')) #返回结果 False

#4、format的三种玩法

 

print('my name is  %s age is %s' %('egon',18))
print('my name is {} age is {}'.format('egon',33))
print('my name is {name} age is {age}'.format(name='egon', age=18))
print('my name is {0} age is {1}'.format('egon', 88))   # 0,1 对应的是 format 中元素的索引值

#5、split,rsplit

msg = 'my|name|is|egon|age|is|88'
print(msg.rsplit('|',1))
#结果是列表
['my|name|is|egon|age|is', '88']

msg = 'my|name|is|egon|age|is|88'
print(msg.rsplit('|',3))

['my|name|is|egon', 'age', 'is', '88']

#6、join

msg = 'my|name|is|egon|age|is|88'
# print(msg.rsplit('|',3))
l = msg.split('|')
print (l)

src_msg = '|'.join(l)
print(src_msg)
print(type(src_msg))

结果
['my', 'name', 'is', 'egon', 'age', 'is', '88']
my|name|is|egon|age|is|88
<class 'str'>

#7、replace

 

msg = 'alex say i have one tesla,alex is alex'
print(msg.replace('alex','sb'))
sb say i have one tesla,sb is sb

print(msg.replace('slex','sb',1))
sb say i have one tesla,sb is alex

#8、isdigit # 判断字符串中包含的是否为纯数字

 

print('10.1','isdigit')
age = input('>>:').strip()
if age.isdigit():
    age = int(age)
    if age >30:
        print("too big")
    elif age < 30:
        print("too small")
    else:
        print('you got it')
else:
    print('必须输入纯数字')

## 列表类型

1 用途:存放多个值,可以根据索引存取值

# 2 定义方式:在[]内用逗号分割开多个任意类型的值

l=['egon','lxx','yxx'] # l=list(['egon','lxx','yxx'])
l1=list('hello') #list就相当于调用了一个for循环依次取出'hello'的值放入列表
print(l1)
l2=list({'x':1,'y':2,'z':3})
print(l2)
list(10000) # 报错

# 3 常用操作+内置的方法

优先掌握的操作:

#1、按索引存取值(正向存取+反向存取):即可存也可以取

l=['egon','lxx','yxx']
print(l[0])
l[0]='EGON'
print(l)
print(l[-1])
print(l[3])
l[0]='EGON' # 只能根据已经存在的索引去改值
l[3]='xxxxxxxx' #如果索引不存在直接报错

#2、切片(顾头不顾尾,步长)

l=['egon','lxx','yxx',444,555,66666]
print(l[0:5])
print(l[0:5:2])
print(l[::-1])
返回结果:

['egon', 'lxx', 'yxx', 444, 555]
['egon', 'yxx', 555]
[66666, 555, 444, 'yxx', 'lxx', 'egon']

#3、长度

l=['egon','lxx','yxx',444,555,66666,[1,2,3]]
print(len(l))

## 常见列表的操作:

追加

append()

l = [11, 22, 33, 44, 55, 66]
l. append(99)
print(l)

[11, 22, 33, 44, 55, 66, 99]

#6、往指定索引前插入值

l=['egon','lxx','yxx']
l.insert(0,11111)
print(l)
l.insert(2,2222222)
print(l)

# 一次性添加多个元素

l = ['jason','nick']

l.extend(['tank','sean'])

 

#7、删除

l=['egon','lxx','yxx']

 

# 单纯的删除值:

# 方式1:

del l[1] # 通用的

print(l)

 

# 方式2:

res=l.remove('lxx') # 指定要删除的值,返回是None

print(l,res)

 

# 从列表中拿走一个值

res=l.pop(-1) # 按照索引删除值(默认是从末尾删除),返回删除的那个值

print(l,res)

 

#8、循环

l=['egon','lxx','yxx']

for item in l:

    print(item)

 

 

 

猜你喜欢

转载自www.cnblogs.com/chendaodeng/p/11128512.html
今日推荐