Data Types in Python

Data Types in Python

When discussing data types in Python, we must start with the standard data types. The standard data types in Python are as follows:

Data Types in Python

Numeric Types:
Numeric data types are used to store numerical values.
They are immutable data types, meaning that changing a numeric data type will allocate a new object.
In Python 2.X, there are more data types, including int (signed integer), long (long integer [which can also represent octal and hexadecimal]), float (floating-point), and complex (complex numbers).
In Python 3.X, the long type has been removed and replaced with int, with the most commonly used types being int or float.
Strings:
Strings are represented by the string type and consist of a sequence of characters that can include numbers, letters, and underscores.
In Python, strings are enclosed in quotes, which can be: single quotes (‘ ‘), double quotes (” “), or triple quotes (”’ ”’), and there are two indexing methods:
Left to right indexing starts at 0, with the maximum value being the length of the string minus 1.
Right to left indexing starts at -1, with the maximum value at the beginning of the string.

Data Types in Python

Indexing is represented with [ ], for example, if we define str=RUNOOB, to get the N value, the expression would be: str[2] or str[-4].
For string operations, there are the following methods:
str = Hello World!
print(str ) # Outputs the complete string
print(str[0]) # Outputs the first character in the string
print(str[2:5]) # Outputs the substring from the third to the sixth character
print(str[2:]) # Outputs the substring starting from the third character
print(str*2) # Outputs the string twice
print(str+TEST) # Outputs the concatenated string
Lists:
Lists are represented by [ ], and are one of the most frequently used data types in Python, as well as the most general collection data type.
In addition to indexing, slicing operations can also be performed:

Data Types in Python

For example:
list = [ runoob, 786 , 2.23, john, 70.2 ]
tinylist = [123, john]
print(list) # Outputs the complete list
print(list[0]) # Outputs the first element of the list
print(list[1:3]) # Outputs the second to third elements
print(list[2:]) # Outputs all elements from the third to the end of the list
print(tinylist*2) # Outputs the list twice
print(list+tinylist) # Prints the combined list
Tuples:
Tuples are similar to lists, but unlike lists, tuples are represented by ( ) and their elements are separated by commas. Tuples cannot be modified, only read. To delete, the entire tuple must be deleted, and operations are not as flexible as lists.
For example:
tuple = ( runoob, 786 , 2.23, john, 70.2 )
tinytuple = (123, john)
print(tuple) # Outputs the complete tuple
print(tuple[0]) # Outputs the first element of the tuple
print(tuple[1:3]) # Outputs the second to fourth (not including) elements
print(tuple[2:]) # Outputs all elements from the third to the end of the tuple
print(tinytuple*2) # Outputs the tuple twice
print(tuple+tinytuple) # Prints the combined tuple
Dictionaries:
Dictionaries are the most flexible data type in Python, aside from lists. The difference between dictionaries and lists is that elements in a dictionary are accessed by keys, not by offsets. Dictionaries are represented by { }, with each element consisting of a key and a value. Keys cannot be duplicated, while values can be duplicated.
For example:
dict = {}
dict[one] = This is one
dict[2] = This is two
tinydict = {name: john,code:6734, dept: sales}
print(dict[one]) # Outputs the value with key ‘one’
print(dict[2]) # Outputs the value with key 2
print(tinydict) # Outputs the complete dictionary
print(tinydict.keys()) # Outputs all keys
print(tinydict.values()) # Outputs all values
This concludes the introduction to the standard data types in Python.
Additionally, here are some differences between Python 2.X and Python 3.X:
1. Print Function:
The print function in Python 2 becomes the print() function in Python 3, with an added parentheses.
2. Unicode:
In Python 2, there is an ASCII str() type, while unicode() is separate and not a byte type.
In Python 3, there are Unicode (utf-8) strings, and utf-8 encoding is used by default in Python 3.x.
Thus, in Python 3, the following syntax is also correct:
中国=”China”print(中国)
The output is: China
3. Division Operation:
Here are some examples:
In Python 2.X:
>>> 1 / 2
0
>>> 1.0 / 2.0
0.5
In Python 3.X:
>>> 1/ 2
0.5
These are a few common differences between Python 2 and Python 3; there are certainly other differences, but they will not be discussed here.

Data Types in PythonExclusive for fansData Types in Python

I have organized resources worth over 2000+

100G of resources

Data Types in Python

The content includes:

Planning a software testing learning path from 0 to 1

Common testing templates and strategies in the workplace

Software testing improvement e-books

Classic interview questions

Recorded courses from Songqin

Limited time free~~~

Data Types in PythonLong press the image belowData Types in Python

Add Teacher Tang from Songqin to get free 100G resources

Data Types in Python

Currently, over 100,000 people have followed and joined us

Data Types in Python Data Types in Python Data Types in Python Data Types in Python Data Types in Python Data Types in Python Data Types in Python Data Types in Python

Data Types in Python Data Types in Python Data Types in Python Data Types in Python Data Types in Python Data Types in Python Data Types in Python Data Types in Python

Long press the QR code

Follow the 【Songqin Online Course】 video account

Data Types in Python

Data Types in Python

Data Types in Python

Data Types in Python

Data Types in Python

Leave a Comment