search
HomeBackend DevelopmentPython TutorialWhat are the basic data types in python

What are the basic data types in python

Jun 11, 2019 pm 04:55 PM
pythonBasic data types

What are the basic data types in python

What are the basic data types of python? Let me introduce them to you one by one:

1. Numbers ---> int class

Of course, for numbers, Python’s number types are Int integer type, long long integer type, float floating point number, complex complex number, and Boolean value (0 and 1). Here we only introduce the integer type.

In Python2, the size of integers is limited, that is, when the number exceeds a certain range, it is no longer of int type, but of long type. In Python3, regardless of the size of the integer The length is collectively called an integer.

The main methods are as follows:

int -->Convert the string data type to int type, Note: The content in the string must be numbers

bit_length() -->Convert the number to binary and return the minimum number of binary digits

2, Boolean value --->bool class

For Boolean values, there are only two results, True and False, which correspond to 0 and 1 in binary respectively. There are too many values ​​of True, we only need to know what the values ​​of False are---》None, empty (i.e. [ ]/( ) /" "/{ }), 0;

Related recommendations: "python video tutorial"

3. String --->str class

About string is Python The most commonly used data type in , it has many uses. We can use single quotes '' or double quotes "" to create strings.

Strings cannot be modified. All about characters, we can introduce strings from aspects such as indexing, slicing, length, traversal, deletion, splitting, clearing whitespace, case conversion, determining what starts with, etc.

Create string

Slice

Index--> index(), find()

The difference between index() and find() The point is: if the character or sequence of the index is not in the string, for index--》ValueError: substring not found, and for find--> returns -1.

Length-->len()

Note: The len() method--> can also be used for other data types, such as checking the number of elements in lists, tuples, and dictionaries .

Delete--> del

Judge string content--> isalnum(), isalpha(), isdigit()

Case conversion--> capitalize(), lower(), upper(), title(), casefold()

Determine what starts and ends--> startswith(), endswith() 

Extension-- >expandtabs()

Formatted output-->format(), format_map()

Join method

Split--> split(), partition( )

Replacement-->replace

Replacement-->makestran 、translate

4、List --->list class

A list is composed of a series of elements arranged in a specific order. Its elements can be any data type, such as numbers, strings, lists, tuples, dictionaries, Boolean values, etc., and its elements can also be Modified.

The form is:

names = ['little-five","James","Alex"]2 #or 3 names = list(['little-five" ,"James","Alex"])

Index, slice

Append-->append() 

Expand-->extend()

Note: The difference between extend and append: --> The former adds elements as a whole, while the latter decomposes elements of data types and adds them to the list. Example:

insert() -->insert

pop() -->remove

remove()-->remove, del --> ;Delete

sorted()-->Sort, the default is forward order, add reverse =True, it means reverse order

5, Tuple --->tuple class

A tuple is an unmodifiable list. Its characteristics are similar to those of list. It is identified using parentheses instead of square brackets.

#Tuple name = ("little-five","xiaowu")print(name[0])

6. Dictionary --->dict class

The dictionary is a series of key-value pairs, each key-value pair is separated by commas, each key corresponds to a value, and the corresponding value can be accessed by using the key. disorderly.

The definition of the key must be immutable, that is, it can be a number, a string, a tuple, a Boolean value, etc.

The definition of value can be any data type.

Traverse -->items, keys, values

7, collection -->set class

About the definition of set: In my opinion, a set is like a basket. You can store things in it and take things out of it. However, these things are unordered and it is difficult for you to specify which one to take separately. things; at the same time, it can be filtered through certain methods to get the part of the things you need. Therefore, sets can be created, added, deleted, and relationally operated.

Characteristics of collections:

1. Deduplication

2. Unordered

3. Each element must be unique The variable type is (hashable type, which can be used as the key of the dictionary).

Create: set, frozenset

Add: add, update

Delete: pop, remove, discard

Relational operations: intersection &, union | , difference set - , intersection complement set^ , issubset , isupperset

Determine whether the relationship between two sets is a subset or parent set--> issubset , isupperset

The above is the detailed content of What are the basic data types in python. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

For and While loops: a practical guideFor and While loops: a practical guideMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

Python: Is it Truly Interpreted? Debunking the MythsPython: Is it Truly Interpreted? Debunking the MythsMay 12, 2025 am 12:05 AM

Pythonisnotpurelyinterpreted;itusesahybridapproachofbytecodecompilationandruntimeinterpretation.1)Pythoncompilessourcecodeintobytecode,whichisthenexecutedbythePythonVirtualMachine(PVM).2)Thisprocessallowsforrapiddevelopmentbutcanimpactperformance,req

Python concatenate lists with same elementPython concatenate lists with same elementMay 11, 2025 am 12:08 AM

ToconcatenatelistsinPythonwiththesameelements,use:1)the operatortokeepduplicates,2)asettoremoveduplicates,or3)listcomprehensionforcontroloverduplicates,eachmethodhasdifferentperformanceandorderimplications.

Interpreted vs Compiled Languages: Python's PlaceInterpreted vs Compiled Languages: Python's PlaceMay 11, 2025 am 12:07 AM

Pythonisaninterpretedlanguage,offeringeaseofuseandflexibilitybutfacingperformancelimitationsincriticalapplications.1)InterpretedlanguageslikePythonexecuteline-by-line,allowingimmediatefeedbackandrapidprototyping.2)CompiledlanguageslikeC/C transformt

For and While loops: when do you use each in python?For and While loops: when do you use each in python?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

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 Article

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools