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)
IDE such as pythoncharm
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
if('Rose' in student) :
print('Rose in the collection')
else :
print('Rose is not in the collection')
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