Python grammar learning

Keywords: Python Lambda

I want to learn about crawlers recently, but I can't understand Python, so I can only learn the script language from the syntax.

In my understanding, Python means the serpent, which can be seen from its icon that the two snakes are intertwined with each other.

 

                    

The purpose of writing an article is to have a place to record the learning process. It's also convenient for later review. It has no teaching meaning. It's purely personal learning.

#print usage

a=1
print("Matthew")
print("The number is%d"%a)
print("The number is",a)
print("My name is%s,My nationality is%s"%("Matthew","China"))
print("www","baidu","com",sep=".")   #sep = ". Use. As separator
print("12",end='\n')
print("34",end='\t')
print("56")

Matthew
The number is one
The number is one
My name is Matthew, my nationality is China
www.baidu.com
12
34    56
 

#View keywords

import keyword
keyword.kwlist

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from',
'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
 

#Input

password = input("password:")
print("Enter the password as:",password)

#View type

a=10
print(type(a))

b="123"
print(type(b))

#Type conversion
a="123"
print(type(a))

b=int(a)
print(type(b))

<class 'int'>
<class 'str'>
<class 'str'>
<class 'int'>

#for while usage

import random #Random library

x =random.randint(0,5)
print(x)

#for

# 0-5 traversal
for i in range(5):
    print(i)

#Step 0-10 add 3 each time
for i in range(0,10,3):
    print(i)

#Traversal string
name = "Matthew"
for x in name:
    print("%s"%x,end="\t")

#Traversal list
a =["aa","bb","cc"]
for i in range(len(a)):
    print(i,a[i])

#while

i = 0
while i<5:
    print("i:",i+1)
    i+=1
else:
    print("i",i+1)

 

3
0
1
2
3
4
0
3
6
9
M    a    t    t    h    e    w    0 aa
1 bb
2 cc
i: 1
i: 2
i: 3
i: 4
i: 5
i 6

#string

a = "123"
b = "456"
c = "789"
d = "123456"
print("a+b",a+b)
print(c)
print(d[1:5])   #[start position: end position: step value]
print(d[1:5:2])
print(d[1:])
print(d[:5])
print(d*2)
print(r"123\n456") #No escape if there is r
print("123\n456")

print(d.find(a,0,len(d)))  # d is the parent string a is the child string

#list

list = ["1","2","3"]
list2 = ["4","5"]
for li in list:
    print(li)

list.append(list2)
for li in list:
    print(li)
print("--------------------------")
list.extend(list2)
for li in list:
    print(li)


a = [1,4,3,2]
print(a)

a.reverse()
print(a)

a.sort()
print(a)

a.sort(reverse=True)  #Descending order
print(a)

a.sort(reverse=False) #Ascending order
print(a)

#Using enumeration functions to get subscripts and contents
for i,x in enumerate(a):
    print(i,x)
  '''

[1, 4, 3, 2]
[2, 3, 4, 1]
[1, 2, 3, 4]
[4, 3, 2, 1]
[1, 2, 3, 4]
0 1
1 2
2 3
3 4

 

 

#Functions

def printinfo():
    print("--"*20)
    print("--"*20)
def printinfo1(a,b):
    print("--"*20)
    print(a + b)
    print("--"*20)
#Return multiple
def printinfo3(a,b):
    print("--"*20)
    return a+b,a-b



#function call
printinfo()
printinfo1(1,2)
a,b = printinfo3(1,2)
print(a,b)

#Exception handling

try:
 print(num)
except (NameError) as result:
    print(result)

 

 

 

 

 

 

 

 

Posted by dyconsulting on Thu, 04 Jun 2020 09:27:21 -0700