Python-rapid transformation: Basic Knowledge

Source: Internet
Author: User

first, python installation of the pyenv

Install Python under Windows

(1) open a Web browser and visit the official Python site http://www.python.org

(2) Click download, or click on the homepage to download

(3) Download the required version, currently the latest version is 3.5.2

(4) Double-click and perform the installation when the download is complete

Install Python and pyenv under CentOS

Pyenv:pyenv is a python version manager, because different programmers may use different python to develop, but also need to be on the same machine and do not affect each other. The main function of pyenv is that you can set different directories to use different versions, and you can easily install Python.

1. Install dependent packages
yum"Development Tools""Server Plataform Development" -y
2, installation Pyenv

(1) cloning pyenv program via git

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

(2) setting environment variables for pyenv

$ echo ‘export PYENV_ROOT="$HOME/.pyenv"‘ >> ~/.bash_profile
$ echo ‘export PATH="$PYENV_ROOT/bin:$PATH"‘ >> ~/.bash_profile

(3) add pyenv init to environment variable

$ ‘eval "$(pyenv init -)"‘>> ~/.bash_profile

(4) Re-read your shell

$SHELL
$ source ~/.bash_profile
3, the use of pyenv and installation of Python

List all available versions

install -l

Install the specified version

3.5.2
Downloading Python-3.5.2.tar.xz...
-> https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz

Toggle Local Python version

2.7.12

Toggle Global Python version

3.5.2
4, Uninstall Pyenv

CentOS Uninstall

`pyenv root`

Mac OS X

brew uninstall pyenv
second, Interactive Interpreter

When you start python, the following prompt appears:

$ python
3.5.2 (default17201618:31:29
4.4.7201203134.4.7-17)] on linux
"help""copyright""credits""license"for more information.
>>>

Interactive input with Python can be done again, such as

print(‘Hello World‘)
当按下回车后,会得到下面的输出
Hello World

of course, Many computer languages, the habit of ending with semicolons, but Python is different, a row is a row, regardless of the Content.
Described above is the interactive interpreter, you can type some characters to get the output to achieve their own purposes.

third, What is the algorithm

Before beginning to become, first explain what is computer programming. In short, it's about telling the computer what to do. A computer can do a lot of things, but it doesn't do it on its own, and the programmer tells it the exact details like a child feeding, and uses the language-algorithm that the computer can understand. "algorithmic" is just another genteel of "steps" or "recipes"-a detailed description of how to do something. Like what:
Cool Skin Procedure:
(1) cut the cool skin into thin strips
(2) put the cool skin into the container
(3) add mahjong, vinegar, garlic juice, chili and other spices
(4) stirring about 1 minutes or so
In fact, the above step is the algorithm, he tells the vinegar program how to do, how long, and what to do, and what to do.

Iv. numeric and expression numeric operations

The interactive Python interpreter can be used as a very powerful calculator

36666654445661123134546
3667788579112
58882245633334678
19628206969929168

Of course there are some special, the following two examples in different versions

Python2.7. 5(default, June -  -,xx: a: +)
[GCC4.8. 3 20140911(Red Hat4.8. 3-9)] on LINUX2
Type"help","copyright","credits"Or"license" forMore Information.
>>>1/2
0

Python3.5. 2(default, the - ., -: to: in)
[GCC4.4. 7 20120313(Red Hat4.4. 7- -)] on Linux
Type"help","copyright","credits"Or"license" forMore Information.
>>>1/2
0.5
Divisible
203
6.666666666666667
20// 3
6
Take surplus
1223
2
2.750.5
0.25
Power
>>> (-32
9
>>> (-33
-27
33
27
Long integers

Long integers are displayed differently after python3.0, with an L after 3.0

Python2.7. 5(default, June -  -,xx: a: +)
[GCC4.8. 3 20140911(Red Hat4.8. 3-9)] on LINUX2
Type"help","copyright","credits"Or"license" forMore Information.
>>>30000000000000000000000000000
30000000000000000000000000000L

Python3.5. 2(default, the - ., -: to: in)
[GCC4.4. 7 20120313(Red Hat4.4. 7- -)] on Linux
Type"help","copyright","credits"Or"license" forMore Information.
>>>
>>>243000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
243000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
V. variables

Variable (variable) is another concept that needs to be well Known. A variable basically represents (or references) a worthy name.

>>3
>>> x
3
>>2
6

Note: variable names can include letters, numbers, and Underscores. A variable cannot begin with a number.

Vi. Get user input

When writing a program, we may not know the value of certain variables. It may be through the User's input, but the interpreter knows his value, so we might need to get the User's input, get the User's input using the input () function (after 3.0, The Raw_input function is replaced by the input Function)

>>> input(‘Please input your name? ‘)
PleaseAntony
‘Antony‘

We can also deposit the obtained value into a variable

>>> name = input(‘Please input your name? ‘)
Please input your name? superman
>>> name
‘superman‘

Calculation

>>> x = input(‘Please input a number x: ‘)
Pleasex:34
>>> y = input(‘Please input a number y: ‘)
Pleasey:56
>>> x
‘34‘
>>> y
‘56‘
>>> int(x) * int(y)
1904
seven, function

Functions are similar to small programs that can be used to implement specific functions. Python has many functions, and print () is a function. The definition function is described later, and now describes how to use Python's built-in Functions.

Pow ()

The POW represents a power, which does not only compute

23
8
pow(2,3)
8
pow(3,2)
9
ABS ()

ABS denotes absolute value

abs(-10)
10
Round ()

The round function rounds up floating-point numbers

12
0.5
>>> round(12)
0
>>> round(52)
2
Floor ()

Floor for the downward rounding, such as a Person's age is 32.9 years old, both 32 years old 09 months, this time can not say he is 33 years old, this time using the floor function

>>> import math
>>> math.floor(32.9)
32

Floor function After python3.0 value is no longer a decimal, directly obtain the certificate, the following 3.0 output

>>> import math
>>> math.floor(32.9)
32.0
int ()

int means taking an integer

int(256.125)
256
Input ()

Input means to get user input, which has been introduced in the above, say not much, on the example

input(‘Do you want me? ‘)
DoyouwantmeNo!!!
‘No!!!‘
Exit ()

An interactive interpreter to exit Python

$ python
3.5.2 (default17201618:31:29
4.4.7201203134.4.7-17)] on linux
"help""copyright""credits""license"for more information.
exit()

eight, Module

Modules are designed to extend the functionality of python, and you need to import the modules using a special command import. The floor function mentioned in the previous section is in the name math Module.

>>> import math
>>> math.floor(333.20)
333

Sometimes you just need to use one of the functions in a module, and you don't want to write the name of the module each time, you can use a different form.

sqrt
sqrt(9)
3.0
Ix. Save in execution script

In order to store the command permanently for later use, it can be written to a file.
The beginning of the file needs to be noted, because there is a standard implementation in unix: add it before the first line of the script #! . After that, add the absolute path to the program that explains the Script.

For example:

$ cat first.py
#!/usr/bin/env python3.5
input(‘Please input your name: ‘)
print(‘Hello, ‘ + name)

$ chmod +x first.py
$ ./first.py
input your name: Antony
Hello, Antony
Summary

Algorithm: the algorithm is a detailed description of how to accomplish a Task. In fact, when writing a program, it is necessary to describe the algorithm using a language that the computer can understand, such as Python. This kind of machine-friendly description is called a program, and the program consists mainly of expressions and statements.

This article is from "standing on the shoulders of giants" blog, Please be sure to keep this source http://xinzong.blog.51cto.com/10018904/1855854

Python-rapid transformation: Basic Knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.