The second chapter of python----type and operation

Source: Internet
Author: User
Tags python list
Python supports 5 data types, including numbers (numbers), strings (string), lists, tuples (tuple), and dictionaries (dictionary). As a dynamic type language, it is not necessary to declare the type of an identifier, and what type will be determined automatically when used.

For a well-defined variable, if you do not use the variable, you can use del Delete to release the owning resource, such as

var = 10del var

Python Number (numbers): Supports int, long, float, and complex, which is relatively simple and does not show an example.

Python string: As mentioned above can be defined by a variety of quotation marks, support [P] (get p position character), [L:r] (Get the string from L to r Cutoff), *t (Get t the string that strings are linked together) and + S (get the string after the link with s) method (methods, why use the method, with Ruby, all objects of the. Methods can show all the actions that can be performed, so I'm also starting to call a method of what an object can do), as in the following example

s = ' This was a test case. ' Print s[0] #输出s的第一个字符 ' T ' print s[0:4] #输出s的第一个到第四个字符组成的字符串 ' This ' print s*2 #输出两个s字符串链接的新字符串print s + ' good! ' #输出s与 ' good! ' The string after the link

Python list: Use [] to define, use a container vector like C + +, but this container can be loaded with different types of values, and can be nested definition, a list contains another list, the method is similar to string, the following example

L = [' Wyp ', 1, 3.14, [2, ' CC ']]print l #输出整个lprint l[0] #输出l的第一个元素 ' wyp ' print l[3][0] #输出l的第四个元素的第一个元素2print L[0:3] #输出l的第一 Elements to a third element print l*2 #输出两个l链接在一起的列表print l+['. ' #输出l与列表 ['. '] A list that is linked together

Python tuple (tuple): Defined with (), the method is similar to a list, the only difference is that it cannot be assigned two times, so no example is given.

Python Dictionary (dictionary): defined using {}, defines methods like JSON, uses a container map like C + +, and gives examples

D = {1: ' Wyp ', ' both ': ' Is ', 3.0: ' Coder '}
d[' four '] = ' like ' d[5] = ' farmer ' Print D #输出整个字典dprint d[1] #输出键为1的值 ' wyp ' Print D.keys () #输出所有的键print d.values () #输出所有的值

Python type conversion: Visual use is not much, below to show examples

int (x) #把x转成int型, rounding directly, regardless of the trailing long (x) #把x转成long型, directly rounding, regardless of the trailing mantissa, float (x) #把x转成float型complex (y) #创建一个x为实部, y is the complex CHR of the imaginary part ( x) #转成对应ascii码x的字符ord (c) #转成c对应的ascii码unichr (x) #把x转成unicode字符hex (x) #把x转成它的十六进制的字符串oct (x) #把x转成它的八进制的字符串str (x) # Convert x to string eval (str) #把一个字符串表达式str的值算出来tuple (s) #把序列型结构的s转成元组list (s) #把序列型结构的s转成列表dict (s) #把序列型结构且每个元素都是二元组的s转成字典

--------------------------------------------------------------------------------------------------------------- --------------------------------

--------------------------------------------------------------------------------------------------------------- --------------------------------

--------------------------------------------------------------------------------------------------------------- --------------------------------

Python's operations are very similar to C + +, which are divided into arithmetic operations, comparison operations, logical operations, and bitwise operations, which are based on the addition of member operations and identity operations, and the following are examples of discrepancies between C + +

Arithmetic operations: Extra operators have * * and//, so more **= and//= are given to show examples

A * * b #即a ^b,a B-Side A//b #先a-B and then direct rounding

Comparison operation: The extra operator is "<>", as with the "! =" function, so no example is given

Logical operation: Here is a bit different, the logical operations are all expressed in English, that is, with (and), or (or) and non (not), the use of the same is not to show an example

Bitwise operations: Same as C + +

Member operations: Operators are in and not in, as in for example, if an element is true in a sequence, false, and the other is negation, the following shows an example

t = [1, 2, 3, 4]1 in T #返回true1 No in T #返回false0 in T #返回false0 #返回true

The identity operation: is and not, that is, to determine whether two variables are the same storage unit, where a small python details are found, its 0~256 these integers are all in memory, and the others are in the definition of the direct allocation of new memory, the following example

A = -1B = -1a is b #值为false, found here-1 is not python pre-stored in memory A + = 1b + = 1while (A is b): #在256之前都为true
A + = 1
b + = 1 Print str (a) + ', ' + str (b) + ' \ n '

The above is the Nu Learning python--second article-type and operation of the content, more relevant content please pay attention to (!

  • 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: 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.