Table of Contents
Numeric type
Example
Output
Boolean value
Text Sequence Types – string
Text Sequence Types – String
List
Create a Python List with Integer elements
Create a Python List with String elements
Tuple
dictionary
示例
输出
Home Backend Development Python Tutorial What are the common built-in data types in Python?

What are the common built-in data types in Python?

Aug 20, 2023 pm 10:57 PM
list string dictionary

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.

The Chinese translation of

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)
Copy after login

Output

25
11.89
(6+2.9j)
301
17
Copy after login

Boolean value

The Chinese translation of

Example

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)
Copy after login

Output

True
False
Copy after login
The Chinese translation of

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!'
Copy after login
The Chinese translation of

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)
Copy after login

Output

John
This,
is it!
Copy after login

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 of

Example

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])
Copy after login

Output

List =  [25, 40, 55, 60, 75, 90, 105, 130, 155, 180]
Length of the List =  10
1st element =  25
Last element =  180
Copy after login

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 of

Example

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])
Copy after login

Output

List =  ['BMW', 'Audi', 'Tesla', 'Honda', 'Toyota']
Length of the List =  5
1st element =  BMW
Last element =  Toyota
Copy after login

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 of

Example

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))
Copy after login

Output

Tuple =  (20, 40, 60, 80, 100)
Tuple Length=  5
Copy after login

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
Copy after login

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)
Copy after login

输出

Dictionary = 
 {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Copy after login

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!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to Use Python to Find the Zipf Distribution of a Text File How to Use Python to Find the Zipf Distribution of a Text File Mar 05, 2025 am 09:58 AM

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

How Do I Use Beautiful Soup to Parse HTML? How Do I Use Beautiful Soup to Parse HTML? Mar 10, 2025 pm 06:54 PM

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

Image Filtering in Python Image Filtering in Python Mar 03, 2025 am 09:44 AM

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

How to Work With PDF Documents Using Python How to Work With PDF Documents Using Python Mar 02, 2025 am 09:54 AM

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

How to Cache Using Redis in Django Applications How to Cache Using Redis in Django Applications Mar 02, 2025 am 10:10 AM

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

How to Perform Deep Learning with TensorFlow or PyTorch? How to Perform Deep Learning with TensorFlow or PyTorch? Mar 10, 2025 pm 06:52 PM

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

How to Implement Your Own Data Structure in Python How to Implement Your Own Data Structure in Python Mar 03, 2025 am 09:28 AM

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

Introduction to Parallel and Concurrent Programming in Python Introduction to Parallel and Concurrent Programming in Python Mar 03, 2025 am 10:32 AM

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

See all articles