Go to Python data type detailed

Source: Internet
Author: User

Python data types are detailed in catalog 1, String 2, Boolean Type 3, Integer 4, floating point 5, Number 6, List 7, tuple 8, Dictionary 9, date 1, string 1.1, how to use string A in Python, use single quotation marks (') to denote strings in single quotes, for example: str= ' The IS string ';p rint str;b, using double quotation marks (") in double quotation marks is exactly the same as string usages in single quotes, for example: Str=" This is a string ";p rint str;c, using three quotation marks (" ") to use three quotation marks, A string that represents multiple lines, which can be used freely in quotation marks and double quotes, for example: Str= "This is Stringthis is Pythod stringthis is a string" Print Str;2, Boolean type bool=false; Print Bool;bool=true;print bool;3, integer int=20;print int;4, floating-point float=2.3;print float;5, numbers include integers, floating-point number. 5.1, delete the numeric object reference, for example: A=1;b=2;c=3;del A;del B, C; #print A; #删除a变量后, the call to a variable will error 5.2, numeric type conversion (x [,]) converts x to an integer (x) converts x to a floating-point number complex (real [, imag]) to create a complex str (x) to convert object x to a string repr (x) to convert object X The expression string eval (str) is used to evaluate a valid Python expression in a string and returns an object tuple (s) converts the sequence s to a tuple list (s) converts the sequence s to a list Chr (x) converts an integer to a character unichr (x) Converts an integer to a Unicode character, Ord (x) converts a character to its integer value, Hex (x) converts an integer to a hexadecimal string, the OCT (x) converts an integer to an octal string 5.3, a math function 10104.15Ten117182818284590451004.9401001001002.042 6, List 6.1, initialization list, for example: list=[' physics ', ' Chemistry ', 1997, 2000];nums=[1, 3, 5, 7, 8, 13, 20];6.2, access the values in the list, for example: Print, nums[] Prin T, nums[:] Print, nums[:] print, nums[:-] print, nums[:]6.3, update list, for example: 0 "LJQ"; Print nums[];6.4, delete list element 0]; Print, nums[:];6.5, List script operator list pairs + and * operators are similar to strings. The + sign is used for the combined list, and the * number is used for repeating lists, for example: 1233123456123456 ' hi! ' 4' hi! ' hi! " hi! " hi! ' 3in123forin123123 6.6, List intercept ' spam ' ' spam ' spam! '; Print l[]; # print l[-]; # print l[:]; #[,]6.7, List functions & methods 17, tuple (tuple) Python's tuples are similar to lists, except that the elements of a tuple cannot be modified; tuples use parentheses (), and the list uses square brackets []; tuple creation is simple, just add elements in parentheses and use commas (,) Separated, for example: ' Physics ' chemistry ' 19972000); Tup2 = (,,,,); Tup3 =,,,;  creates an empty tuple, for example: Tup = (); When there is only one element in the tuple, you need to add a comma after the element, for example: Tup1 = (50), a tuple is similar to a string, the subscript index starts at 0, and can be intercepted, combined, and so on. 7.1, access to the tuple ' physics ' chemistry ' 19972000); #tup1 []: Physics Print, tup1[] #tup1 [:]: (,) print, tup1[:]7.2, modifying element values in tuple tuples is not allowed to be modified, but we can concatenate combinations of tuples, for example: Tup1 = (12, 34.56); tup2 = (' abc ', ' xyz '); # The following modification of tuple element operations is illegal. # Tup1[0] = 100;# Create a new tuple Tup3 = Tup1 + tup2; Print Tup3; # (,,,) 7.3, delete the element value in the tuple tuple is not allowed to delete, you can use the DEL statement to delete the entire tuple, for example: ' Physics ' chemistry ' 19972000); Print tup; Del tup;7.4, tuple operators, like strings, can be operated between tuples using the + sign and the * number. This means that they can be combined and copied, and a new tuple is generated after the operation. 7.5, tuple index & intercept ' spam ' spam ' spam! '); Print l[]; # print l[-]; # print l[:]; #[,]7.6, tuple built-in functions 8, Dictionary 8.1, Dictionary Introduction Dictionary (dictionary) are the most flexible built-in data structure types in Python except for lists. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets. A dictionary consists of a key and a corresponding value. Dictionaries are also referred to as associative arrays or hash tables. The basic syntax is as follows: ' Alice ' 2341 ' Beth ' 9102 ' Cecil ' 3258 ' The can also create dictionaries like this: ' ABC ' 456}; Dict2 = {:,:}; Each key and value must be separated by a colon (:), each pair is separated by commas, and the whole is placed in curly braces ({}). The key must be unique, but the value is not necessary; The value can take any data type, but it must be immutable, such as a string, a number, or a tuple. 8.2. Access the value in the dictionary ' name ' ' Zara ' ' Age ' 7 ' class ' first '; print, dict[]; Print, dict[];8.3, modify the dictionary to add new content to the dictionary by adding new key/value pairs, modifying or deleting existing key/value pairs as follows: ' Name ' Zara ' Age ' 7 ' class ' first}; dict[]=; #修改已有键的值 dict[]=; #增加新的键/value pair print, dict[]; Print, dict[];8.4, delete dictionary del dict[' name ']; # Delete key is ' name ' entry dict.clear (); # Empty dictionary all entries del dict; # Delete Dictionaries For example: ' Name ' ' Zara ' ' Age ' 7 ' class ' first '; Del dict[]; #dict {:,:} print, dict; Note: The dictionary does not exist, Del throws an exception 8.5, the dictionary built-in function & method Getdefaultdefault9, date and time 9.1, gets the current time, for example: import times, Datetime;localtime = Time.localtime (Time.time ()) #Local current Time:time.struct_time (tm_year=2014, tm_mon=3, Tm_mday =21, tm_hour=15, tm_min=13, tm_sec=56, tm_wday=4, tm_yday=80, tm_isdst=0) print "Local Current time:", localtime Description: TIME.S Truct_time (tm_year=2014, tm_mon=3, tm_mday=21, tm_hour=15, tm_min=13, tm_sec=56, tm_wday=4, tm_yday=80, tm_isdst=0) Belonging to the Struct_time tuple, the Struct_time tuple has the following properties: 9.2, the time to get the format can be selected according to the requirements of various formats, butThe simplest function to get a readable time pattern is asctime (): 2.1, date converted to string '%y-%m-%d%h:%m:%s '); Second: Print Datetime.datetime.strftime (Datetime.datetime.now (),) Finally: Print str (Datetime.datetime.now ()) [:]2.2, The string is converted to the date "2013-05-21 09:50:35" "%y-%m-%d%h:%m:%s"9.3. Get Date Difference120140321201403202014032220140321000000 '%y-%m-%d%h:%m:%s ' 00000.0010001000011001001100001100000170000019.4, Get the time difference 100000120140321160723.94300020140320160723.94300020140322160723.943000 '%y-%m-%d%H:%M:%S '%Y-%m-%d%H:%M:%S '%y-%m-%d%h:%m:%s ' 9.5, get last day of last month 119.6, string date formatted as seconds, return floating-point type: "2013-05-21 09:50:35" "%y-%m-%d%h:%m:%s" 9.7, date formatted as number of seconds, return float type: 9.8, Seconds to string "%y-%m-%d%h:%m:%s" Category: Python Tags: python string, Python boolean type, python integer, Python floating point, Python number, Python list, p Ython tuple, Python dictionary, Python sequence

Go to Python data type detailed

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.