How to quickly transform python

Source: Internet
Author: User
Tags floor function
This article mainly explains how python can be quickly transformed. For more information, see python installation.

Install Python in windows

(1) open the web browser, visit the Python official site http://www.python.org

(2) click Download, or click Download on the homepage.

(3) download the required version. The latest version is 3.5.2.

(4) after the download is complete, double-click and install

Install Python and pyenv in CentOS

Pyenv: pyenv is a python version manager. different programmers may use different python versions for development, but they must be on the same machine without affecting each other. The main function of pyenv is to set different directories to use different versions, and install python easily.

1. install the dependency package

Yum groupinstall "Development Tools" "Server Plataform Development"-y

2. install pyenv

(1) clone the pyenv program through git

$ Git clone https://github.com/yyuu/pyenv.git ~ /. Pyenv

(2) set environment variables of pyenv

$ Echo 'export PYENV_ROOT = "$ HOME/. pyenv" '> ~ /. Bash_profile
$ Echo 'export PATH = "$ PYENV_ROOT/bin: $ PATH" '>> ~ /. Bash_profile

(3) add pyenv init to the environment variable.

$ Echo 'eval "$ (pyenv init-)" '>> ~ /. Bash_profile

(4) repeat your shell

$ Exec $ SHELL
$ Source ~ /. Bash_profile

3. use pyenv and install python

List all available versions

Pyenv install-l

Install the specified version

$ Pyenv install 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

Switch to the local python version

$ Pyenv local 2.7.12

Switch the global python version

Pyenv global 3.5.2

4. uninstall pyenv

Uninstall CentOS

Rm-rf 'pyenv root'

Mac OS X

Brew uninstall pyenv

II. interactive interpreter

When python is started, the following prompt is displayed:

$ Python
Python 3.5.2 (default, Aug 17 2016, 18:31:29)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

You can perform interactive input with python again, such

>>> Print ('Hello World ')
After you press enter, the following output is displayed:
Hello World

Of course, many computer languages are used to ending with semicolons, but python is different. a line is a line regardless of the content.
As described above, it is an interactive interpreter. you can enter some characters to obtain the output for your own purposes.

3. what is an algorithm?

Before starting to change, first explain what is computer programming. Simply put, it is to tell the computer what to do. A computer can do a lot of things, but it won't do it by itself. a programmer will tell a child the details and use the language-algorithm that the computer can understand. "Algorithms" are simply a simple statement of "steps" or "recipes"-a detailed description of how to do something. For example:
Cool skin practices:
(1) cut slice into thin strips
(2) put the skin in a container
(3) add mahjong, vinegar, garlic juice, chilies and other spices
(4) stirring for about 1 minute
In fact, the above steps are Algorithms. he tells the vinegar program how to do it, how long it will take, what it will do, and what it will do.

IV. numbers and expressions

Numeric operations

The Interactive python interpreter can be used as a very powerful calculator.

>>> 3666665444566 + 1123134546
3667788579112
>>> 588822456*33334678
19628206969929168

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

Python 2.7.5 (default, Jun 24 2015, 00:41:19)
[GCC 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
0
Python 3.5.2 (default, Aug 17 2016, 18:31:29)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
0.5

Division

>>> 20/3
6.666666666666667
>>> 20 // 3
6

Remainder

>>> 3, 122%
2
>>> 2.75% 0.5
0.25

Power

>>> (-3) ** 2
9
>>> (-3) ** 3
-27
>>> 3 ** 3
27

Long integer

The display method of long integers is different after python3.0. before 3.0, an L

Python 2.7.5 (default, Jun 24 2015, 00:41:19)
[GCC 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 30000000000000000000000000000
300100000000000000000000000000l
Python 3.5.2 (default, Aug 17 2016, 18:31:29)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> 243000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
243000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

V. variables

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

>>> X = 3
>>> X
3
>>> X * 2
6

Note: variable names can contain letters, numbers, and underscores. A variable cannot start with a number.

6. get user input

When writing a program, we may not know the values of some variables. User input may be used, but the interpreter knows his value. Therefore, we may need to obtain user input and obtain user input using input () function (raw_input function is replaced by input function after 3.0)

>>> Input ('Please input your name? ')
Please input your name? Antony
'Antony'

We can also save the obtained value to a variable.

>>> Name = input ('Please input your name? ')
Please input your name? Superman
>>> Name
'Superman'

Computing

>>> X = input ('Please input a number x :')
Please input a number x: 34
>>> Y = input ('Please input a number y :')
Please input a number y: 56
>>> X
'34'
>>> Y
'56'
>>> Int (x) * int (y)
1904

VII. Functions

Functions are similar to applets and can be used to implement specific functions. Python has many functions, and print () is a function. The definition function will be introduced later. At present, we will introduce how to use python built-in functions.

Pow ()

Pow indicates the power, which is used to calculate the multiplication

>>> 2 ** 3
8
>>> Pow (2, 3)
8
>>> Pow (3, 2)
9

Abs ()

Abs indicates absolute value

>>> Abs (-10)
10

Round ()

The round function rounds the floating point number.

>>> 1/2
0.5
>>> Round (1/2)
0
>>> Round (5/2)
2

Floor ()

Floor is rounded down. for example, if a person is 32.9 years old and is 32 years old and nine months old, he cannot be said to be 3 or 3 years old. in this case, the floor function is used.

>>> Import math
>>> Math. floor (32.9)
32

The floor function is no longer a decimal number after python3.0, and the certificate is obtained directly. the following output is before 3.0.

>>> Import math
>>> Math. floor (32.9)
32.0

Int ()

Int indicates an integer.

>>> Int (256.125)
256

Input ()

Input indicates getting user input. it has been described above.

>>> Input ('Do you want me? ')
Do you want me? No !!!
'No !!! '

Exit ()

Interactive interpreter used to exit python

$ Python
Python 3.5.2 (default, Aug 17 2016, 18:31:29)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> Exit ()
$

8. module

The module is used to extend the python function, and special commands need to be used to import the module. The floor function mentioned above is in the module named math.

>>> Import math
>>> Math. floor (333.20)
333

Sometimes you only need to use a function in a module, and you do not want to write the module name every time. you can use another form.

>>> From math import sqrt
>>> Sqrt (9)
3.0

9. save it to the execution script

To permanently store commands for future use, you can write them to files.
Note the beginning of the file, because there is a standard implementation method in unix: Add #! before the first line of the script #!. Add the absolute path of the program used to explain the script.

For example:

$ Cat first. py
#! /Usr/bin/env python3.5
Name = input ('Please input your name :')
Print ('Hello, '+ name)
$ Chmod + x first. py
$./First. py
Please input your name: Antony
Hello, Antony

Summary

Algorithm: describes in detail how to complete a task. In fact, when writing a program, you must describe the algorithm using a language that can be understood by a computer (such as Python. This type of machine-friendly description is called a program, which mainly contains expressions and statements.

The above is a detailed explanation of how python can be quickly transformed. For more information, see other related articles in the first PHP community!

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.