Python Basics (1): My dog jake--simple data types commonly used

Source: Internet
Author: User
Tags add numbers arithmetic square root

Python's annotation function: https://www.cnblogs.com/PythonInMyLife/p/6918909.html
In script mode, when you want to comment on a line, you can first click the line code, and then Alt+3 (here 3 is not the numeric keypad 3, but the Ribbon 3, which has the # of that). Alt+4, Cancel, 4 of the Ribbon.
When you want to annotate multiple rows, you can first select the desired area with the mouse and then alt+3, alt+4
It is important to note that in interactive mode, you cannot comment, and PY runs all the code. Comments can help you understand the code better.
Like this code is not able to return the correct answer in interactive mode, the PY will be 5 plus 2 also counted in. But in order to make it easier for the reader to understand, I added a comment later in the code that resembles script mode.
So the reader in the interactive mode of their own computer, do not add the comments in the OH. Annotations can only exist in script mode.

1 >>> 5 + 2 ;; 5 plus 22 7

Data type of 1.python--STR

(1) string (denoted by str in python)

such as ' name ', ' names ', with single or double quotation marks on either side of a string of characters, the data in this class becomes a string in the Py and is represented by Str.

1>>>'name'2 'name'3>>>"name"4 'name'5>>>'name'+'would!';; +the number is the meaning of stitching6 'name would!'7>>> 5 + 2;; +the number is the meaning of addition87

+ Number in mathematics is the sum of two numbers, in the Py, when the left and right side of the added object is two strings, it will return two strings of a new concatenation of strings. At this point the + number in the program is also called the concatenation operator, but when the left and right sides are full of numbers, PY will be like in mathematics, add numbers to return the results.
Like + 、-、 *, this type of symbol is called the arithmetic operator in the PY. For specific details, please see link 1 at the end of this article.

It is important to note that both sides of the character can be all single or double quotes and must be in English. But it cannot be a single quotation mark on one side and double quotation marks. This will cause an error because the form does not conform to the code specification of the PY.
At the same time, using the concatenation operation (+), the string and number cannot be added, this will be an error.

1>>>'name ";; single and double quotes with error2Syntaxerror:eol whileScanning string literal3>>>'name'+ 5;; strings and numbers cannot be added4 Traceback (most recent):;; error message5File"<pyshell#16>", Line 1,inch<module>6     'name'+ 5;; What line of code the error originated from7Typeerror:can only concatenate str ( not "int") to STR;; What's wrong?

(2) The print function and the line break in the string.

1>>>Print('Name\nis\njake')  ;; \ n is an escape character, which represents a newline2 name3  is4 Jake5>>>" "name;;" " " "three single or double quotation marks can also be used to indicate line breaks6  is7 Jake8 " "9 'Name\nis\njake';; Returns the resultTen>>>Print(" "name; This is the same as \ nthe effect One  is A Jake" ")                     ;; When the code is finished, enter to return the result automatically - The result returned by name; -  is theJake;; return end of result

The ' \ n ' in py refers to a newline character, in addition to \ r, \f, etc., which are collectively known as escape characters.
Other escape characters: http://bbs.fishc.com/thread-92997-1-1.html

1>>>Print(5)253>>>Print(5 + 3)                ;; Python can be arithmetic, bank code: Returns the result of 5+3485>>>Print(5 * 3)6157>>>Print('name'+' World')    ;; The plus sign is the meaning of the operation when the number is added, and when the string is added, it is the meaning of connecting the string together.8Name World

Print is a function in the PY. The function is used in the PY function to implement the function itself. Such as:
My dog Jake eats all kinds of food every day, every food, his tongue will change a color, if it eats noodles, its tongue may become yellow color, if eat apple, it will turn into green color, but if it eats several kinds of things at the same time, it might make the tongue colorful. Here we define a function whose name is ' Jake ', which is the function of not having to let Jake eat food, I can guess what color its tongue will be.
I put an apple into the function jake and the function automatically returns green. In that case, I used a function once. The function only has something to output when something is entered.
The print here is something like ' Jake '. Its function is to print something inside ' () '.
The official explanation is as follows: (You can use the function help to get the print function description, in interactive mode, input: Help (print))

Help on built-inchfunctionPrint inchmodule builtins:;; The following is a functional document for the print functionPrint(...) Print(Value, ..., sep=' ', end='\ n', File=sys.stdout, flush=False) Prints the values to a stream,orTo sys.stdout by default. Optional keyword arguments:file:a file-Like object (stream), defaults to the current sys.stdout.    Sep:string inserted between values, default a space.    End:string appended after the last value, default a newline. Flush:whether to forcibly flush the stream.

(3) What are the functions in str: You can find the answer through the Link 2 in the end, the link shows how to use the Help function, is through the STR example, there are several commonly used examples of functions.

There are included functions in each data type in the Py, which helps us to use the data for various functions.

For example, the following format function is a sample:

' thi{} '. Format ('s')' this ' This was {}'. Format ('Jake')'this is Jake'

2. Integral type--int

The number of integers is called integer in the py, denoted by int.

>>> 5 + 16>>> 5-14>>> 5 * 15>>> 5/2  ;; 5 divided by 22.5>>> 5//2  ;; 5 divided by 2, it is important to note that the same is 5 divided by 2, the result is 2.5 and 2. 2.          ; in the py, '//' will remove the decimal immediately after the value. >>> 5 * * 2 ; 5 2 power, i.e. 5 squared 25>>> 5 2  ; return, 5 divided by 2 remainder 1

Why 5/2, get 2, and 5//2, get 2.5? This involves the python rounding rule, see link 3.

3. Floating point--float

A decimal number is also called a floating point in Python, which is used to represent such numbers.

>>> 1.25          ; floating point 1.25>>> 5.0/2       2.5>>> 5/2.02.5>>> float (5) 
    5.0>>> Float (5)/2  ; float is a function with the same name as floating-point float, converting 5 to a decimal and dividing by 22.5

Here's a link that says the operation symbols for PY, such as subtraction, and their arithmetic priorities, which can help you better understand how the following code is calculated. The following example is a bit like a synthetic operation in mathematics.

Note that the link finally comes to the priority. Oh: http://www.runoob.com/python/python-operators.html

>>> ((3**2) * 3)/2      ; 3 squared times 3 divided by 213.5>>> ((3**2) * 3)/2% 3; the  remainder is divided by 3, which returns 1.5>>> 3** (a); the            square root 3 value, i.e. 3 of 1/2 1.7320508075688772>>> 4**            ;;Square Root 4 2.0

4. Conversions between the three

strings, integers, floating-point numbers are represented in Python in Str,int,float, and all three types have a function with the same name, which can be used to convert other data types to other types.

;; Str function: You can convert other data types to strings. >>> Str ('name')'name'>>> Str (1)'1'>>> Str (1.0)'1.0';; int function: You can convert a number to an integer. The string cannot be converted. >>> Int (1.1)1>>> Int (1)1;; float function: You can convert a number to a floating point. >>> Float (1.1)1.1>>> Float (1)1.0

5. Boolean value (BOOL)

There are two bool values in the PY, which are true and false, respectively. Their main functions are:
In the case of arithmetic: true represents the 1,false represented by the 2.
In the case where the proposition is true or false: True represents True, False represents false.

;; scenario 1:>>> true + 01>>> true + False1>>> true + true2  ;; Scenario 2:>>> 1 = = 1    ;; 1 equals 1? True>>> 1 = = 0    ;; 1 equals 0? False>>> 1 + 1 > 3;; is 1+ 1 greater than 3? False>>> A = 1     ;; In a py, an equals sign represents an assignment operation, and our code means assigning a variable name A to a value equal to 1>>> a 1>>> A + 2     ;; two equals is to determine whether the left and right sides are equal 3

The value of 6.python is None

Https://www.cnblogs.com/changbaishan/p/8084863.html

Link 1:http://www.runoob.com/python/python-operators.html

Link 2:http://www.cnblogs.com/nianqiantuling/p/9020772.html

Link 3:51581330/

Python Basics (1): My dog jake--simple data types commonly used

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.