Learning Instance Code
#求1到100的和
Print ("################# #1到100求和 #################")
def sum (A, B):
s = 0
For I in Range (a,b+1):
s = s+i
return s
if __name__ = = ' __main__ ':
sum1 = SUM (0,100)
Print (SUM1)
Print (' ####################### #求1-500 all odd and ########################### ')
Def sumAn (A, B):
m = 0
For I in Range (a,b+1):
If i%2==0:
m = m + I
Continue
Else
Continue
return m
if __name__ = = ' __main__ ':
A= SumAn (1,500)
Print (a)
Print (' ################ #求1 +2!+3!... 20! and ################ ')
Def SUMFAC (A, B):
sum2 = 0
K = 1
For I in Range (a,b+1):
#print (I**i)
K = K*i
sum2 = sum2 + K
Return sum2
if __name__ = = ' __main__ ':
FAC = SUMFAC (1,20)
Print (FAC)
Print (' ###################### #列表排序 ################### ')
def sort (a):
b = Sorted (a)
Return b
if __name__ = = ' __main__ ':
List1 = [2,32,43,453,54,6,576,5,7,6,8,78,7,89]
List1_sort = sort (list1)
Print (List1_sort)
Numeric, String, list. Yuan Zu, dictionary.
Numeric types are divided into types
Integer (int) long integer (long) floating-point (float) complex (complex) a=12j value followed by J
Type () View variable types
String type
There are three ways of defining
’’
“”
‘’’’’’’
Sanchong quotation marks (docstring) can also be used as annotations in addition to defining strings.
Str= ' abc '
Index (starting at 0, 1 = last character, + string connector), slice
Str[0] =a Str[0:2] (2 means take to the first) Str[0::2] means that the two take bits
Str[0:3:-1] represents right-to-left.
strings, lists, and tuples are sequences.
Features: indexing and slicing operations
Basic operation of the sequence
Len (): Find the length of the sequence
+: Connect 2 sequences
*: Repeating sequence elements
In: Whether the discharge element is in the order
Max (): Returns the maximum value
MIN (): Returns the minimum value
CMP (x, y): Compares the sequence of columns for equality
list []
The > list is a data structure that processes an ordered set of items, which can store a sequence of items in a list.
> variable element structure
Create a list
List1=[]
List2=list ()
List3=[' A ',
List2.append () Adding elements to the list
List operations
Take value
Slices and indexes
Add to
List2.append ()
Delete
Del listp[]
List.remove (list[])
Modify
List[]=x
Find
var int list
Insert
List2.insert (1, ' abc ')
Sort
List2.sort ()
Reverse
List2.reverse ()
Delete and return a value
List.pop (1)
Append element
List2.extend (' Qweq ')
Tuple ()
Tuples and lists are similar
Tuples and strings are not becoming
-tuples can store a range of values
-tuples are typically used in user-defined functions to safely take a set of worthwhile times, that is, the value of the tuple being used does not change.
T= (1,a,)
Split of a B c=t tuple
Common methods of dictionaries
> Dictionary is the only type of mapping (hash table) in Python
The > Dictionary object is mutable, but the dictionary must be built using immutable objects, and a dictionary can use different types of key values.
Method of the Dictionary:
Key () value
VALUES () Element
Items () return list
Dic.get () returns the dictionary element.
Dic.has_key (2) Determine if key is in the dictionary
Dic.copy () copy dic2=dic.copy () paste
Dic.clear Clear Elements
Dic.pop (' a ') deletes the K value and returns the element.
Dict () turn a list into a dictionary
Dic.fromkeys (' abc ', 100) Create the same dictionary as the element
day9-Review Learning Python examples