Python Basic Tutorial Notes chapter I

Source: Internet
Author: User
Tags integer division cmath

/ represents the divisible, when you import version in the _future_ module/ represents a normal division, it is available at this time // Represents the integer integer , whether the number is integral or floating point, can be used // represents an integer division.

* * indicates that a power party such as 2**3 indicates that 2 of the 3 - second-party result is 8

The * * operator has a higher precedence than a minus - operator, so -3**2 =-9 (-3) c15>**2 = 9

The Pow(3,2) function also represents a power-squared calculation, which represents 3 of the 2 - th side, and the result is 9

The input uses the input function,input("pleaseinput anumber:") this statement outputs Please input anumber: This prompt statement allows the user to enter data.

The ABS () function is to take the absolute value for example:abs ( -4) result is 4

The round () function is rounded to take an integer, unlike integer division, where integer division is the deletion of fractional parts. For example:round (4.5) result is 5, round (4.4) result is 4

The floor () function in the Math module is the largest integer that takes a floating-point integer that does not exceed the number. Example:Math.floor (32.9) result is 32.0

The Int(32.9) Coercion type conversion is also rounded down, and the result is also .

Corresponds to the floor of the ceil () function, which is to take no less than the number of integers.

There are two ways to import a Python module, one is import math directly imported module, so when using the function in the module is preceded by the name of the module, in case the function in the module and the function in other modules, the calling function is ambiguous. The other is the form math import sqrt a function from the module that can be used directly without having to precede the module name.

You can use variables to refer to functions in Python. For example:import Math math.sqrt (9) results for 3.0, when foo = math.sqrt foo (3) also has the same effect for 3.0 and the previous method.

When usingMathin the modulesqrtfunction to open the square root of a negative number when it appearsnanthe results,nanrepresents "Not a number"(non-numeric), this is becauseMathin the modulesqrtThe function can only handle floating-point numbers. UseCmath(Complex Math, plural) in the module.sqrtfunction to calculate the open root of negative numbers. For example:Import Cmath cmath.sqrt ( -1)The result is1j, this time theJorJrepresents the imaginary part. (1+3j)*(9+4j) The result is (-3+31j).

there is no separate imaginary type in Python, and all imaginary numbers are considered to be the complex number (the sum of real and complex numbers) that are divided into 0 of the real part.

Sometimes you want to run like other programs (such as a browser, a text editor)pythonprograms (also called scripts) without the need to display the usepythoninterpreter. In theUnixthere is a standard method: Add the first line of the script#! (Calledpound Bangorshebang), followed by an absolute path to the program that interprets the script (this ispython). #! /usr/bin/env pythonno matterpythonwhere the binaries are, the program executes automatically.

Some environments where older versions of Python are installed may need to modify the following absolute paths.

Before you actually run the script, you must have the executable properties of the script:

$ Chmod a+x hello.py

Now you can run the code like this:

$ hello.py

When running the program under window , it is possible that the program finishes and the window is closed, and the program's most raw_input ("Press <Enter>"), can solve this problem.

comments in Python are using the '#' sign

The ' single quotation marks and ' double quotes in Python Act the same, and can represent strings, except when a string contains single or double-tone numbers, use double or single quotation marks to express the string. Avoid ambiguity when interpreting code. Or use an escape character.

you can use the plus + + in Python to concatenate two of strings. However, you cannot use the plus sign to link strings and integers.

PythonThe two mechanisms in which the median value is converted to a string areStrand therepr, the former is to convert the value into a reasonable form of the string, so that the user's understanding. For example:Print str("Hello World") output asHello World. Print str (10000L)output to10000. The latter creates a string that takes a legitimatepythonthe form of an expression to represent a value. For example:Print repr ("Hello World")output to' Hello World '.Print repr (10000L)output to10000L. repr (X)function functions can be used with the' X 'to replace (note: It is an anti-quote, not a single quotation mark). For example:temp = repr (temp)and the' temp 'results in the same way that both produce a string' 42 '. This mechanism can be used to convert an integral type to a string and a string to add. (Python3is no longer used in anti-quotes, it is best to use allreprfunction)

in Python, input assumes that the user entered a valid expression. raw_input The input as the raw data and into the string. In cases where there is no special requirement, the raw_input function is used.

A long string can be entered in the middle of the three quotation marks, which can be multi-line, without the need to precede each line with a backslash \ as a newline flag. the string between the single quotation mark ' and the double quotation mark ' will need to be prefixed with a backslash \ to represent a line break.

Precede the string with R, which can represent the original string, for example:print R' C:\Program File\foo\bar ', input directly to C:\Program file\foo\ Bar, which does not require a transfer character. However, the last character of the original string cannot be a backslash \, the program will not know whether or not the string should end. If the string must have the most plus a backslash \, then you can add an escape character backslash \, or the last backslash of the string \ Write separately from the string.

Unicodestring. Normalpythonthe string is in8bitASCIIcode is stored,UnicodeThe string is in -bitUnicodecharacter store, which can represent more character sets, including special symbols for most languages in the world. Unicodethe use of strings with the original stringR, just precede the string with theu. For example:u"Hello World".Python3all the strings in theUnicodestring.

Python Basic Tutorial Notes chapter I

Related Article

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.