python Start Basics 2

# Digital Type

The basic use int (), float ()

 

Converted into a digital string:

= A ' 123 ' 
I = int (A) 

# int () can not contain any other converter character string purely digital

 2 is defined way

age=18 #age=int(18)

print(type(age))

int ( 'abadf') # error

int ('10 .1 ') # error

int ( '101') #int string can be converted into digital integer containing pure

 

# Turn into other binary decimal

print(bin(12))

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

print(hex(16))

 

# Hexadecimal conversion (to understand **)

# Other binary to decimal

print(int(10,2))

print(int(10,8))

print(int(10,16))

 

## string type

# 1, according to an index value (forward + reverse take take): can only take

 

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

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

2, a slice (care regardless of the end, step)

 

str1 = 'this is sh'

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

# 3, the length len

str1 = 'this is sh'

print(len(str1))

4, in operation members and not in: determining whether there is a substring of string in a large

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, the left and right sides of a character string is removed Strip, regardless of intermediate

Result ## strip returns a list! ! ! ! ! !

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

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

Segmentation split: some delimiter character strings for tissue, can be cut into split list, the value for further

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

result:

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

#1、strip,lstrip,rstrip

Print ( ' ******* ******* Egon ' .lstrip ( ' * ' ))   # remove left '*' 
Print ( ' ******* Egon ***** ** ' .rstrip ( ' * ' ))   # removed to the right of' * ' 
Print ( " ******* ******* Egon ' .strip ( ' * ' ))    # remove both sides' * '

#2、lower,upper

= MSG ' Process Finished with Exit code ' 

Print (msg.startswith ( ' P ' )) # returns the result True 

Print (msg.endswith ( ' de ' ))   # returns the result True 

Print (msg.endswith ( ' ABC ' )) # returns False results

# 4, format three games are played

 

Print ( ' My name Age IS IS S% S% ' % ( ' Egon ' , 18 is ))
 Print ( ' My name Age IS IS {} {} ' .format ( ' Egon ' , 33 is ))
 Print ( ' My name Age {name} IS IS Age {} ' .format (name = ' Egon ' , Age = 18 is ))
 Print ( ' My name IS IS Age {{0}}. 1 ' .format ( ' Egon ' , 88))    # 0,1 format is the index value corresponding to the elements

# 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 # determines whether a string contains purely digital

 

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 ( ' must enter a purely numeric ' )

## List Type

1 Purpose: to store a plurality of values, the index may be accessed in accordance with values

# 2 is defined by: a plurality of values ​​separated by commas of any type within []

= L [ ' Egon ' , ' LXX ' , ' YXX ' ] # L = List ([ 'Egon', 'LXX', 'YXX']) 
L1 = List ( ' Hello ' ) # List is equivalent to a call taken sequentially for loop 'hello' values into a list of 
Print (L1) 
L2 = list ({ ' X ' :. 1, ' Y ' : 2, ' Z ' :. 3 })
 Print (L2) 
list ( 10000) # error

# 3 + common operations built-in method

Master priority actions:

# 1, access by index value (Forward + Reverse Access Access): may be taken to deposit

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)

 

 

 

Guess you like

Origin www.cnblogs.com/chendaodeng/p/11128512.html