Basics of Python syntax

Reference link: Python Set

The first reason to learn Python is that it is relatively simple and has plenty of time for winter vacation, and I heard that the functions are also very good. The most important thing is that I will use it in this year's project.

And Liu Rujia’s book says that it would be a shame if a good Acmer doesn’t know a little Python. Don't talk nonsense, look at the learning content of the first day!

How to write Python 1. Enter python under cmd to enter development mode 2. Python editor: IDLE, aptana, I use IDLE here

Constants and variables 1. The definition of constants: the method of using objects, (although I still don’t know how python implements object programming)

1 class _const(object):

2     class ConstError(TypeError): pass

3

4     def setattr(self,name,value):

5         if self.dict.has_key(name):

6             raise self.ConstError, "Can't rebind const(%s)" % name

7         self.dict[name] = value

8

9     def delatter(self,name):

10          if name in self.dict:

11              raise self.ConstError, "Can't unbind const(%s)" % name

12          raise NameError, name

13

14 import sys

15 sys.modules[name] = _const()

  1. Variables, number of assignments and number of strings (int, long, float, bool, complex) strings: 1. Escape character

1 print 'It's a dog'

2

3 print "hello\nhello"

  1. Single quotation mark, double quotation mark, triple quotation mark (single quotation mark protects double quotation mark, double quotation mark protects single quotation mark, and triple quotation mark retains the newline format) 3. Natural string (r): unescaping

1 print "hello boy\nhello boy"

2

3 print r'hello boy\nhello boy'

  1. Simple string operation (repeat *, index [], slice)

1 #string repeat

2 string="Yinjian"

3

4 print string*20

5

6 _str = "YinJianPython"

7

8 c = _str[0]

9 print c

10

11 # The slicing operator [a:b] is a,b-1 which is closed left and opened right

12 _ str1 = _str[:2]

13 print _str1

14

15 _ str1 = _str[:3]

16 print _str1

17

18 _ str1 = _str[:]

19 print _str1

Data type 1, basic data type (number + string) 2, list

stu = ["Yinjian","xixi"]

print stu[1]

stu[1] = "xixi"

print stu[1]

  1. Tuples (cannot be modified)

1 stu = ("Yinjian","Tom")

2

3 print stu[1]

4

5 stu = (1,2,3)

6 print stu[1]

  1. Collection (establish relationship, remove duplication)

1 set1 = set("skdfjsofd")

2 set2 = set("dsflksdf")

3

4 print set1&set2

5

6 print set1|set2

7

8 print set1-set2

9

10 new = set(set1)

11 print new

1 lis = [1,2,2,3,3,"hello","hello","xixi"]

2

3 sett = set(lis)

4

5 print sett

6

7 lislen = len(lis)

8

9 print lislen

10

11 settlen = len(sett)

12 print settlen

pickle (solution to use an object for a long time, 1: load into memory, 2: third-party file)

import pickle

lista = ["ming yue ","ji shi ","you"]

listb = pickle.dumps(lista)

print listb

listc = pickle.loads(listb)

print listc

Read file

group = ("ba jiu ","wen ","qing tian")

f1 = file('1.pk1','wb')

pickle.dump(group,f1,True)

f1.close()

f2 = file('1.pk1','rb')

t = pickle.load(f2)

print t

f2.close()

Reprinted at: https://www.cnblogs.com/TreeDream/p/6345173.html

Recommended Posts

Basics of Python syntax
Basic syntax of Python
Python basics
Python basics 2
Python basics
Python basics 3
Python basics 4
Python basics 5
Python basic syntax (1)
Python3 basic syntax
7 features of Python3.9
Python object-oriented basics
Learn the basics of python interactive mode
Two days of learning the basics of Python
The difference between the syntax of java and python
Python basic syntax generator
Basic knowledge of Python (1)
Prettytable module of python
09. Common modules of Python3
Python basic syntax iteration
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Consolidate the foundation of Python(6)
Analysis of JS of Python crawler
Consolidate the foundation of Python(5)
Python implementation of gomoku program
Analysis of Python Sandbox Escape
Some new features of Python 3.10
Deep understanding of Python multithreading
Python multi-process and multi-thread basics
Analysis of Python object-oriented programming
Python version of OpenCV installation
9 feature engineering techniques of Python
matplotlib of python drawing module
Python method of parameter passing
Consolidate the foundation of Python (3)
Collection of Python Common Modules
The usage of wheel in python
Summary of logarithm method in Python
Use of Pandas in Python development
Detailed implementation of Python plug-in mechanism
Detailed explanation of python sequence types
Implementation of reverse traversal of python list
Python implementation of IOU calculation case
Magic methods and uses of Python
In-depth understanding of Python variable scope
Python preliminary implementation of word2vec operation
Python handles the 4 wheels of Chinese
Python calculation of information entropy example
Implementation of python selenium operation cookie
Use of numpy in Python development
Python simulation of the landlord deal
What is the use of Python
Scrapy simulation login of Python crawler
Analysis of Python conditional control statements
Black hat programming application of Python1
Detailed usage of dictionary in Python
Usage of os package in python
Several common modes of Python functions