Python Basic Learning Notes (i)

Source: Internet
Author: User

Installing and running the interactive interpreter

In the vast majority of Linux and UNIX system installations (including Mac OS X), the Python interpreter already exists. We can verify by entering the Python command at the prompt (author Environment Ubuntu)

Ok! If the above information is present in the typing Python command, we have already installed the python environment.

If not installed, in Ubuntu/debin Linux, you can install Python with the following command :

$ apt-get Install Python

If it is under Window installation, and Linux under the download source package installation, I believe this kind of person problem is easy to find, here is not specifically explained. It 's much easier to believe than configuring the Java environment for the first time.

Enter a command to see if it will work.

>>> print "hello,world!" hello,world!

Note: If you want to launch the interactive interpreter, you can ctrl+d

What is the algorithm?

Before serious programming, first of all to say what is computer programming, simply speaking, computer programming is to tell the computer to do what. The computer can do a lot of things, but it does not think for itself, we need to tell it the specifics, we need to use the computer to understand the language of the algorithm to tell it. "Algorithmic" is just another sweet word for "program" or "recipe" ----describe in detail how to do something.

Stir-fried tofu, tofu,

First, take some tofu; (Declare a variable)

Then add the shallot on the tofu. (two variables added)

If you need a special spicy tofu. (Consider a specific condition)

Cooked ---Inspected every 10 minutes. (Run this command repeatedly)

Recipes and algorithms contain elements (objects, items), and directives (statements). "Shallot" and "tofu" are the elements. Instructions include "Add shallot on tofu" and so on.

Numbers and Expressions:

The interactive Python interpreter can be used as a powerful calculator

>>> 1-2-1 >>> 1+2 3 >>> 32784*13 426192 >>> 1/2 0

What's the situation? 1 except 2 should be equal to 0.5? Why is it 0? "/" for rounding, not evenly divisible, must be 0, try floating point

>>> 7/2 3>>> 1.0/2.0 0.5

Try "%" to take the remainder

>>> 6 3 0 >>> 6/3 2 >>> 6% 3 0 >>> 7/3 2 >>> 7% 3 1 >>> 13 9 4>>> 0.75 0.5 0.25

See the difference between the divisible (/) and the remainder (%).

6/3 is exactly divisible by 2, so the remainder (%) is 0, 13/9 can only bring 1 9 , and the remainder is 4.

Then try the " * *" Power operation (exponentiation) character

>>> 2*2*2 8 >>> 2**3 8 >>> 2**6 >>> -3**2-9 >>> (-3) **2 9

2 of the 3 times can be used by Fang (* *) to indicate that this is quite convenient.

The precedence of the -3**2 (unary minus operator) is higher, so it is equivalent to-(3**2), and if you want to calculate (-3)**2, you need to explicitly describe it.

Long integers

Python can handle very large numbers.

>>> 100000000000000000000000 100000000000000000000000L >>> 100000000000000000000000L 100000000000000000000000L

normal integers in 2 147 483 647 to 2 147, 483 647, beyond can be used long shape representation (L), we can also convert integers to grow integer (at the end of the input with "L")

Variable

This is no different from other programming languages, and if you've never been in touch with a program, it's easy to understand.

>>> x = 3 >>> x*2 6>>> x = 5 >>> x * 2 1

Assign 3 to X, then the x variable is now the value is 3, x multiplied by 2 The result is 6, and I assign the 5 to X, then the result of multiplying with 2 is definitely 10.

Define variable rules: You cannot start with a number, you can include letters, numbers, and underscores.

Statement

In fact, the first thing we've said is that the statement print is the printout statement.

>>> 2*2 4 >>> Print 2*2 4

Although the result is the same, the above is the expression, the following is the statement.

Get user input

A process that runs using a program requires user input and continues execution based on what the user enters.

>>> input (Input expression:) Enter an expression: 2*2 4

The second line of the program waits for the user to input, andthe 2*2 is freely entered by the user. Let's take a look at something more interesting.

Ah ~! Don't look silly, it's fun to knock with me! I promise you can't read the following!

Well, tell you, if 1 equals (= =)2, the condition is false (or not),print ' one equals one ' will not be executed.

If 1 equals (= =)1, the condition is true (set),print ' one equals ' will be executed.

Function

The Power operator (* *) was described earlier to calculate the exponentiation. In fact, a function can be used instead of an operator, which is the POW:

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

Functions are like small programs that can be used to implement specific functions. many of Python's functions do amazing things. We can also customize the function. Therefore, we usually refer to the functions provided by the POW and other systems as built-in functions.

>>> 10+pow (2, 3*5)/3.010932.666666666666>>> abs ( -10) 10>>> 18/131>>> round (18.4/ 13.7) 1.0

We're hot. You can use function calls and operators to create more complex expressions; the abs () function can get the absolute number of people, and theround function rounds the floating-point numbers to the nearest integer value.

Module

You can think of modules as extensions that are imported into python to enhance their functionality. You need to import the module using the Special Person command import .

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

The function of the floor function is to take the whole. Because the floor () function is part of the math module, you import the module (import math) before using it, and then use the function of the module in the format (module . function).

If you want to convert 32.9 to an integer (32) instead of afloating-point number (32.0), you can use the int function.

You may not want to write the module's name every time you call a function, if you are certain that you will not import multiple functions with the same name (imported from different modules). Then, you can use another form of the import command:

>>> from math import sqrt>>> sqrt (9) 3.0>>> sqrt (8) 2.8284271247461903

The sqrt () function is used to calculate the square root of a number.

Save and execute the program

I study in the Ubuntu process, a python command into the interactive interpreter. The interactive interpreter is one of Python's strengths, allowing real-time verification of the solution and experiencing the language as a reality. However, everything entered in the interactive interpreter will be lost when it exits. What we really want to do is write programs that you and others can run.

Below we create a file [email protected]:/hzh/python# touch hell.py[email protected]:/hzh/python# lshell.py[email Protected]:/hzh /python# vi hell.py Input: print "hello,world!" Save exit. Run [email protected]:/hzh/python# python hell.py hello,world!

See! This is a program that can be run all the time. All right! In this way, to do an interesting program.

Open the file and enter the program: Name=raw_input ("What's your name?") print "Hello" +name+ "!" Save exit, Run! [Email protected]:/hzh/python# python hell.py What is your Name?huhu      //Here asks the user to enter their own name Hello Huhu!

Python Basic Learning Notes (i)

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.