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

王林
Release: 2023-08-20 22:57:13
forward
1639 people have browsed it

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!

Related labels:
source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template