Python basics: A simple introduction to Python

Keywords: Python pip sudo git

1. Introduction to Python

Python is a computer programming language.It is an object-oriented, dynamic type language that can be applied in the following areas:

  • Web and Internet Development
  • Scientific Computing and Statistics
  • cloud computing
  • Artificial intelligence
  • System Operations and Maintenance

Python was designed by Guido van Rossum in the late 1980s and early 1990s at the National Institute of Mathematics and Computer Science in the Netherlands.

2. Python Installation

Visit the Python website: https://www.python.org/ , select the appropriate Python version to download and install.Use pyenv to install multiple versions in the CentOS environment.

1. Installation Dependent Environment

sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel libpcap-devel xz-devel

2. Create pyenv environment root directory

mkdir $HOME/.pyenv

3. Get pyenv

git clone git://github.com/yyuu/pyenv.git ~/.pyenv 

4. Configuring the environment

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile 
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bashrc  

5. Install Python version

Install Python 3.6.4 and Python 2.7.12

pyenv -install -v 3.6.4
pyenv -install -v 2.7.12

Set 3.6.4 as the global Python version after installation

pyenv global 3.6.4

3. Installation of Python Virtual Environment

1. pip installation source uses Tsinghua source

mkdir $HOME/.pip
vim $HOME/.pip
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

2. Install virtualenv and virtualenvwrapper

sudo pip install virtualenv
sudo pip install virtualenvwrapper

3. Create virtual environment directory

mkdir $HOME/.virtualenvs

4. Configuring the environment

vim $HOMW/.bash_profile
export WORKON_HOME=$HOME/.virtualenvs
source ~/.pyenv/versions/3.6.4/bin/virtualenvwrapper.sh

5. Create a virtual environment

mkvirtualenv -p $HOME/.pyenv/versions/3.6.4/bin/python3.6 py3
mkvirtualenv -p $HOME/.pyenv/versions/2.7.12/bin/python2.7 py2

4. Basic Python Syntax

1. Variables

Variable Naming Rules:
(1) Variables consist of letters, numbers and underscores and cannot begin with numbers;
(2) Keyword cannot be used;
(3) case sensitivity;
(4) Naming by hump or underscore is recommended

2. Constants

In Python, constants are usually represented in uppercase letters, such as PI=3.141592653

3. Comments

Single line comment: #
Multiline comment: three Quotes

4. Basic data types

  • Integer type: int
  • Floating point type: float
  • String type: str
  • boolean type: boolean

Data type can be determined by type(varName)

5. Process Control

5.1 Selection Structure

  • Single Branch
    if x>0:
    print('Positive number')
  • Double branch
    if x>0:
    print('Positive number')
    else:
    print('Non-positive number')
  • Multi-Branch
    if x>0:
    print('Positive number')
    elif x<0:
    print('negative')
    else:
    print('Fatal Frame')
  • IF Nesting
    if score >100:
    print('Please re-enter your score[0-100]')
    else:
    if score >=90:
            print('A')
        else:
            print('B')

5.2 Cycle Structure

for loop traverses elements

for i in range(5):
    print(i)

for loop nesting

for j in range(1,10):
    for i in range(1,j+1):
            print("%d*%d=%d\t" %(i,j,i*j),end="")
        print("\n")


while loop traverses elements

i=0
lst = [i for i in range(5)]
while i<len(lst):
    print(i)
        i+=1

while loop nesting

i=1
while i<10: 
    j=1 
    while j<=i: 
        print("%d*%d=%d\t" %(j,i,j*i),end="") 
        j+=1 
    print("\n") 
    i+=1 

Posted by annihilate on Sat, 27 Jul 2019 15:12:45 -0700