Python Initial Basic Data Type

Source: Internet
Author: User

Python Initial Basic Data Type
The data structure is divided into two parts: 1. Single-value integer: for 32-bit machines, the integer's number of digits is 32 bits, and the value range is-2 ** 32 ~ 2 ** 31-1, that is,-2147483547 ~ -2147483548 for 64-bit machines, the integer's number of digits is 64-bit, and the value range is-2 ** 63 ~ 2 ** 63-1:-9223372036854775808 ~ 9223372036854775807 long integer: relatively large numeric floating point: 3.14 plural: z = a + bj Boolean value: True (1), False (0) Example ① operation: >>> I = 123 >>> print type (I) <type 'int' >>>> j = True >> print type (j) <type 'bool '>>>> name = 'Alex' >>> print type (name) <type 'str' >>>> The placeholders % s and % d in the string Python are represented as placeholder values for the string and number types, respectively, the type of the value to be filled later must be characters and numbers. Example ① >>> name = 'I am % s' % 'Tony' >>> print namei am tony >>> name001 = 'I am % s, age is % d' % ('Alex ', 30) >>> print name001i am Lex, age is 30 >>> example ② >>> name = 'I am {0}, age is {1}, come from {2}' >>> name. format ("alex", 30, "china") 'I am alex, age is 30, come from China' >>> conclusion: the placeholder values of Python can be defined by % and {}. The two methods are essentially the same, but the expressions are different. Python has three quotation marks to indicate that the strings are single quotation marks (''), double quotation marks ("), and three quotation marks ("). The single quotation marks and double quotation marks represent the strings of a single row, three quotation marks can indicate the index of string characters in multiple rows: example 1> name = "alex"> name [0] # ----------> get string 1st characters 'A'> name [1] # ----------> get String 2nd characters 'l' >>>> name [2] # ----------> get string 3rd characters 'E' >>> name [3] # ------------> get string 4th characters' x'> name [4] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range >>> character slice: Example ② >>> print name [0: 2] # -------> get 1st and 2nd characters of a string al >>> print name [0: 3] # --------> get 1st to 3rd characters of a string ale >>> print name [] # ----------> get a string of 2nd to 3rd characters le >>>>>> print name [0:] # -----------> get 1st characters and all following, that is, all characters alex >>> print name [1:] # ------------> Get 2nd strings and all following strings, that is, from 2nd to all the subsequent characters lex >>> print name [-1] # ------------> get the first to last character of the string, that is, from the last character x> note: for string slices, such as name [n: m] (n and m are integers), it means that the left side contains no content on the right side, that is, from the nth to m-1) evaluate the number of characters in the string (that is, the number of characters in the string): Example ③ >>> Print namealex >>> print len (name) 4 >>> to get the last character of a string, use the following method: >>> print namealex >>>>> print name [-1] # ------------> print the subscript x or >>> print name [len (name) -1] # ----> use the value after the string length minus 1 as the subscript x of the last character of the string >>> Note: For strings, indexes (I .e. subscript) the last value is smaller than the length value of the string. 1. Remove the space from the string. 4. >>>> name = "alex" >>> print name alex >>>> print name. strip () # -------------> remove the spaces at both ends of the string alex >>> print name. lstrip () # -------------> remove the space on the left of the string alex >>> pri Nt name. rstrip () # --------------> remove the space alex on the right of the string> split the string using the specified separator (', ', 'A',' \ t') is split into a List Example ⑤ >>> names = "alex, eric, tony" >>> names. split (',') ['Alex ', 'Eric', 'Tony '] >>> names. split ('A') ['', 'lex, eric, tony '] >>> names. split ('T') ['Alex, eric, ', 'ony'] >>> string appending Element Method append, at the same time, the memory address of the string does not change. Example 6> name_list.append ('seven') >>> name_list ['Alex ', 'seven', 'Eric ', 'seven'] >>>>> name_list.append ('Hello') >>>> id (name_list) 140546186766960 >>>>>> name_list ['Alex ', 'seven', 'Eric', 'seven ', 'Hello'] >>> example del for deleting string elements> name_list ['Alex ', 'seven', 'Eric', 'seven ', 'Hello'] >>> del name_list [0] >>> name_list ['seven', 'Eric ', 'seven ', 'Hello'] >>>> del name_list [1] >>> name_list ['seven', 'seven', 'Hello'] >>> if the string is modified, we need to re-allocate the memory space example >>> list1 = "good evening everyone! ">>> Id (list1) 140546186876528 >>> list1 =" good morning everyone! ">>> Id (list1) 140546186876656 >>> 2. The list of sets is connected to a string through the join method, you can define the connection method as follows: ① >>> name_list ['seven', 'seven', 'Hello'] >>> "-". join (name_list) 'seven-seven-hello'> "". join (name_list) 'seven seven hello '>>> "#". join (name_list) 'seven # seven # hello '> judge whether an element is used in the list --- in. The returned result is a Boolean value (False or True) example ② >>> name_list ['Alex ', 'seven', 'Eric', 'seven ', 'Hello'] >>> "alex" in name_listTrue >>> "tony" in name_listFalse>> "Seven" in name_listTrue> the literal difference between the tuples and the list is: A. The tuples are defined in parentheses (), the list is enclosed in brackets [] B. Elements in the tuples cannot be modified, added, or deleted, in the list, all operations C, tuples, and lists can be performed on the elements in the list, and the values of indexes can be sliced. The length can be obtained through len, including (in )) if the loop D and string are modified, you need to re-allocate the memory space. Example ① list: >>> name_tuple = ('Alex ', 'seven', 'Eric ', 'seven', 'Hello') tuples: >>> name_list = ['Alex ', 'seven', 'Eric', 'seven ', 'Hello'] example ② >>> list1 = ["alex", ('seven', 'Eric ')] >>> print list1 ['Alex ', ('seven', 'Eric ')] >>> list list1: The first The element is alex, and the second element is: ('seven', 'Eric ') You can modify these two elements, but for the second element ('seven', 'Eric ') the elements in it cannot be modified, because the second element is a tuples, and then the operation object changes. We often say that the list can be added, deleted, modified, and so on. The smallest unit of the list for the smallest unit is the element, and the smallest unit of the tuples is also the element, but in list1, ('seven', 'Eric ') Although it is a list, it is also an element of list1. we replace ('seven', 'Eric') as a whole, however, the elements in ('seven', 'Eric ') cannot be further modified. Summary: # str applies to str, list, And tuple for slice, index, len (), inclusion, and loop: If modified, the space needs to be re-allocated, and the memory address changes. list: after modification, the memory space address remains unchanged. tuple: dict cannot be modified: dictionary, key-value pair, Dictionary unordered, special for loop. items (), keys () ----> List vlaues () ----> list items () -------> used only for loop, assign the element to k, v (defined by k and v) for loop structure for ele in (list, str, tuple) >>> for ele in name_list :... print ele... rickcoolericsevenhello >>>>> for ele in name_tuple :... print ele... alexsevenericsevenhello >>>>> for ele in Str:... print ele... dafagaegahg >>>> [root @ Python scripts] # vim find. py #! /Usr/bin/env python #-*-coding: utf8-*-name_list = ['Alex ', 'xiaoeyue', 'wenqiang '] for ele in name_list: if ele = "alex": print "sb" # This loop no longer executes continue if ele = "xiaoyueyue ": print "find" # break [root @ Python scripts] # python find. pysbfind while loop structure while condition: print '000000' if the condition after while is true (that is, the Boolean value is true), the subsequent statement continues; specifically: spacer.gifwKiom1Y9-EezklrXAACV2tma7OE990.jpg prompt: if the value is False after the while clause, only step 1 is executed, and step 2 is not followed. The dictionary stores data in the form of key: value pairs. It is defined as follows: person = {"name": 'Alex ', "age": '18', "gender ": "1",} person. keys () # ---> all keys person. values () # ----> so the value of person. items () # ---> all values in the key-Value Pair (element) dictionary are paired with keys for k, v in person. items (): print k, v [root @ Python scripts] # python dict_test.py gender 1age 18 name alex [root @ Python scripts] # It is found that the print order is not the original storage order, therefore, the stored data in the dictionary is unordered. For k, v in person. intems (): print k # -------> print v # -------> in list form print items () # ------> all elements

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.