Detailed explanation of python's basic data types

零到壹度
Release: 2018-03-31 17:48:10
Original
1664 people have browsed it

This article mainly shares with you a detailed explanation of the basic data types of Python. Friends who need it can take a look at

This article This article mainly shares with you a detailed explanation of the basic data types of python. Friends who need it can take a look.

How to run python files

  1. Python’s own shell

  2. Go to the directory where the python file is located, Then run
    python xxx.py (such as C:\work>python hello.py)

3.pythoncharm and other IDE

4.sublime Text etc. Plug-in editor

Data type

Python data types are divided into variable types and immutable types

Python basic data types

The variable type is

Number (number):

Including int, float, bool, complex (plural).

Note:

1. Python can assign values ​​to multiple variables at the same time, such as a, b = 1, 2.

2. A variable can point to objects of different types through assignment.

3. The division of numerical values ​​(/) always returns a floating point number. To obtain an integer, use the // operator.

4. During mixed calculation, Python will convert integers into floating point numbers.

5. Power a**b

6. Complex number a+bj or complex(a,b)

String (string):

List (list)

Dictionary (dictionary)

Sets (set)

A set (set) is an unordered sequence of non-repeating elements.
The basic function is to perform membership testing and remove duplicate elements.
You can use curly brackets { } or the set()

function to create a set. Note: To create an empty set, you must use set() instead of { }, because { } is used to create an empty dictionary.

#!/usr/bin/python3student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}

print(student)   # 输出集合,重复的元素被自动去掉# 成员测试if('Rose' in student) :
    print('Rose 在集合中')else :
    print('Rose 不在集合中')# set可以进行集合运算a = set('abracadabra')
b = set('alacazam')

print(a)

print(a - b)     # a和b的差集print(a | b)     # a和b的并集print(a & b)     # a和b的交集print(a ^ b)     # a和b中不同时存在的元素
Copy after login

The immutable type is

Tuple (tuple)

  1. List content

Constructing a tuple containing 0 or 1 elements is special

tup1 = ()    # 空元组tup2 = (20,) # 一个元素,需要在元素后添加逗号
Copy after login

==Tuples can also be spliced ​​using the + operator. ==

The so-called "unchanged" of tuple means that each element of tuple points to the same forever
>>> t = ('a', 'b', ['A', 'B'])
>>> t[2][0] = 'X'
>>> t[2][1] = 'Y'
>>> t
('a', 'b', ['X', 'Y'])
Copy after login

==python Description of variables==

The declaration of a python variable is a reference to an object. For a variable type, if its copy changes, it itself will also change

>>> a
[1]>>> a=b=[]>>> a
[]>>> b
[]>>> b.append(0)>>> b
[0]>>> a
[0]>>>
Copy after login

For immutable types Type, its variable value will not be affected by the copy

>>> a=b=(1,2,3)>>> a
(1, 2, 3)>>> b
(1, 2, 3)>>> b+(4,)
(1, 2, 3, 4)>>> b
(1, 2, 3)>>> b=b+(4,)>>> b
(1, 2, 3, 4)>>> a
(1, 2, 3)
Copy after login

Python data type conversion

##tuple(s)Convert sequence s into a tuplelist(s)Convert sequence s into a listset(s)Convert to a variable setdict(d)Create a dictionary. d must be a sequence of (key, value) tuples. frozenset(s)Convert to immutable collectionchr(x)Convert an integer to a characterord( x)Convert a character to its integer valuehex(x)Convert an integer to a hexadecimal stringoct(x)Convert an integer to an octal stringRelated recommendations:
Function Description

int(x [,base])

Convert x to an integer

float(x)

Convert x to a floating point number

complex(real [ ,imag])

Create a plural number

##str(x)

Convert object x to string

repr(x)

Convert object x to expression The formula string

eval(str)

is used to evaluate the valid Python expression in the string, And return an object


Python’s basic data types

Python common data types and common operations

Detailed introduction to Python basic data types

The above is the detailed content of Detailed explanation of python's basic data types. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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