1. Data type
1.1. String
1.1.1. Variable declarations
1.1.2. Related functions
1.2. Boolean type
1.2.1. Variable declarations
1.2.2. Related functions
1.3. Number types
1.3.1. Variable declarations
1.3.2. Related functions
1.4. Lists (list)
1.4.1. Creating a list
1.4.2. Getting elements and slices
1.4.3. adding/replacing elements
1.4.4. Removing elements
1.4.5. Other methods
1.5. Tuples (tuple)
1.5.1. Creating tuples
1.5.2. Accessing elements
1.5.3. Related methods
1.6. Set (set)
1.6.1. Creating a Collection
1.7. Dictionary (Dictionary)
1.7.1. Creating a Dictionary
1.7.2. Accessing dictionary elements
1.7.3. Deleting elements
1.7.4. Other methods
1. Data type
Variables in Python do not need to be declared, they are assigned directly at the time of use, and the data type of the variable is determined by the type of the value it is assigned to.
1.1. String
1.1.1. Variable declarations
There are three ways to declare a string in Python, using single and double quotation marks to declare a single-line string, and three single quotes to declare multiple lines of string.
Use single-quote declarations:
' This is a string '
Use double-quote declarations:
" This is a string "
Use three single-quote declarations:
" " This is a multi-linesstrings " "
1.1.2. Related functions
To convert a variable to a string type
Strvar = str (x)
1.2. Boolean type
1.2.1. Variable declarations
Boolvar == True
1.2.2. Related functions
To convert a variable to a Boolean type
Boolvar = bool (x)
1.3. Number types
1.3.1. Variable declarations
The number types in Python are categorized as integer and floating-point.
Intvar = Ten= 2.5
1.3.2. Related functions
To convert a variable to an integral type:
Intvar = Int (x)
To convert a variable to a floating-point type:
Floatvar = float (x)
Returns the absolute value:
Value = ABS (x)
To convert floating-point numbers into integers, note that this function is under the math package, so import the math package first:
Import= math.ceil (4.3)# result:5
Rounding the fractional part returns the integer part of the floating-point number as an integer:
Import= Math.floor (4.9)# result:4
Returns the X-power of the constant e:
Import mathmath.exp (0) # 1.0Math.exp (1) # 2.718281828459045math.exp (2) # 7.38905609893065
To calculate a logarithmic function:
Import Mathmath.log (1) # 0Math.log (MATH.E) # 1math.log (Ten) # 0.5 Math.log (Ten) # 2
Returns the maximum/minimum value in a given parameter, which can receive a sequence:
Max (1, 8, all, ten) # min (1, 8, max) # 1
Calculate the exponential function:
Pow (2,2) # 4pow (2,3) # 8
Calculate Open Square:
sqrt (4) # 2
Sum:
SUM ([i]) # 6sum ([1) # 7# Receives up to two parameters, the first parameter is an iterative object
1.4. Lists (list)
The list in Python is used to organize the values of a series of elements by placing them in a pair of brackets, using a comma interval between the elements. The elements in the list can be of different types, but in most cases they are the same type.
1.4.1. Creating a list
list = [1, 4, 9,1'b']
1.4.2. Getting elements and slices
The elements in the list can be obtained using subscripts, with the subscript starting from 0
list = [1, 4, 9,]list[0] # 1list[3] #
You can get a subset of the elements from the list in the form of a slice, forming a new list:
list = [1, 4, 9,]list[0:3] # [1, 4, 9]list[-3:-1] # [9, +]li St[-3:] # [9, +]list[:] # [1, 4, 9, 16, 25] Return a new copy
1.4.3. adding/replacing elements
list = [1, 4, 9, 16, 25]#add a single element at the endList.append (6)Print(list)#[1, 4, 9, +, 6]List.append ([1, 2, 3])Print(list)#[1, 4, 9, +, 6, [1, 2, 3]]#add multiple elements at the endList.extend ([0, 1, 2])Print(list)#[1, 4, 9, 6, 0, 1, 2]#Insert an element in a locationList.insert (0, 7)Print(list)#[7, 1, 4, 9, 6, 0, 1]#Replace an element in a listList[2:4] = [0, 1, 0]Print(list)#[1, 4, 0, 1, 0, a, 6]
1.4.4. Removing elements
list = [1, 4, 9, +]# removes the last element and returns the element list.pop ()print(list) # [1, 4, 9, +]
1.4.5. Other methods
list = [1, 4, 9, +]# length len (list) # 5# count the number of occurrences of an element /c7>list.count (4) # 1# Reverse list list.reverse () # [ +, 9, 4, 1]# sort list.sort () # [1, 4, 9,]
1.5. Tuples (tuple)
Tuples are very similar to lists, unlike elements in tuples that cannot be modified. The list uses brackets, and tuples use parentheses.
1.5.1. Creating tuples
tuple = ("This" is"a" tuple', 666)
1.5.2. Accessing elements
The access to the elements in the tuple is basically the same as the list.
1.5.3. Related methods
seq = [1, 2, 5, 6,]print(seq) # [1, 2, 5, 6,]tuple (seq)print
(seq) # (1, 2, 5, 6,)
1.6. Set (set)
Python also contains a collection of data structures that can be used to hold non-repeating elements.
1.6.1. Creating a Collection
Fruitset = {'apple'Orange'pear' 'apple'}print(fruitset) # {' Apple ', ' orange ', ' pear '}
1.7. Dictionary (Dictionary)
The difference between a dictionary and a list is that the dictionary uses key as the index of the element, and the list uses the subscript as the index of the element.
1.7.1. Creating a Dictionary
DIC = {'key1'value1'key2' 'key3': [1, 2, 3]}
1.7.2. Accessing dictionary elements
dic['key1'] # ' value1 'dic['key1'] # 2dic['key1' # [1, 2, 3]
1.7.3. Deleting elements
del dic['key2'print(DIC) # {' Key1 ': ' Value1 ', ' Key3 ': [1, 2, 3]}
1.7.4. Other methods
dic.clear () removes all the elements in the dictionary dic.copy () returns a dictionary of shallow copy Dic.fromkeys () creates a new dictionary with the keys of the elements in the sequence seq, Val is the initial value corresponding to all keys of the dictionary dic.get (key, Default=None) Returns the value of the specified key if the value does not return the default value in the dictionary Dic.has_key (key) If the key returns true in the dictionary dict, otherwise returns Falsedic.items () Returns a Dic.keys (key, value) tuple array as a list () returns a dictionary with a list of all keys Dic.setdefault (key, default=None) and get () similar, but if the key does not already exist in the dictionary, The key is added and the value is set to Defaultdic.update (DICT2) to update the key/value pairs of the dictionary dict2 to Dict dic.values () to return all values in the dictionary as a list
2. Process Control
2.1.IF statements
x = 1if x > 0: print('A')elif x = = 0: print('B')else: Print('C')
2.2.FOR statements
Traverse a list
Fruits = ['apple'Orange'pear ' ] for in fruits: print(f)
Traverse a sequence of numbers
forIinchRange (5): Print(i)#results:0 1 2 3 4 forIinchRange (5, 10): Print(i)#Results:5 6 7 8 9 forIinchRange (0, 10, 2): Print(i)#results:0 2 4 6 8Fruits= ['Apple','Orange','Pear'] forIinchrange (len (fruits)):Print(Fruits[i])#results:apple Orange Pear
2.3. Functions
def afun (n): " " This is a function :p Aram N: : return: "' for" range (N): Print (i) Afun (10)
python--Language Basics