Python self-study note two

Source: Internet
Author: User

1. Integer types and operations

Python's data types are: reshape, float, and string

1.1 Integer type

An integer is a number that is not part of a fractional number: such as: 25,-86,0, etc.

The operations supported by Python are: + 、-、 *,/,//(divisible), & (Redundancy), * * (exponentiation), etc.

Precedence of the 1.2 operator

* * &///*-+

1.3 Operation Accuracy

Unlike most other programming languages, Python has no limit on the length of the data type and can perform dozens of-bit or even hundreds of-thousands of-bit integer operations.

2 floating-point number types and operations

2.1 Floating-point number types

The floating-point type is a number with a decimal point, such as: -3.1,-2.89,-60,5. (5.0),. 6 (0.6)

All arithmetic operators for integer operations can be used for floating-point operations, including: +-*///% * *, etc.

For very large or very small floating-point numbers, it is usually expressed in scientific notation, for example, the 8.8**-5.4 evaluates to:
7.939507629591553E-06,E-06 represents 10 to 6 times, and the result is actually the preceding number multiplied by 10-6-time Square.

2.2 Unlike integers, floating-point numbers have upper and lower limits, and exceeding the upper or lower limits causes data to overflow and prompt for errors. An overflow error means that the result of the calculation is too large or too small for Python to represent as a floating-point number. Sometimes when an overflow error is encountered, Python will continue to operate to get the wrong result, we should avoid the overflow error and try to calculate it without too large a number.
Example: >>>500.0**1000
Run result hint: overflowerror: ("result too large"), indicating overflow error.

2.3 Data Accuracy
The precision of floating-point numbers is an unresolved problem, because floating-point calculation can sometimes get an infinite worth of results, especially when doing division or root calculation, it is difficult to get a precise value, for example: 2/3, the actual result should be after the decimal point has an infinite 6, But the result of the Python calculation is 0.6666666666666666.
This example results in a precision of 16-bit, which is generally not a problem with this subtle error. However, when performing a large number of calculations, small errors accumulate large errors. For example, in the field of scientific engineering computing, when calculating the pressure on a bridge, subtle errors can cause serious problems.

2.4 plural
Python provides support for complex numbers, mathematically, the unit of a complex number is the square root of 1, represented by 1j-1 square root. The field of scientific computation uses complex numbers, and we do not speak in detail.

3. String

A string is a series of characters, including letters, numbers, punctuation, and hundreds of other special symbols and non-printable escape characters.
(1). The representation of a string, in 3 different ways
A. Single quotes, for example: ' http ', ' Cat '
B. Double quotes, for example: "Hello, World", "Open House"
C. Three quotation marks, used to refer to multiple lines of string, such as:
“””
Hello, how is it?
What is your name?
“””
When you have long strings that generally have more than one line, enclose them in three quotation marks, and the contents of the three quotes can contain the double quote character "and the single quote character."
(2). The length of the string, with the built-in function len (s) can be used to get a string containing several characters, that is, the length of the string, S is the string parameter.
Example: Len ("pear"), the result is that 4,len ("") is the length of the empty string, with a value of 0
(3). String connection
The string can be added to a new string, such as:>>> ' hot ' + ' dog '
This operation is called a string connection, and to concatenate the same string multiple times, you can use the multiplication method. For example: >>>10* ' ha '
The result of a string connection becomes another string that you can use to concatenate strings wherever you want the string.
Example: >>>len (12* ' pizza ')
>>>len (' house ' + ' car ')
4 Getting Help
Python is an automated document language, and most functions and modules contain short explanations.
(1). List the functions in the module
After exporting the module with import, you can use the function dir (m) to list all functions of the module, import is the command for the export module, and M is the module name. The following function chapters will be explained in detail.
Example: >>>import Math
>>>dir (Math)
This example lists some of the functions of the math module, with names starting with double underscores (__) For more complex Python programming.
To see the complete list of Python built-in functions, enter >>>dir (__builtins__) after the prompt
You can use the function Help (f) To view the document helper information for a function.
Example: >>>help (SUM)
You can enter help () directly at the prompt, and then enter a module or function name to get detailed assistance information.
(2). Print the document string, Example: >>>print (math.acos.__doc__)
5. Type conversion
(1). Automatic conversion
When you include both integers and floating-point numbers in an expression, Python automatically converts integers to floating-point numbers.
Example: >>>25*8.5
The result is: 212.5
(2). Convert an integer or string to a floating-point number
You can use the function float (x) to convert an integer or string into a floating-point number.
Example: >>>float (3)
>>>float (' 3.5 ')
(3). Converts an integer or floating-point number to a string, and the function str (n) converts the specified digit to the corresponding string.
Example: >>>str (85)
>>>str (-9.78)

(4). Converting floating-point numbers to integers
This is a bit of a hassle and you need to decide how to handle fractional parts. the function int (x) removes the fractional part. and the function round (x) uses rounding method. Note: Python's floating-point number if the fractional part is 0.5, rounding will move to an even number, such as the result of round (8.5) is 8
Example: >>>int (8.64)
The result is: 8
>>>round (8.64)
The result is: 9
>>>round (8.5)
The result is: 8
(5). Convert a string to a numeric value
You can use the function int () to convert a string to an integer, and float (x) converts the string to a floating-point number.
Example: >>>int (' 3 ')
>>>float (' 3.5 ')
6. Variables and Assignments
(1). Definition of variables
variables, like functions, modules, and classes, have specific names, collectively known as identifiers. Variables in memory point to the address where a value is stored, and the assignment operator is used to define the variable.
Example: >>>fruit= ' Apple '
>>>fruit
' Apple '
Fruit is the variable name, which points to the string ' Apple ', the line of code fruit= ' Apple ' is called an assignment statement; = is an assignment operator that points a variable to a value.
(2). Naming rules for variables
A. Variable names are not limited in length, but the characters must be letters, numbers, or underscores.
B. The first character of a variable name cannot be a number, it must be a letter or an underscore.
C. Case-sensitive, tax, tax, and tax are different variable names.
D. The keyword cannot be a variable name, for example, if, else, while, Def, or, and, not, in and other keywords.
(3). Assignment statements
An assignment statement consists of 3 main parts: An lvalue, an assignment operator, and a right-hand value. For example: Var=value,var is an lvalue, = is an assignment operator, and value is an rvalue. An assignment statement has two purposes: Define a new variable, and have the defined variable point to a specific value.
Example: >>>x=5
>>>x+=8
The first assignment statement x=5 two functions, one creating a variable and the other assigning a value of 5. The second statement is to recalculate the value of x, after the calculation of the value of X is 13, before this statement needs to use an assignment statement to define the variable x, otherwise it will be an error. You can assign any value to a variable, including other variables, when defined.
Example: >>>x=3
>>>y=x
Define x First, then Y, and they all have a value of 3.
(4). Multiple assignments
In Python, there is a convenient way to assign values to multiple variables at the same time, such as: >>>x,y,z=1, ' 3.0 ',
There are 3 variables on the left side of the assignment operator, 3 values on the right, separate commas between each variable, and the values separated by commas.

(5). Value of the swap variable
A useful use of multiple assignments is to exchange the values of two variables, such as:
>>>a,b=5,9
>>>a,b
(5,9)
>>>a,b=b,a
>>>a,b
(9,5)
The meaning of statement a,b=b,a is to assign a value to variables A and b at the same time.

Python self-study note two

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.