Python basic content: data types
1. Variables: Rules for variable definition: 1. The variable name can only be any combination of letters, numbers or underscores
2. The first character of the variable name cannot be a number
3. The following keywords Cannot be declared as a variable name
['and', 'as', 'assert',
'break', 'class', 'continue', 'def', 'del', 'elif', 'else ', 'except', 'exec',
'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'] Data type: 2. Numeric type : int (integer type) On a 32-bit machine, the number of integers is 32 bits, and the value range is -2**31~2**31-1, that is, -2147483648~2147483647. On a 64-bit system, the integer The number of digits is 64, and the value range is -2**63~2**63-1, that is, -9223372036854775808~9223372036854775807 long (long integer). Unlike C language, Python's long integer does not specify the bit width, that is: Python does not limit the size of long integer values, but in fact due to limited machine memory, the long integer values we use cannot be infinitely large. Note that since Python 2.2, if an integer overflow occurs, Python will automatically convert the integer data to a long integer, so now not adding the letter L after the long integer data will not cause serious consequences. float (Floating point type) Floating point numbers are used to deal with real numbers, that is, numbers with decimals. Similar to the double type in C language, it occupies 8 bytes (64 bits), of which 52 bits represent the base, 11 bits represent the exponent, and the remaining bit represents the symbol. Complex (complex number) A complex number consists of a real part and an imaginary part. The general form is x+yj, where x is the real part of the complex number and y is the imaginary part of the complex number. Here x and y are both real numbers. Note: There is a small number pool in Python: -5 ~
257 3. Boolean value True or False1 or 0 (values other than 0 are True) How to check the bool type of a variable? >>>bool(0) 4. The evil string splicing of strings: The string in python is reflected as a character array in C language. Every time you create a string, you need to open a continuous block in the memory. Empty, and once you need to modify the string, you need to open up space again. Every time the evil + sign appears, a new space will be opened up inside it. A simple understanding is to use the plus sign to splice strings, which wastes resources. String formatting name = 'ian'age = 12print('%s is %d years
old.' %(name,age))#The string is % s; integer %d; floating point number %f Commonly used functions of strings: str = 'ian is 12!! '# Remove blanks #This method will remove leading and trailing spaces and trailing \n newlines str.strip() # Split #split() will put the split fields into a list, separated by spaces by default, str.split(',') separated by commas s = str.split()print(type(s))print(s[ 1]) # Length print(len(str)) # Index # gives a string, which can output any character. If the index is a negative number, it is equivalent to counting from back to front. print(str[10])print(str[-4]) #Slice#Slicing is to separate part of the content from the given string print(str[0:3])print(str[:3]) 5. List creation list: list1 =
['apple','pear','peach'] or list1 =
list(['apple','pear','peach']) Common functions of lists: list =
['apple','pear','peach',66] # Index print(list[0]) # Slice, same as string print(list[0:2]) # Append list.append( 'banana')print(list) #Delete #remove method, delete elements, no return value #pop method, delete elements, return the value of the element, delete list from back to front by default.remove('banana')print(list) print('*' * 20)a = list.pop()print(a)print(list.pop(2)) #Delete peach, or use pop(-2) #Length, display the number of list elements print( len(list)) # Loop, how to loop a list? x = 0 #Add serial numbers to list elements for i in list: x += 1 print(x,i) #Include if 'apple' in list: print('in')else: print('out') 6. Origin Create a tuple: tuple1 =
('apple','pear','peach') or tuple1 =
tuple(('apple','pear','peach')) Commonly used operations for tuples: tuple1 =
('apple','pear','peach')#Tuples are basically the same as lists, but tuples cannot be modified after they are created, while lists can be modified# Index print(tuple1[1]) # Slice print(tuple1[1:3 ]) # Loop x = 0for i in tuple1: x += 1 print(x,i) # Length print(len(tuple1)) # Contains if 'apple' in tuple1: print('in')else: print(' out') 7. Dictionary Dictionary is unordered! ! ! ! Create a dictionary: dic =
{'k1':'v1','k2':'v2'} or dic =
dict({'k1':'v1','k2':'v2'} )Common operations on dictionary: dic =
{'k1':'v1','k2':'v2'} # Index #The index of the dictionary uses key as the key print(dic['k1']) # Add dic['k3'] = 'v3 'print(dic) # Delete #del is the same as remove in the list, the pop() method is still the same, delete the value and return del dic['k3']print(dic)del_key = dic.pop('k2')print( del_key)print(dic) #Key, value, key-value pair print('*'*20)dic =
{'k1':'v1','k2':'v2','k3':'v3 '}print(dic.keys()) #Only display keyprint(dic.values()) #Only display valueprint(dic.items()) #Display key and value # Loop for i in dic: #Default is .keys( ), you can use dic.values() or dic.items() print(i) # Length print(len(dic)) 8. Loop/range/break/continue#Loop #A simple for loop is as follows for i in [1,2,3]: print(i) #range function>>> range(1,5)
# represents from 1 to 5 (excluding 5)[1, 2, 3, 4] >>>
range(1,5,2) #Represents from 1 to 5, interval 2 (excluding 5)[1, 3]>>> range(5)
# Represents from 0 to 5 (excluding 5) [0, 1, 2, 3, 4] ps: The above is the display method of 2.0. It is not applicable in 3.0. In 3.0, you can use the loop to take out the #breakbreak statement, which can be used in for In loops and while loop statements. Simply put, the break statement will exit the loop immediately, and the loop body following it will not be executed. The #continuecontinue statement is also used in for loops and while loop statements. Using continue, you can skip this loop. The unfinished loop body does not loop, but directly proceeds to the next loop.
The above is the detailed content of Python basic content: data types. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The speed of mobile XML to PDF depends on the following factors: the complexity of XML structure. Mobile hardware configuration conversion method (library, algorithm) code quality optimization methods (select efficient libraries, optimize algorithms, cache data, and utilize multi-threading). Overall, there is no absolute answer and it needs to be optimized according to the specific situation.

It is impossible to complete XML to PDF conversion directly on your phone with a single application. It is necessary to use cloud services, which can be achieved through two steps: 1. Convert XML to PDF in the cloud, 2. Access or download the converted PDF file on the mobile phone.

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

An application that converts XML directly to PDF cannot be found because they are two fundamentally different formats. XML is used to store data, while PDF is used to display documents. To complete the transformation, you can use programming languages and libraries such as Python and ReportLab to parse XML data and generate PDF documents.

XML can be converted to images by using an XSLT converter or image library. XSLT Converter: Use an XSLT processor and stylesheet to convert XML to images. Image Library: Use libraries such as PIL or ImageMagick to create images from XML data, such as drawing shapes and text.

To generate images through XML, you need to use graph libraries (such as Pillow and JFreeChart) as bridges to generate images based on metadata (size, color) in XML. The key to controlling the size of the image is to adjust the values of the <width> and <height> tags in XML. However, in practical applications, the complexity of XML structure, the fineness of graph drawing, the speed of image generation and memory consumption, and the selection of image formats all have an impact on the generated image size. Therefore, it is necessary to have a deep understanding of XML structure, proficient in the graphics library, and consider factors such as optimization algorithms and image format selection.

XML formatting tools can type code according to rules to improve readability and understanding. When selecting a tool, pay attention to customization capabilities, handling of special circumstances, performance and ease of use. Commonly used tool types include online tools, IDE plug-ins, and command-line tools.

To convert XML images, you need to determine the XML data structure first, then select a suitable graphical library (such as Python's matplotlib) and method, select a visualization strategy based on the data structure, consider the data volume and image format, perform batch processing or use efficient libraries, and finally save it as PNG, JPEG, or SVG according to the needs.
