Python study notes (1)

python study notes (1)

1. When installing for the first time, running some commands may report errors, you need to upgrade or download the specified dependencies

pip upgrade python -m pip install --upgrade pip -ihttp://pypi.douban.com/simple --trusted-host pypi.douban.com

Depend on downloading pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn module name

Since the default mirror download is particularly slow and download timeouts often occur, you can directly specify the mirror download here

1.1 Reference Mirror

1.2 Set up Global Mirror Agent

  1. Add the pip folder in the specified location of your computer and create the pip.ini file
  2. The configuration file is as follows
[ global]

# Configure the mirror source,Choose by yourself
index-url = http://pypi.douban.com/simple
# Add the source as a trusted host, otherwise an error may be reported 3
trusted-host = pypi.douban.com
# Cancel every version update check of pip
disable-pip-version-check =true
# Configure download link timeout
timeout =120

2. Basic usage

  1. Define the global coding format # -*- coding: cp-1252 -*-
  2. Use the \ character to escape specific characters. If you do not want to escape, add r before the string

2.1 Basic data type

1. String
  1. Operation string can be repeated directly using operators such as print ('string','test'*3)
    The output is string testtesttest There are two indexing methods, one is from left to right 0, the other is from right to left -1
  2. Intercept (Intercept rules through java substring)
>>> test ='helloword'>>> test[1:5]
ello
>>> test[:]
hellowrd
>>> test[5:]
word
>>> test[-1:-6]
loword
2. list list

The basic properties of list are similar to those of java, which can be inserted, appended, modified, intercepted (the official term in python is slice and index). Index and trimming are the same. Here is the record of the additional usage

>>> a =[1,2,3,4,5]>>> a +[6,7,8,9][1,2,3,4,5,6,7,8,9]
3. Tuple Tuple

The nature of the ancestor is roughly the same as that of list. The difference is that the element attribute of the ancestor cannot be changed (not including the element in the ancestor as an object). The ancestor is enclosed by brackets and connected by commas.

>>> a =(1991,2014,'physics','math')>>> print(a, type(a), len(a))(1991,2014,'physics','math')<class'tuple'>4
4. Collection sets and dictionary Dictionaries

A set is unordered and non-repetitive data. The way to create a set is set(), and () is to create an empty dictionary. Both the set and the dictionary are included by ()

set

>>> student ={'Tom','Jim','Mary','Tom','Jack','Rose'}>>> print(student)   #Duplicate elements are automatically removed
{' Jim','Jack','Mary','Tom','Rose'}>>>'Rose'in student  #membership testing
True
>>> # set can perform set operations
...>>> a =set('abracadabra')>>> b =set('alacazam')>>> a
{' a','b','c','d','r'}>>> a - b     #difference of a and b
{' b','d','r'}>>> a | b     #Union of a and b
{' l','m','a','b','c','d','z','r'}>>> a & b     #the intersection of a and b
{' a','c'}>>> a ^ b     #elements in a and b that do not exist at the same time
{' l','m','b','d','z','r'}

dictionary

>>> dic ={}  #Create empty dictionary
>>> tel ={'Jack':1557,'Tom':1320,'Rose':1886}>>> tel
{' Tom':1320,'Jack':1557,'Rose':1886}>>> tel['Jack']   #Main operation: query by key
1557>>> del tel['Rose']  #Delete a key-value pair
>>> tel['Mary']=4127  #Add a key-value pair
>>> tel
{' Tom':1320,'Jack':1557,'Mary':4127}>>> list(tel.keys())  #Return a list of all keys
[' Tom','Jack','Mary']>>> sorted(tel.keys()) #Sort by key
[' Jack','Mary','Tom']>>>'Tom'in tel       #Membership test
True
>>>' Mary' not in tel  #Membership test
False
>>> dict([('sape',4139),('guido',4127),('jack',4098)]){'jack':4098,'sape':4139,'guido':4127}>>>{x: x**2for x in(2,4,6)}{2:4,4:16,6:36}>>> dict(sape=4139, guido=4127, jack=4098){'jack':4098,'sape':4139,'guido':4127}

Recommended Posts

Python study notes (1)
python study notes
Python study notes (3)
Python notes
Ubuntu Linux study notes
SkyWalking study notes (CentOS environment)
Python entry tutorial notes (3) array
Python introductory notes [basic grammar (on)]
Python entry notes [basic grammar (below)]
Container study notes CentOS7 install Docker
Python CookBook
Python FAQ
Python3 module
python (you-get)
Python string
Python basics
Python descriptor
Python basics 2
Python exec
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5