17/Precision Python Program Development Guide First Chapter

Source: Internet
Author: User
Tags arithmetic operators logical operators

Feature # #: Data type

1) Python represents integers using the type int, and a string (Unicode character sequence) using the str type

The integer size that python can represent is limited to machine memory, not a fixed number of bytes, and strings can be encapsulated in double or single quotes-as long as the symbol is symmetric, eg.

1 -9732 23156304"Python code"5 ' Python Code ' 6 " positvelyαβγ "
Examples of variables

Since Python uses Unicode encoding, the symbol for the string is not limited to acsii, such as the last string above

2) Python uses square brackets ([]) to access an item of a sequence such as a string, eg.

  

1 >>>"Hard Times"[5]2'T'  3 >>>"griaffe"[0]4' g '
use of square brackets

Traditionally, the Python shell uses >>> as a prompt; square brackets Access this syntax applies to data items of any data type (as long as it can form a sequence), such as a string or list, and note that the index position is counted from 0 in the Python syntax.

3) in Python, the STR type and the basic numeric type (such as int) are fixed; the only thing to say is that although you can retrieve the character of a string at an indexed position in square brackets, you cannot set it to a new character (in Python, the character is a string of length 1)

4) If you need to convert a data item from one type to another, you can use the syntax datatype (item), eg.

1 >>>int ("2 ")3 >>>str (912 )4'912'
Conversion of data types

the int () conversion allows spaces at the beginning and tail, as well as int ("45"), and the STR () conversion is suitable for almost all data items

Feature # #: Object reference

1 " Green " 2 " Blue " 3 z = x
Example

1) in the above several statements, the syntax is objectreference = value; Python does not have to declare the variable type beforehand, the variable type is determined by the value, and in the first statement above, Python creates a Str object with the text "green", and also creates an object application called X, and X refers to the Str object, which we can say " The variable x has been assigned "green"; in the third statement, an object reference named Z is created and set to the object referred to by x, and the name used for the object reference is called an identifier

2) Python is a case sensitive language, "Lim", "Lim", "Lim" is a three different identifier; When testing, you can use the type () function to return the data type of a given data item

1 >>>a =2 >>>type (a)3 <class'int  '>4'blue'5 >>>type (b) 6 <class'str'>
the use of the type () function

Feature # #: Combined data types

1) tuples and lists can be used to store any number of data items of any kind, tuples are fixed, immutable after creation, lists are mutable, and can be modified when needed; parentheses are tuples, square brackets are lists

1>>> list = [" One", 2,3,4,5]#Create a list2>>>len (list) returns the length of the data item using the Python built-in function len ()354>>>tuple = ("Allen","Jack","Mary")#To create a tuple5>>>len (tuple)63
tuples and Lists

Feature # #: Logical operators

1) Identity operator

The IS operator is a two-tuple operator, which returns true if the object reference on the left side of an object refers to the same object as the right end.

 1  >>>a = [1, " rention  "  ,none]  Span style= "COLOR: #008080" >2  >>>b = [1, " rention  "  ,none]  3  >>>a is   b  4  false  5  >>>a = 106  >>>b = a  7  >>> a  is   b  8  True 
is operator

You can also reverse-test the identity test with is not

The role of the identity operator is to see if two object references point to the same object, and if you want to compare object values, you should use the comparison operator

2) comparison operator

< represents less than, <= means less than or equal to, = = equals (= represents assignment, binds to object),! = is not equal, >= represents greater than or equal to,> represents greater than

1>>>a = 22>>>b = 63>>>a = =b4 False5>>>a <b6 True7>>>a! =b8 True9>>>a ="many"Ten>>>b ="many" One>>>a isb A False ->>>a = =b -True
comparison Operators

As can be seen from the above, A and B are different objects, but have the same value, so the result of comparison is equal

!!! You must have the same type of data to compare!!!

1 >>>a = 42 >>>0 <= a <=53 True
python comparison features

3) member operators

For data types such as a series or a collection, we can use the operator in to test the membership, and not to test for non-member relationships, eg.

1>>> p = [" is"," not","Yes"]2>>> 3inchP3 False4>>> YesinchP5 True6>>> s ="wa,you is so beautiful"7>>> REinchs8True
member Operators

3) Logical operators

In Python, logical operators have and,or,not; In a Boolean context, and and or return the result should be true,false; in short-circuit logic, the operand that determines the result is returned, eg.

1 # in short-circuit logic 2 >>> five = 53 >>> 24  and both5 26 or Five 7 2
logical Operators

Feature # #: Control flow statements

1) (supplemental) in Python, there is a special control flow, eg.

 1  x = 0   2  while  x < 5:  3  #   if x = = 3:  4  #   break  5  print   (x)  Span style= "COLOR: #008080" >6  x + = 17  else
    :  8   Print  ( " this is an abnormal operation  ) 
Loop ... else

If the loop exits normally, the subsequent else statement is executed, and if break jumps, it is not executed, such as the number of primes

Feature: arithmetic operator

1) Python has a set of arithmetic operators, including basic arithmetic + 、-、 *,/; and an enhanced operator +=,-=,*=,/=; an additional stripping operator//,eg.

1 >>> 5 + 2 2 7 3 >>> 5-2 4 3 5 >>>a = 3 6< /c5> >>> a *= 4    #  <==>  a = a * 4  7  8 >>> b = 24< c12> 9 >>> b/= 6    #<==>  b = B/64 >  >> 7/2 3.5 >>> 7/2 3
Arithmetic Operators

If you divide a decimal number, you can use the Peel operator if you need to produce an integer value

1>>> name ="Jack"2>>> name +"Joe"     #represents a connection of two strings, but does not change the original string3 'Jackjoe'4>>>name5 'Jack'6>>> name + ="Doe"    #extending the string will change the original string7>>>name8 "Jackdoe"
Connection of Strings

Feature # #: input/output

1) Python provides input () inputs, using print () output, eg.

1 Print("-----------------------------------")2Name = input ("Please enter your name:")#input () with parameter as prompt for information3 Print("Oh,your name is", name)4 5 #The results of the operation are as follows:6-----------------------------------7 Please enter your name:jack8Oh,your Name isJack
Input/Output

17/Precision Python Program Development Guide First Chapter

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.