🚀 Author: "big data Zen" 🚀 Welcome to praise 👍, Collection ⭐, Leaving a message. 💬
catalogue- python Number
- python data type conversion
- python String (String)
- python escape character
- python string operator
- python string formatting
- python three quotation marks
- python List
- Access values in the list
- Update list
- Delete list element
- Python list functions & Methods
- python tuple
- Access tuple
- Modify tuple
- Delete tuple
- Tuple built-in function
- Python dictionary
- Accessing values in the dictionary
- Modify dictionary
- Delete dictionary element
- Dictionary features and built-in functions
Now study every new knowledge hard in order to say less words of asking for people in the future
python Number
Python supports three different numeric types:
Integer (Int) - often referred to as an integer or integer, it is a positive or negative integer without a decimal point. Python 3 integers have no size limit and can be used as Long types, so Python 3 does not have the Long type of Python 2.
Float - a float consists of an integer part and a decimal part. A float can also be represented by scientific counting (2.5e2 = 2.5 x 102 = 250)
Complex number ((complex)) - the complex number is composed of real part and imaginary part, which can be used as a + bj, Or complex(a,b) means that the real part a and imaginary part B of the complex are floating-point
int float complex 10 0.0 3.14j 100 15.20 45.j -786 -21.9 9.322e-36j 080 32.3e+18 .876j -0490 -90. -.6545+0J -0x260 -32.54e100 3e+26J 0x69 70.2E-12 4.53e-7j
python data type conversion
Sometimes, we need to convert the built-in type of data. For data type conversion, you only need to use the data type as the function name.
int(x) converts x to an integer.
float(x) converts x to a floating point number.
complex(x) converts x to a complex number. The real part is x and the imaginary part is 0.
complex(x, y) converts X and y to a complex number. The real part is x and the imaginary part is y. X and y are numeric expressions.
python String (String)
String is the most commonly used data type in Python. We can use quotation marks ('or ') to create strings. Creating a string is as simple as assigning a value to a variable. For example:
va1 = 'Hello World!' va2 = "Python" va3 = "BIG data B Not limited to that"
python escape character
python uses backslashes \ to escape characters when special characters need to be used in characters. The following table:
\(At the end of the line) Continuation character \\ Backslash symbol \' Single quotation mark \" Double quotation mark \a Ring the bell \b Backspace(Backspace) \e Escape \000 empty \n Line feed \v Vertical tab \t horizontal tab \r enter \f Page change \oyy Octal numbers, yy Characters represented by, for example:\o12 Represents line feed \xyy Hexadecimal number, yy Characters represented by, for example:\x0a Represents line feed \other Other characters are output in normal format
python string operator
+ String connection >>>a + b 'HelloPython' * Duplicate output string >>>a * 2 'HelloHello' [] Get characters in string by index >>>a[1] be careful python The definition in is calculated from 0 'e' [ : ] Intercepts a portion of a string >>>a[1:4] 'ell' in member operator - Returns if the string contains the given character True >>>"H" in a True
python string formatting
Python supports the output of formatted strings. Although this may use very complex expressions, the most basic usage is to insert a value into a string with string formatter% s, as follows
>>>print "My name is %s and weight is %d kg!" % ('Zara', 21) >>>My name is Zara and weight is 21 kg! python String formatting symbol: %c Formatting characters and their ASCII code %s format string %d Format integer %u Format unsigned integer %o Format unsigned octal number %x Format unsigned hexadecimal number %X Format unsigned hexadecimal number (upper case) %f Format floating-point numbers to specify the precision after the decimal point %e Formatting floating point numbers with scientific counting %E Same function%e,Formatting floating point numbers with scientific counting %g %f and%e Abbreviation for %G %F and %E Abbreviation for %p Format the address of a variable with a hexadecimal number
python three quotation marks
Three quotation marks in python can copy complex strings:
python three quotation marks allow a string to span multiple lines. The string can contain line breaks, tabs and other special characters.
The syntax of three quotation marks is a pair of consecutive single quotation marks or double quotation marks (usually in pairs).
python List
Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number - its position, or index. The first index is 0, the second index is 1, and so on. Python has six built-in types of sequences, but the most common are lists and tuples. Sequences can perform operations, including indexing, slicing, adding, multiplying, and checking members. In addition, python has built-in methods to determine the length of the sequence and to determine the maximum and minimum elements. List is the most commonly used Python data type and can appear as a comma separated value within square brackets. The data items of the list do not need to have the same type. To create a list, just enclose the different data items separated by commas in square brackets. The code is as follows:
list1 = ['databasesB', 'mysql', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", "b", "c", "d"]
Access values in the list
Use the subscript index to access the values in the list. Similarly, you can intercept characters in the form of square brackets. The code is as follows
list1 = ['Bigdata class B', 'mysql', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5] >>>list1[0]: Big data B >>>list2[1:5]: [2, 3, 4, 5]
Update list
You can modify or update the data items in the list, or you can use the append() method to add the list items. The code is as follows:
list = [] ## Empty list list.append('Google') ## Add elements using append() list.append('Runoob') print (list) >>>['Google', 'Runoob']
Delete list element
The del statement can be used to delete the elements of the list. The code is as follows:
list1 = ['physics', 'chemistry', 1997, 2000] print (list1) del list1[2] print "After deleting value at index 2 : " print (list1) #list1[1:] this means to intercept the list from the second element list[-2]: read the penultimate element >>>['physics', 'chemistry', 1997, 2000] >>>After deleting value at index 2 : >>>['physics', 'chemistry', 2000]
Python list functions & Methods
Serial number function 1 cmp(list1, list2) Compare elements of two lists 2 len(list) Number of list elements 3 max(list) Returns the maximum value of a list element 4 min(list) Returns the minimum value of a list element 5 list(seq) Convert tuples to lists
python tuple
Python tuples are similar to lists, please pay attention!!! The difference is that the elements of tuples cannot be modified. Tuples use parentheses and lists use square brackets. Tuples are easy to create. You only need to add elements in parentheses and separate them with commas.
tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5 ) When a tuple contains only one element, you need to add a comma after the element tup1 = (50,)
Access tuple
A tuple can use a subscript index to access the value in the tuple. The code is as follows:
tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", tup1[0]) print ("tup2[1:5]: ", tup2[1:5]) >>>tup1[0]: physics >>>tup2[1:5]: (2, 3, 4, 5)
Modify tuple
Attention!!! The element values in tuples are not allowed to be modified, but we can connect and combine tuples
tup1 = (12, 34.56) tup2 = ('abc', 'xyz') ##The following operation to modify tuple elements is illegal. ## tup1[0] = 100 # Create a new tuple tup3 = tup1 + tup2 print( tup3 ) >>>(12, 34.56, 'abc', 'xyz')
Delete tuple
The element values in tuples are not allowed to be modified, but we can connect and combine tuples. The code is as follows:
tup = ('physics', 'chemistry', 1997, 2000) print tup del tup print "After deleting tup : " print tup >>>('physics', 'chemistry', 1997, 2000) >>>After deleting tup : Traceback (most recent call last): File "test.py", line 9, in <module> print tup NameError: name 'tup' is not defined
Tuple built-in function
1 cmp(tuple1, tuple2) Compare two tuple elements. 2 len(tuple) Calculate the number of tuple elements. 3 max(tuple) Returns the maximum value of an element in a tuple. 4 min(tuple) Returns the minimum value of the element in the tuple. 5 tuple(seq) Converts a list to tuples.
Python dictionary
A dictionary is another variable container model and can store objects of any type. Each key value key = > value pair of the dictionary is separated by colon, and each key value pair is separated by comma. The whole dictionary is included in curly brackets {}. The format is as follows:
d = {key1 : value1, key2 : value2 } The key is generally unique. If the last key value pair is repeated, the previous value will be replaced. It does not need to be unique. >>>dict = {'a': 1, 'b': 2, 'b': '3'} >>> dict['b'] '3' >>> dict {'a': 1, 'b': '3'} The value can take any data type, but the key must be immutable, such as string, number or tuple, such as: dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
Accessing values in the dictionary
Put the corresponding key in familiar square brackets, and the code is as follows:
dict = {'Name': 'jiayuan', 'Age': 18, 'Class': 'First'} print ("dict['Name']: ", dict['Name']) print ("dict['Age']: ", dict['Age']) >>>dict['Name']: jiayuan >>>dict['Age']: 18
Modify dictionary
dict = {'Name': 'jiayuan', 'Age': 18, 'Class': 'First'} dict['Age'] = 8 # to update dict['School'] = "gongcehng" # add to print( "dict['Age']: ", dict['Age']) print( "dict['School']: ", dict['School']) >>>dict['Age']: 8 >>>dict['School']: gongcheng
Delete dictionary element
You can delete a single element or empty the dictionary. Emptying requires only one operation. The del command for deleting a dictionary is displayed, and the code is as follows:
dict = {'Name': 'jiayuan', 'Age': 18, 'Class': 'First'} del dict['Name'] # Delete entry with key 'Name' dict.clear() # Clear all entries in the dictionary del dict # Delete dictionary print ("dict['Age']: ", dict['Age'] ) print ("dict['School']: ", dict['School']) The obtained results will be reported in error because del The dictionary no longer exists after deletion: dict['Age']: Traceback (most recent call last): File "test.py", line 8, in <module> print "dict['Age']: ", dict['Age'] TypeError: 'type' object is unsubscriptable
Dictionary features and built-in functions
The dictionary value can take any python object without restriction, which can be either a standard object or a user-defined object, but the key cannot be used. The same key is not allowed to appear twice. If the same key is assigned twice during creation, the latter value will be remembered. Keys must be immutable, so you can use numbers, strings, or tuples, so you can't use lists. Dictionary built-in functions and usage
1 cmp(dict1, dict2) Compare two dictionary elements. 2 len(dict) Calculate the number of dictionary elements, that is, the total number of keys. 3 str(dict) Output dictionary printable string representation. 4 type(variable) Returns the input variable type. If the variable is a dictionary, it returns the dictionary type. 5 dict.keys() Returns all keys of a dictionary in a list 6 dict.values() Returns all values in the dictionary as a list
PS: this concludes the summary of data types.