Python BASICS (str, list, tuple), pythontuple
Python isStrong type of Dynamic InterpretationDefinition Language (compiled first and then explained)
Dynamic Language
When programming in a dynamic language, you never need to specify a data type for any variable. This language records the data type in the internal part when you assign a value to the variable for the first time.
Interpreted type
Each time a program executes a certain command of the source program, a shell program called an interpreter converts the source code into binary code for execution, is to constantly explain, execute, explain, execute
Strong type
Once a variable is specified with a data type, if it is not forcibly converted, it will always be the data type.
Advantages: high efficiency, team development, porting, expansion, and embedding
Disadvantage: slow speed, no encryption, multi-thread parallel process prohibited
Tell the interpreter what encoding is used for execution
#!/usr/bin/env python
# -*- coding: utf-8 -*-Hexadecimal binary 01 octal 01234567 decimal 0123456789 hexadecimal 0123456789 ABCDEF comment # single line comment '''multiline comment ''' user input: input ('user input content ') print Screen name = 'sunlizhao'
Print ('I am % s' % (name) % s is a string % d is a Number % f is a floating point data type number int (integer), long (long integer ), float (float type), complex (negative number) Boolean true or false, 1 or 0, true or false string name = 'sunlizhao' print ('I am % s' % (name )) % s is string % d is Number % f is float string operation: name. capital name of capitalize. casefold () upper case all lower case name. center (50, '-') outputs the-character name on both sides of name. count ('s') counts the number of times s appears. name. the encode () character is encoded in bytes format name. endswith ('lz') determines whether the string ends with lz's \ tlz '. expandtabs (10) Description \ t is converted to a long space name. find ('Sl ') searches for sl and returns its index. If no index is found,-1 is returned.
format : >>> msg = "my name is {}, and age is {}" >>> msg.format("alex",22) 'my name is alex, and age is 22' >>> msg = "my name is {1}, and age is {0}" >>> msg.format("alex",22) 'my name is 22, and age is alex' >>> msg = "my name is {name}, and age is {age}" >>> msg.format(age=22,name="ale") 'my name is ale, and age is 22'format_map >>> msg.format_map({'name':'alex','age':22}) 'my name is alex, and age is 22'Num1 = [11,22, 33] num2 = (11,22, 33) # num3 = {'A' = 11, 'B' = 22, 'c' = 33} myStr = 'Hello word itcast and' # string operation print (myStr) print (myStr. find ("word") # returns-1 If no result is found, and returns the subscript print (myStr. rfind ("itcast") # search from the right side and return the following print (myStr. index ('word') # Same as find. If no value is found, the print (myStr. rindex ("itcast") # Same as find print (myStr. count ('E') # print (myStr. replace ('word', 'word', 1) # replace word with Word (one from left to right) print (myStr. split ('') # Cut the spaces in the string out of print (myStr. capitalize () # print (myStr. title () # print (myStr. lower () # convert all uppercase characters to print (myStr. upper () # convert all lowercase letters to uppercase lyric = "I want to watch the sea with you" print (lyric. center (50) # center print (lyric. ljust (50) # print (lyric. must ust (50) # Back To right print (lyric. strip () # clear spaces on both sides of the print (lyric. lstrip () # Clear the left space print (lyric. rstrip () # Clear the right space print (lyric. partition ("together") # split the string into three print (lyric. rpartition ("together") # search for (tuples) file_name = "xxxx.txt" print (file_name.endswith (". txt "cannot begin without suffix. txt is suffixed with print (file_name.startswith (" xxxx ") # determine if it is xxxx starting with numbe =" q "print (numbe. isalpha () # determine whether the letter is print (numbe. isdigit () # determine whether it is a digital print (numbe. isalnum () # determine whether it is a number or letter print (numbe. isspace () # determine whether it is a pure space. a = ["aa", "bb", "cc"] B = "" print ("". join (a) # connect the string in a with B
List, ancestor operations
Definition list names = ['Sun Li Zhao ', 'slz', 'sunlizaho'] subscripts starting from 0 names [0], names [1], names [2] inverted from-1 names [-1], names [-2], names [-3] Slice: take multiple element step sizes for 2 names [:: 2] if you want to get the last element, you cannot write-1 names [0:] append: Add names from the end. append ('I am new new') insert: insert names through index. insert (0, 'I am the first') Modify: Modify names through index [0] = 'I am the first' Delete: delete list def names to clear list names. clear () deletes the element content def names [0] names [0] = [] According to the subscript to delete the specified element names. remove ('slz') deletes the last names value in the list. pop () Extension: add the content of list1 to nam Names. extend (list1) Statistics: count the number of times that slz appears in names. count ('slz') Sorting/flip: sort by ascll code. Note that names cannot be sorted for different data types in py3. sort () sorts the entire list in reverse order (not in reverse order) names. reverse () to obtain the subscript: Obtain the subscript Based on the element content (only return the first one found) names. index ('slz') tuples: Once a tuple is created, t = (a, B, [c, d]) cannot be modified. tt = t [2] tt [0] = ett [1] = fprint (tt) Doesn't it mean it cannot be changed? Why can it be changed now! Tuple does not change, but the content in the list cannot be changed.