In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are Python data types and common operations". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Numerical value
The numeric data type is used to store numeric values, the data type is immutable, and the change allocates a new object. In Python, the and or not Boolean operation uses the keyword and/or/not.
Basic common operation codes:
Print (True+False) # output: 1dint true defaults to 0print (True or False) # output True, keyword or executes "or" operation print (5xpx2) # output2Cindy / to take the integer operator print (5% 2) # output1fuge% is the remainder operator print (3cm) # output 9j * represents the multiplier operation print (5x1.6) # output 6.6 The numerical addition of different precision types defaults to the highest precision type as the result.
Running result:
II. String
A String is a string of characters consisting of numbers, letters, and underscores, and objects are created by single, double, or triple quotation marks.
Basic common operation codes:
Str = 'Python' # assign the variable Str Pythonprint (len (Str)) # len (obj): return the length of the object, output 6print (Str,Str [1], Str [- 1]) # get the element print (Str+'Hello',Str*3) # output PythonHello PythonPythonPython: merge and repeat S =' Java'+Str [2:] # generate a new string Javathon and assign a value to S Str [2:]: print the second and subsequent string print ("Str:%s,S:%s"% (Str,S)) #% s means print string
Running result:
Expand the common operation codes:
S = "HelloWorld" # cut: str.split (str= "", num=-1), slice the string by specifying the delimiter. If the parameter num has a specified value, split the num+1 substrings.-1 means to split all print (S.split ('W')) print (S.split ('lame max 1)) # replace: str.replace (old,new [, max]), replace old with new, if the third parameter is specified Then replace no more than max times print (S.replace ('py','PY')) # str.upper (): return the value of converting lowercase characters to uppercase characters print (S.upper ()) # str.lower (): return the value of uppercase characters converted to lowercase print (S.lower ()) print (' Hello'.lower ()) # stitching: str.join (sequennce), sequence: the sequence to be connected Returns the elements in the specified character concatenation sequence and generates a new string print ('. Join (['Nothing','is', 'impossible']))
Running result:
III. List
List (list) can complete the data structure implementation of most collection classes, supporting characters, numbers, strings and even nesting lists, identified by [].
Basic common operation codes:
Animals = ['cat','dog','monkey'] # definition list animals#list.append (obj): add a new object animals.append (' fish') print (animals) # list.remove (obj) to the end of the list: remove the first occurrence of a value in the list animals.remove ('cat') print (animals) # list.insert (index,obj): used to insert the specified object into the list at the specified location Index: insert position animals.insert (1) print (animals) # list.pop ([index=-1]): to remove the element corresponding to the following table in the list (default last one), index: subscript animals.pop (1) print (animals)
Running result:
Expand the common operation codes:
Animals = ['cat','dog','monkey'] # definition list animals#enumerate (sequence): combines a traverable data object into an index sequence, listing both the data and the data subscript Commonly used in for loops: for i in enumerate (animals): print (I) # Index consisting of element subscript and element # list derivation squares = [xanth2 for x in animals] # batch generate a list of elements that conform to the rules print (squares) # sort # list.sort (): list ascending order list = [23 list.sort () print (list) # list.reverse (): element list.reverse () print (list) in reverse list
Running result:
IV. Tuple
Tuples are identified by (), internal elements are separated by commas, and tuples cannot be assigned twice, which is equivalent to a read-only list.
Basic common operation codes:
T = (1) print (7) + T1) # create tuple T1 = (9) print (T7) + T1) # tuple combined with T2 = (12, 13, 14, [11p22]) print (T2) T2 [3] [0] = 44 # the variable elements in the tuple can be assigned to change print (T2)
Running result:
5. Dictionary
Dictionaries (dictionary) are flexible built-in data structures identified by {}.
The dictionary consists of an index (key) and its value value. Compared with the list, the elements in the dictionary are accessed by keys, not offset access.
Basic common operation codes:
# three assignment operations of the dictionary D = {'animal':'cat','num':'10','color':'pink'} D1 = dict (animal='cat',num='10',color='pink') D2 = dict ([("animal", "cat"), ("num", "10"), ("color") "pink")]) print (D) print (D1) print (D2) # dict.copy (): copy data d = D.copy () d ['color'] =' red'print (D) print (d) # element access print (d.get ('name')) print (d.get (' name',' key does not exist!') Print (d.keys ()) print (d.values ()) print (d.items ()) d.clear () # clear all data in the dictionary print (d) # output {} del (d) # delete dictionary
Running result:
VI. Assemble
A set is an unordered sequence of non-repeating elements that can be created using the {} or set () function.
Basic common operation codes:
St = {'she','he','I','you'} # create a collection print (' I'in St) # find whether the element exists in the collection, return True, no longer return Falseprint ('her' in St) # set.add (obj): add an element There is no operation St.add ('her') # add element print (St) print (len (St)) # output set length # set.remove (obj): remove an element from the collection St.remove (' you') print (St) # list to remove repetition list1 = [1m 3J 5J 5J 3] # set immutable St = frozenset (St)
Running result:
This is the end of the content of "what are the Python data types and common operations". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.