Python3 Basic Data Type _ 1, python3 Data Type _ 1

Source: Internet
Author: User

Python3 Basic Data Type _ 1, python3 Data Type _ 1

As a last resort, you have to learn about python3. We have learned that py2 is very different from py3, but we can feel it after learning it, such as print.

However, the same code can be executed using py3 and py2, and the results are similar. Let's take a look.

Probably because I was a beginner, I haven't found any major difference. For example, some functions and methods have been discarded in py3.

The Code is as follows:

1 #! Urs/bin/python3 2 # coding: UTF-8 3 4 # define variables a, B, c and assign values to 5 a, B, c = 1, 5.3, "sub2020" 6 # value assignment type of output variables 7 print (type (a), type (B), type (c )) 8 9 # output string c length 10 print ("len (c):", len (c) 11 # output c12 print (c) 13 # output the character 14 print ("(c [0:-1]) between the first [0] and the last [-1] of c:", (c [0:-1]) 15 # output the first character 16 print ("(c [0]):", (c [0]) 17 # print the 18 characters ("(c []) between the c Index [1] and [6]):", (c []) 19 20 # There are two indexing methods for strings in Python, starting from left to right with 0, start from right to left and start with-1. 21 # print the 22 characters ("(c [-1:-4]) between the output index [-1]-[-4]):", (c [-1:-4]) 23 #24 print ("(c [-4: -1]): ", (c [-4:-1]) 25 # print (" (c [2:]) of characters After output index [2]: ", (c [2:]) 27 # output 2 times c28 print (" (c * 2): ", (c * 2 )) 29 # output result 30 print ('(c + "WWW"):', (c + "WWW") after two string links ")) 31 32 print ("*" * 60) 33 list1 = ['sub', 2020, 'sub2020', 20.20, 'http'] 34 list2 = ['www ', [1, 2, 3] 35 36 print ("list1:", list1) 37 print ("list2:", list2) 38 print ("len (list1 ):", len (list1) 39 print ("len (list2):", len (list2) 40 print ("(list1 [0: 4]):", (list1 [0: 4]) 41 # print ("(list1 [-1]):" (list1 [-1]) output error, list cannot use negative value index 42 print ("list2 * 2:", list2 * 2) 43 print ("list1 + list2:", list1 + list2) 44 45 # the elements in the List are changeable 46 list1 [4] = "new" 47 print ("new list1:", list1) 48 49 print ("*" * 60) 50 tuple1 = ('sub', 2020, 'sub202020', 20.20, 'http') 51 tuple2 = ('www ', [, 3]) 52 53 print ("tuple1:", tuple1) 54 print ("tuple2:", tuple2) 55 print ("len (tuple1):", len (tuple1 )) 56 print ("len (tuple2):", len (tuple2) 57 print ("(tuple1 [0: 4]):", (tuple1 [0: 4]) 58 # There are two indexing methods for tuples, starting from left to right with 0, starting from right to left with-1 59 print ("tuple1 [-1]:", (tuple1 [-1]) 60 print ("tuple2 * 2:", tuple2 * 2) 61 print ("tuple1 + tuple2:", tuple1 + tuple2) 62 63 # elements in tuple cannot be changed 64 # tuple1 [4] = "new" 65 # print ("new tuple1:", tuple1)

Py3 output:

<class 'int'> <class 'float'> <class 'str'>len(c): 7sub2020(c[0:-1]): sub202(c[0]): s(c[1:6]): ub202(c[-1:-4]): (c[-4:-1]): 202(c[2:]): b2020(c*2): sub2020sub2020(c+"WWW"): sub2020WWW************************************************************list1 : ['sub', 2020, 'sub2020', 20.2, 'http']list2 : ['www', [1, 2, 3]]len(list1) : 5len(list2) : 2(list1[0:4]) : ['sub', 2020, 'sub2020', 20.2]list2*2 : ['www', [1, 2, 3], 'www', [1, 2, 3]]list1+list2 : ['sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3]]new list1 : ['sub', 2020, 'sub2020', 20.2, 'new']************************************************************tuple1 : ('sub', 2020, 'sub2020', 20.2, 'http')tuple2 : ('www', [1, 2, 3])len(tuple1) : 5len(tuple2) : 2(tuple1[0:4]) : ('sub', 2020, 'sub2020', 20.2)tuple1[-1] : httptuple2*2 : ('www', [1, 2, 3], 'www', [1, 2, 3])tuple1+tuple2 : ('sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3])

Py2 output

(<type 'int'>, <type 'float'>, <type 'str'>)('len(c):', 7)sub2020('(c[0:-1]):', 'sub202')('(c[0]):', 's')('(c[1:6]):', 'ub202')('(c[-1:-4]):', '')('(c[-4:-1]):', '202')('(c[2:]):', 'b2020')('(c*2):', 'sub2020sub2020')('(c+"WWW"):', 'sub2020WWW')************************************************************('list1 :', ['sub', 2020, 'sub2020', 20.2, 'http'])('list2 :', ['www', [1, 2, 3]])('len(list1) :', 5)('len(list2) :', 2)('(list1[0:4]) :', ['sub', 2020, 'sub2020', 20.2])('list2*2 :', ['www', [1, 2, 3], 'www', [1, 2, 3]])('list1+list2 :', ['sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3]])('new list1 :', ['sub', 2020, 'sub2020', 20.2, 'new'])************************************************************('tuple1 :', ('sub', 2020, 'sub2020', 20.2, 'http'))('tuple2 :', ('www', [1, 2, 3]))('len(tuple1) :', 5)('len(tuple2) :', 2)('(tuple1[0:4]) :', ('sub', 2020, 'sub2020', 20.2))('tuple1[-1] :', 'http')('tuple2*2 :', ('www', [1, 2, 3], 'www', [1, 2, 3]))('tuple1+tuple2 :', ('sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3]))Traceback (most recent call last):  File "basic_data_type.py", line 66, in <module>    tuple2[1]=[1,2,3,4]TypeError: 'tuple' object does not support item assignment***Repl Closed***

Quote: http://www.runoob.com/python3/python3-data-type.html

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.