Records of Learning Experiences in CDA LEVELII Examination - Partial Contents
Directory Links:
https://blog.csdn.net/weixin_41744624/article/details/101125379
Updating to~
------------------
3. Basic Python syntax, basic data types, operators,
1.python basic syntax
message="hello world" print message Hello world
Variable assignment
a = 100 -- variable is int
b = "test" ---- variable is character x=y=z=1------ multiple assignment
x,y,z = 1,2,'string'- - multivariate assignment
2. Basic data types
There are six standard data types in Python3:
1.Number (number):
int, float, bool, complex (complex)
In Python 3, only one integer type, int, is represented as a long integer, and there is no Long in Python 2
There is no Boolean type in Python 2. It uses the number 0 for False and 1 for True.In Python 3, True and False are defined as keywords, but their values are 1 and 0, which can be added to numbers
Dividing a number consists of two operators: / Returns a floating point number, // Returns an integer
In mixed computing, Python converts integers to floating-point numbers
2.String (string)
Strings in Python are enclosed in single or double quotes, with backslashes\to escape special characters
#!/usr/bin/python3 str = 'Runoob' //Input: print (str) # Output string print (str[0:-1]) # Output all characters from first to second to last print (str[0]) # Output string first character print (str[2:5]) # Output from the third to the fifth character print (str[2:]) # Output all characters after the third print (str * 2) # Output string twice print (str + "TEST") # Connection String //Output: Runoob Runoo R noo noob RunoobRunoob RunoobTEST
Python uses backslashes () to escape special characters. If you don't want backslashes to be escaped, you can add an r before the string to indicate the original string:
>>> print('Ru\noob') Ru oob >>> print(r'Ru\noob') Ru\noob >>>
The backslash () can be used as a continuation character to indicate that the next line is a continuation of the previous line.
You can also use the. ""...'''Or.'''...''Across multiple lines.
Note that Python does not have a separate character type, one character is a string of length 1
Python strings cannot be changed.Assigning a value to an index location, such as word[0] ='m', can cause errors
Strings in Python are indexed in two ways, 0 from left to right and -1 from right to left.
>>>word = 'Python' >>> print(word[0], word[5]) P n >>> print(word[-1], word[-6]) n P
3.List (List)
List s are the most frequently used data types in Python.
Elements in lists can be of different types, support numbers, and strings can even contain lists (nested).
A list is a comma-separated list of elements written between brackets [].
As with strings, lists can also be indexed and truncated, and the truncated list returns a new list containing the desired elements.
The basic syntax format of the list is as follows:
#!/usr/bin/python3 list = [ 'abcd', 786 , 2.23, 'runoob', 70.2 ] tinylist = [123, 'runoob'] //Input: print (list) # Output full list print (list[0]) # First element of output list print (list[1:3]) # Output from the second to the third element print (list[2:]) # Output all elements starting with the third element print (tinylist * 2) # Output List Twice print (list + tinylist) # Connection List //Output: ['abcd', 786, 2.23, 'runoob', 70.2] abcd [786, 2.23] [2.23, 'runoob', 70.2] [123, 'runoob', 123, 'runoob'] ['abcd', 786, 2.23, 'runoob', 70.2, 123, 'runoob']
Unlike Python strings, elements in a list can be changed:
>>>a = [1, 2, 3, 4, 5, 6] >>> a[0] = 9 >>> a[2:5] = [13, 14, 15] >>> a [9, 2, 13, 14, 15, 6] >>> a[2:5] = [] # Set the corresponding element value to [] >>> a [9, 2, 6]
Attachment: Flip string example:
def reverseWords(input): # Separate words into lists by separating strings with spaces inputWords = input.split(" ") # Flip String # Assuming the list = [1,2,3,4], # list[0]=1, list[1]=2, and -1 represents the last element list[-1]=4 (as with list[3]=4) # inputWords[-1::-1] has three parameters # The first parameter - 1 represents the last element # The second parameter is empty, meaning move to the end of the list # The third parameter is the step, -1 represents the reverse inputWords=inputWords[-1::-1] # Recombine strings output = ' '.join(inputWords) return output if __name__ == "__main__": input = 'I like runoob' rw = reverseWords(input) print(rw)
runoob like I
4.Tuple (tuple)
Tuples are similar to lists, except that elements of tuple s cannot be modified.Tuples are written in parentheses (), with elements separated by commas.
Element types in tuples can also be different:
#!/usr/bin/python3 tuple = ( 'abcd', 786 , 2.23, 'runoob', 70.2 ) tinytuple = (123, 'runoob') //Input: print (tuple) # Output Full Tuple print (tuple[0]) # First element of output tuple print (tuple[1:3]) # Output starts with the second element and goes to the third element print (tuple[2:]) # Output all elements starting with the third element print (tinytuple * 2) # Output Twice Tuples print (tuple + tinytuple) # Connection tuples //Output: ('abcd', 786, 2.23, 'runoob', 70.2) abcd (786, 2.23) (2.23, 'runoob', 70.2) (123, 'runoob', 123, 'runoob') ('abcd', 786, 2.23, 'runoob', 70.2, 123, 'runoob')
Strings can be thought of as a special tuple, and although the tuple element is immutable, it can contain mutable objects, such as a list list list.
Constructing tuples with 0 or 1 elements is special, so there are some extra grammar rules:
tup1 = () # Empty tuple tup2 = (20,) # An element that needs to be followed by a comma
5.Set (Set)
A set is composed of one or more entities of various sizes and shapes. The things or objects that make up a set are called elements or members.
The basic functions are to test membership and delete duplicate elements.
You can use curly braces {} or the set() function to create a collection, note that creating an empty collection must use set() instead of {} because {} is used to create an empty dictionary.
The basic syntax format for a collection is as follows:
#!/usr/bin/python3 student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'} print(student) # Output set, duplicate elements are automatically removed # Member Test if 'Rose' in student : print('Rose In a collection') else : print('Rose Not in collection') # Set can perform set operations a = set('abracadabra') b = set('alacazam') print(a) print(a - b) # The difference between a and b print(a | b) # Union of a and b print(a & b) # Intersection of a and b print(a ^ b) # Elements that do not exist in a and b //Output: {'Mary', 'Jim', 'Rose', 'Jack', 'Tom'} Rose In a collection {'b', 'a', 'c', 'r', 'd'} {'b', 'd', 'r'} {'l', 'r', 'a', 'c', 'z', 'm', 'b', 'd'} {'a', 'c'} {'l', 'r', 'z', 'm', 'b', 'd'}
6.Dictionary
dictionary is another very useful built-in data type in Python.
Lists are ordered collections of objects and dictionaries are unordered collections of objects.
The difference is that elements in a dictionary are accessed by keys, not by offsets.
A dictionary is a mapping type, identified by {}, and is an unordered collection of keys: values.
The key must use an immutable type.
Keys must be unique in the same dictionary.
The basic grammatical format of a dictionary is as follows:
#!/usr/bin/python3 dict = {} dict['one'] = "1 - test" dict[2] = "2 - test" tinydict = {'name': 'runoob','code':1, 'site': 'www.test.com'} print (dict['one']) # Value with output key of'one' print (dict[2]) # Value of output key 2 print (tinydict) # Output complete dictionary print (tinydict.keys()) # Output all keys print (tinydict.values()) # Output all values //Output: 1 - test 2 - test {'name': 'runoob', 'code': 1, 'site': 'www.test.com'} dict_keys(['name', 'code', 'site']) dict_values(['runoob', 1, 'www.testb.com'])
The constructor dict() can construct a dictionary directly from a sequence of key-value pairs as follows:
>>>dict([('Runoob', 1), ('Google', 2), ('Taobao', 3)]) {'Taobao': 3, 'Runoob': 1, 'Google': 2} >>> {x: x**2 for x in (2, 4, 6)} {2: 4, 4: 16, 6: 36} >>> dict(Runoob=1, Google=2, Taobao=3) {'Runoob': 1, 'Google': 2, 'Taobao': 3}
Of the six standard data types for Python 3:
Invariant data (3): Number (number), String (string), Tuple (tuple);
Variable data (3): List (list), Dictionary (dictionary), Set (collection).
function | function |
---|---|
float(x) | Convert x to a floating point number |
int(x [,base]) | Convert x to an integer |
complex(real [,imag]) | Create a complex number |
str(x) | Convert object x to string |
repr(x) | Convert object x to expression string |
eval(str) | Used to evaluate a valid Python expression in a string and return an object |
tuple(s) | Convert sequence s to a tuple |
list(s) | Convert sequence s to a list |
set(s) | Convert to Variable Set |
dict(d) | Create a dictionary.d must be a (key, value) tuple sequence |
frozenset(s) | Convert to an immutable set |
chr(x) | Convert an integer to a character |
ord(x) | Converts a character to its integer value |
hex(x) | Converts an integer to a hexadecimal string |
oct(x) | Converts an integer to an octal string |
3. Expression-Operator (+, -, *, **, /,%)
An expression consists of values, variables, and operators
3 + 5 - - Addition
3 ** 2 - - - power
5/2 -- Division
5% 2 - - Module
'Hello'+'World'- -- String Addition (Note: String Performs Addition) >> 3 + (5 * 4) --- Mixed Transport