Python learning, basics (1)

Python is an object-oriented , interpreted, dynamically typed computer programming language

  •     Interpreted: The program does not need to be compiled into binary code, but compiles the statements one by one at execution time
  •     Dynamic typing: During program execution, the type of a variable can be changed
    Often nicknamed the glue language , it can easily link together various modules made in other languages ​​(especially C/C++ )


1. Simple example

temp = input ( "Guess a number: " )
guess = int (temp)
 if guess == 8 :
     print ( "guessed correctly" )
 else :
     print ( "wrong" )

result:

2. Data type

    1) 150000 == 15e4 //return true. The right side is e notation, e represents 10, and the whole represents the 4th power of 15*10

            15e-4 //The whole represents 15*10 to the power of -4

    2)

def type_test():
    print(type(10))         #<class 'int'>
print(type(10.12))      #<class 'float'>
print(type("werrwer"))  #<class 'str'>
print(type(True))       #<class 'bool'>
print(isinstance("hhhh",str))   #True
print(isinstance("hhhh",int            
        ))   #False

3. Operator

def yunsuanfu ():
    a = b = c = d = 10
     a+= 1         # a=a+1
     b-= 3         # b=b-3
     c*= 10        # c=c*10
     d/= 8         # d=d/8
 print ( a)     # 11
 print (b)     # 7
 print (c)     # 100
 print (d)     # 1.25
 print (b// 3 )      # 7//3=2 Quotient
 print (b % 3 )     # 7%3=1 remainder
 print ( 3 ** 2 )      #3 to the power of 2 = 9 exponentiation
 print (-                
                3 ** 2 )     # 3 to the power of 2
 print ((- 3 )** 2 )   # -3 to the power of 2    

3. The priority of various operation symbols

The top of the tower has the highest priority



4、break、continue

    1) break: Once the i value that satisfies the condition is encountered, it will jump out of the entire for loop and terminate the loop

for i in range(10):
    if i%2 != 0:
        print(i)
        break
    else:
        print("===========")

    2) continue: After meeting the conditions, jump out of the if and else judgments, that is, do not judge the else, and continue from the for loop to the next loop

for i in range(10):
    if i%2 != 0:
        print(i)
        continue
    else:
        print("===========")


5、函数使用

1)向列表中增/删数据,三种方法

增加:append、extend、insert

member=['哈哈哈哈',123,[123,34,2],'啦啦啦']    
member.append('孩子1')            #单个插入到列表中
print(member)
member.extend(['孩子2', '爸爸', '妈妈'])   #要给你一个列表扩展另一个列表
print(member)
member.insert(0, '牡丹')           #指定插入位置
print(member)

删除:remove、del、pop

member=['哈哈哈哈', '孩子', '爸爸', '妈妈', '爷爷']
member.remove('孩子')     #删除'孩子'这个数据
print(member)
del(member[1])            #删除列表索引为1的数据
print(member)
member.pop()              #删除列表最后一个数据
print(member)
member.pop(1)             #删除 列表下标为1 的数据     
print(member)
print(member * 3)    #将列表数据复制添加三遍
 
 
print(member[1:4])    #分片取列表数据 从下标1开始到下表4 结束,不包括下标4,即下表标
print(member[:4])    #从下表0开始,到4结束
print(member[1:])    #从下表1开始,到最后一个

2)、排序sort、sorted,,,,,reverse

    1、简单排序
list = [5, 23, 45, 23, 34, 1, 77, 89, 56, 34, 5, 3]
list.sort()                            
print(list)
print(sorted(list, reverse=True))
list.reverse()
print(list)

list.sort()功能是针对列表自己内部进行排序, 不会有返回值, 因此返回为None。 reverse同理
解决办法:
    1)
        list.sort()
        return list

    2)
        return sorted(list)

        sort、sorted均接受 reverse=True or False 这个参数,表示排序的升降顺序


 2、对元素指定的某一部分进行排序,关键字排序

s = ['Chr1-10.txt','Chr1-1.txt','Chr1-2.txt','Chr1-14.txt','Chr1-3.txt','Chr1-20.txt','Chr1-5.txt']

我想要按照-后的数字的大小升序排序。要用到key

sorted(s, key=lambda d : int(d.split('-')[-1].split('.')[0]))

['Chr1-1.txt', 'Chr1-2.txt', 'Chr1-3.txt', 'Chr1-5.txt', 'Chr1-10.txt', 'Chr1-14.txt', 'Chr1-20.txt']

这就是key的功能,制定排序的关键字,通常都是一个lambda函数,当然你也可以事先定义好这个函数。如果不讲这个关键字转化为整型,结果是这样的:

sorted(s, key=lambda d : d.split('-')[-1].split('.')[0])

['Chr1-1.txt', 'Chr1-10.txt', 'Chr1-14.txt', 'Chr1-2.txt', 'Chr1-20.txt', 'Chr1-3.txt', 'Chr1-5.txt']

这相当于把这个关键字当做字符串了,很显然,在python中,'2' > '10'

你可以定制你想要的key, 如 key = lambda x : len(x) 按照序列的长度去排序。key= lambda x : (x[1], x[0]) 按二个元素,再第一个 等等。。。


3)、count、index、

 
 
member = [2323, 123, 111, 222, 444, 888, 100,123, '123']
    print(member.count(123))    # 列表中123出现的次数  输出:3
    print(member.index(123))    #列表中 123 出现第一次的下标
    print(member.index(123, 2, 8)) #123在下标1到8的数据片段中第一次出现的位置
 
 

4)、列表拷贝

list2 = list[:]
     list1 = list    #list1与list数据是指向同一个
    list2 = list[:]    #拷贝(备份)
    list.reverse()    #list反转
    print(list1)      #与list同步,反转后的list
    print(list2)    #list反转前的数据


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326722653&siteId=291194637