Python basic data types

How the python file works

Go to the directory where the python file is located from the shell that comes with python, and then run python xxx.py (such as C:\work>python hello.py)

  1. IDE such as pythoncharm

  2. Editors with plugins such as Sublime Text

type of data

Python's data types are divided into variable types and immutable types

Python basic data types

Where the variable type is

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 different types of objects by assignment.

3、 Numerical division (/) always returns a floating point number. To get an integer, use the // operator.

4、 In mixed calculations, Python will convert integers to floating point numbers.

5、 Power a**b

6、 Plural a+bj or complex(a,b)

String:

List

Dictionary

Sets

A set is a sequence of unordered and non-repeating elements. The basic function is to test membership and delete duplicate elements. You can use braces {} or set()

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

#! /usr/bin/python3

student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}

print(student) # output collection, duplicate elements are automatically removed

Membership test

if('Rose' in student) :

print('Rose in the collection')

else :

print('Rose is not in the collection')

set can perform set operations

a = set('abracadabra')

b = set('alacazam')

print(a)

print(a-b) # Difference of a and b

print(a | b) # Union of a and b

print(a & b) # the intersection of a and b

print(a ^ b) # Elements that do not exist at the same time in a and b

The immutable type is

Tuple (tuple)

List content

Constructing a tuple containing 0 or 1 element is special

tup1 = () # empty tuple

tup2 = (20,) # An element, you need to add a comma after the element

== 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'])

== Explanation of python variables ==

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

a

[1]

a=b=[]

a

[]

b

[]

b.append(0)

b

[0]

a

[0]

For immutable types, the 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)

Python data type conversion

Function description int(x [,base]) Convert x to an integer float(x) Convert x to a floating point number complex(real [,imag]) Create a complex number str(x) Convert object x to string repr (x) Convert the object x into an expression string eval(str) is used to calculate the valid Python expression in the string, and returns an object tuple(s) Convert the sequence s into a tuple list(s) will The sequence s is converted to a list set(s) is converted to a variable set dict(d) to create a dictionary. d must be a sequence (key, value) tuple. frozenset(s) converted to immutable set chr(x) converted an integer to a character ord(x) converted a character to its integer value hex(x) converted an integer to a hexadecimal string oct (x) Convert an integer to an octal string

Recommended Posts

Python basic data types
02. Python data types
Python basic syntax and number types
Python basic syntax (1)
Python learning-variable types
Python data model
Python basic summary
python data structure
Python basic operators
Python data format-CSV
Python basic syntax generator
Python basic drawing tutorial (1)
Python data analysis-data update
Python function basic learning
Python data analysis-apply function
Python data analysis-data selection
Basic syntax of Python
Basic knowledge of Python (1)
Python data analysis-data establishment
Python basic syntax iteration
Python basic knowledge question bank
Python Data Science: Neural Networks
Python common data structure collation
Python3 crawler data cleaning analysis
Python parses simple XML data
Python basic syntax list production
Python Data Science: Logistic Regression
Python data structure and algorithm
Python basic drawing tutorial (two)
Python Data Science: Regularization Methods
Python Data Science: Related Analysis
Python Data Science: Linear Regression
Python Faker data forgery module
Python Data Science: Chi-Square Test
Python introductory notes [basic grammar (on)]
Python Data Science: Linear Regression Diagnosis
Detailed explanation of python sequence types
Python entry notes [basic grammar (below)]
Python file operation basic process analysis
Python crawler basic knowledge points finishing
Real zero basic Python development web
Python realizes online microblog data visualization
Is python suitable for data mining
Automatically generate data analysis report with Python
Python access to npy format data examples
Python basic actual combat-guess the age game
Comprehensive summary of Python built-in exception types
Can variable types be declared in python
Java or Python for big data analysis
Python uses pandas to process Excel data