What are the common built-in data types in Python?
Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.
Numeric type
Python supports four different numerical types -
int − They are often called just integers or ints, are positive or negative whole numbers with no decimal point.
long − Also known as longs, they are infinite-sized integers written as integers and followed by an uppercase or lowercase L.
float − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).
complex − These are complex numbers of the form a bJ , where a and b are floating point numbers and J (or j) represents the square root of -1 (that is, an imaginary number). The real part of the number is a and the imaginary part is b. In Python programming, complex numbers are rarely used.
Example
is:Example
Let us see an example −
# Python int val1 = 25 print(val1) # Python float val2 = 11.89 print(val2) # Python complex val3 = 6+2.9j print(val3) # Python hexadecimal val4 = 0x12d print(val4) # Python octal val5 = 0o021 print(val5)
Output
25 11.89 (6+2.9j) 301 17
Boolean value
The Chinese translation ofExample
is:Example
The Boolean type has two values, True and False. True represents 1, False represents 0. Let's see an example -
a = (1 == True) b = (1 == False) print(a) print(b)
Output
True False
Text Sequence Types – string
is:Text Sequence Types – String
We can easily create a string by enclosing the characters in quotes. Python treats single quotes as synonyms for double quotes. Creating a string is as simple as assigning a value to a variable.
Let’s see how to easily create a String in Python −
myStr = Thisisit!'
Example
is:Example
We will now see an example of creating single and multi-line strings −
str1 = "John" print(str1) # Multi-line string str2 = """ This, is it! """ print(str2)
Output
John This, is it!
List
A list contains items separated by commas and enclosed within square brackets ([]). Creating a list is as simple as putting different comma-separated values between square brackets. A list can have integer, string or float elements. With that , we can also create a List with mixed data types.
The list can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type
Create a Python List with Integer elements
We will create a list with 10 integer elements and display it. The elements are enclosed by square brackets. With that, we have also displayed the length of the list and how we can access specific elements using the square brackets −
The Chinese translation ofExample
is:Example
# Create a list with integer elements mylist = [25, 40, 55, 60, 75, 90, 105, 130, 155, 180]; # Display the list print("List = ",mylist) # Display the length of the list print("Length of the List = ",len(mylist)) # Fetch 1st element print("1st element = ",mylist[0]) # Fetch last element print("Last element = ",mylist[-1])
Output
List = [25, 40, 55, 60, 75, 90, 105, 130, 155, 180] Length of the List = 10 1st element = 25 Last element = 180
Create a Python List with String elements
We can also add string elements to Python lists. We will create a list of 5 string elements and display it. Elements are enclosed in square brackets. By doing this we also show the length of the list and how to access the first and last element using square brackets −
The Chinese translation ofExample
is:Example
# Create a list with string elements mylist = ["BMW","Audi","Tesla","Honda","Toyota"]; # Display the list print("List = ",mylist) # Display the length of the list print("Length of the List = ",len(mylist)) # Fetch 1st element print("1st element = ",mylist[0]) # Fetch last element print("Last element = ",mylist[-1])
Output
List = ['BMW', 'Audi', 'Tesla', 'Honda', 'Toyota'] Length of the List = 5 1st element = BMW Last element = Toyota
Tuple
is translated into Chinese as:Tuple
A tuple is a series of immutable Python objects. Tuples and lists are sequences. The main difference between tuples and lists is that tuples are immutable while lists are mutable. Tuples use parentheses, while lists use square brackets.
Let us first create a basic Tuple with integer elements and then move towards Tuples within a Tuple −
The Chinese translation ofExample
is:Example
# Creating a Tuple mytuple = (20, 40, 60, 80, 100) # Displaying the Tuple print("Tuple = ",mytuple) # Length of the Tuple print("Tuple Length= ",len(mytuple))
Output
Tuple = (20, 40, 60, 80, 100) Tuple Length= 5
dictionary
Python's dictionary is a hash table type. They work like associative arrays or hashes in Perl, consisting of key-value pairs. The correct syntax for creating a Python dictionary is to store values in the form of key:value pairs. The left side of the colon stores the key, and the right side stores the value, that is,
key:value
Dictionary is enclosed by curly bracket and do not allow duplicates. According to the 3.7 Python update, dictionaries are now ordered. Consider Dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary). Each key in a Dictionary is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.
We will create 4 key-value pairs, with keys Product, Model, Units and Available and values Mobile, XUT, 120 and Yes. Keys are on the left of colon, whereas values are on the right −
Example
的中文翻译为:示例
# Creating a Dictionary with 4 key-value pairs myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Displaying the Dictionary print("Dictionary = \n",myprod)
输出
Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
The above is the detailed content of What are the common built-in data types in Python?. 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

This tutorial demonstrates how to use Python to process the statistical concept of Zipf's law and demonstrates the efficiency of Python's reading and sorting large text files when processing the law. You may be wondering what the term Zipf distribution means. To understand this term, we first need to define Zipf's law. Don't worry, I'll try to simplify the instructions. Zipf's Law Zipf's law simply means: in a large natural language corpus, the most frequently occurring words appear about twice as frequently as the second frequent words, three times as the third frequent words, four times as the fourth frequent words, and so on. Let's look at an example. If you look at the Brown corpus in American English, you will notice that the most frequent word is "th

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Dealing with noisy images is a common problem, especially with mobile phone or low-resolution camera photos. This tutorial explores image filtering techniques in Python using OpenCV to tackle this issue. Image Filtering: A Powerful Tool Image filter

PDF files are popular for their cross-platform compatibility, with content and layout consistent across operating systems, reading devices and software. However, unlike Python processing plain text files, PDF files are binary files with more complex structures and contain elements such as fonts, colors, and images. Fortunately, it is not difficult to process PDF files with Python's external modules. This article will use the PyPDF2 module to demonstrate how to open a PDF file, print a page, and extract text. For the creation and editing of PDF files, please refer to another tutorial from me. Preparation The core lies in using external module PyPDF2. First, install it using pip: pip is P

This tutorial demonstrates how to leverage Redis caching to boost the performance of Python applications, specifically within a Django framework. We'll cover Redis installation, Django configuration, and performance comparisons to highlight the bene

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

This tutorial demonstrates creating a custom pipeline data structure in Python 3, leveraging classes and operator overloading for enhanced functionality. The pipeline's flexibility lies in its ability to apply a series of functions to a data set, ge

Python, a favorite for data science and processing, offers a rich ecosystem for high-performance computing. However, parallel programming in Python presents unique challenges. This tutorial explores these challenges, focusing on the Global Interprete
