Home Backend Development Python Tutorial Python basic content: data types

Python basic content: data types

Apr 01, 2017 pm 01:30 PM

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles