Python Knowledge

#KNOWLEDGE

* Install a new version python

python --version shows the current version

sudo apt update

sudo apt install software-properties-common

sudo add-apt-repository ppa:deadsnakes/ppa

sudo apt update

sudo apt install python3.8

 

*Set the default python 

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
sudo update-alternatives --config python
The commmand will show the current default python, you can intput the number to select the new default Python.

*Install python module 

 pip install xxx

* List the installed pacakges

pip list 

* Install a specific version package

pip install 'PackageName==1.4'

 

#PROGRAMING

1. accept console input

list = [int(x) for x in input("Input numbers:").split()]

2. built-in functions

 

 3. unit test frame

class TestMinMax(unittest.TestCase):
  def test_minmax(self):

4. main

if __name__ == '__main__':
  unittest.main()

5. data types

list [1, 2, 3] Ordered collection
tuple (1, 2, 3) Immutable ordered collection
dict {'a':1, 'b':2, 'c':3} Unordered (key,value) mapping
set {1, 2, 3} Unordered collection of unique values

return a tuple

def MinMaxCalculate(list):
  if (len(list) == 0):
    raise ValueError("Empty List")

  negtiveList = []
  positiveList = []
  for v in list:
    if (v >= 0):
      positiveList.append(v)
    else:
      negtiveList.append(v)
 
  if (len(positiveList) == 0): # only negative
    minValue = sum(negtiveList)
    maxValue = max(negtiveList)
  elif (len(negtiveList) == 0): # only positive
    minValue = min(positiveList)
    maxValue = sum(positiveList)
  else: # both negative and positive
    minValue = sum(negtiveList)
    maxValue = sum(positiveList)

  return (minValue, maxValue)

posted on 2020-01-16 14:07  荷树栋  阅读(254)  评论(0)    收藏  举报

导航