C ++ programmers to learn Python

C ++ programmers to learn Python

@
Open a new pit hhh, to touch the game AI, learn the point Py, made a point of notes.

Chapter II. Variables and Data Types

1. Before comment statements with #:

#这是注释

If an error after adding a comment on the increase at the beginning of a file

#coding:utf-8 

2. Commonly used in sensitive functions:

  • upper (): all uppercase
  • lower (): all lowercase
  • title (): capitalize the first letter

Such as:

print('am'.upper())

Output: AM

  1. Clear blank string functions:

    • lstrip () - rstrip (): Clear the corresponding left / right blank
    • strip (): Clear both sides of the blank
  2. + Combined string, numeric string variable str (number), either by double quotes string '' can also be single quotes'

    Such as:

    print("He has "+str(10)+' apples.')
  3. Variable names can only contain letters, numbers, underscores, you can start with a letter and underline, can not start with a number, to avoid naming keyword

Chapter III. List

1. Brief List

Array-like, the content can be modified

names=['AM','Hong','ZL']
print(names)     #['AM','Hong','ZL']
print(names[0])  #AM
print(names[-1]) #ZL
#从0递增为正序,以-1递减为逆序

2. modify, add, insert, delete list elements

Games=['GTA','LOL','CS']
Games[0]='GTA5'       #修改列表元素,['GTA5','LOL','CS']
Games.append('COC')   #在列表末尾增加元素,['GTA5','LOL','CS','COC']
Games.insert(0,'WOW') #在列表第n处插入元素,['WOW','GTA5','LOL','CS','COC']
del Games[0]          #直接删除列表第n个元素
game=Games.pop(3)     #删除第n个元素并且返回值为该元素
Games.remove('CS')    #删除在列表中第一个值为'xx'的元素

Chapter 4 Operation list

1. traversal

Format: for + variable name + in + list + ':'
only the content inside the loop body indent

Prices=[1,2,3]
for Price in Prices:         
    print(Price)        
    print(Price*2)

2. Create a list of values

range (x, n): x is generated from n-1 to the start up of a string of numbers
range (x, n, a) : x is generated from the start up to n-1, a size of the interval for the series of digits

min (list name), max (list name), sum (list name): can be used to quickly find or calculate numbers

Price=list(range(1,4))   #相当于[1,2,3]
#可用list将range生成的一串数字变成列表

Prices=[]
for value in range(1,4):
    price=value**2
    Prices.append(price)        
    
Prices=[value**2 for value in range(1,4)]
#上面两个列表是一样的,只是后者采用了列表解析,使得步骤简化

3. Use the part of the list

Use slice: '(' name list + + [number_1: number_2] + ')' # replication list

  1. number_1 and number_2 not fill from the top of the list represent the beginning and the end of the last one
  2. number_1: start from the index of the number number_1
  3. number_2: Copy the following total number of number_2
Players=['AM','BM','CM','DM','EM']
New_Players=[]

print(Players[0:3])  
print(Players[:3])         #均输出['AM','BM','CM']

print(Players[2:])
print(Players[-3:])        #均输出['CM','DM','EM']

print(Players[:])
print(Players)            #均输出['AM','BM','CM','DM','EM']

New_Players=Players       #使New_Players也指向Players的列表,二者指向同一个
New_Players=(Players[:])  #使New_Players指向一个和Players的列表一样的新列表,二者分别指向各自的

4. tuple

Format: name = List (list of elements ...)

Usage and a list of similar, but the difference is the tuple content is not modified and is in brackets

game=('GTA5','Gta6')
game[0]='GTA4'          #修改元组,非法,会报错
game=('GTA6','Gta7')    #直接给元组变量赋值,合法
print(game)

The fifth chapter if statement

  1. if else conditional required plus rear end ':' will perform a colon, the following condition is satisfied when the code indented
  2. 'Else if' becomes in a python 'elif', else statements can be omitted, may also be multiple if statements without the other two
  3. '&&' and '||' respectively becomes in python 'and' and 'or', '==' and '! =' Remains
Players=['AM','BM','CM','DM','EM']
Players2=[]
if Players:
    for player1 in Players:
        if player1 == 'AM':
            print(player1)
        elif player1 == 'BM':   #elif语句可以不止一条
             print(player1)
        else:
            print(player1+'.')

if 'AM' in Players:         #检测特定值在不在列表中
    print('AM')
        
for player2 in Players:
    if player2 not in Players2:
        print(player2)

num1=12
num2=24

if num1==12 and num2==23:   #and需要二者均成立,而该条件不为真
    print('right1')
if num1==12 or num2==23:    #or只需要二者中一者成立,则该条件为真    
    print('right2')

Guess you like

Origin www.cnblogs.com/AMzz/p/11802583.html