#!/usr/bin/env python
#-*-Coding:utf-8-*-
# Numeric operations
Print (5 + 4) # add
Print (4.3-2) # Subtraction
Print (3 * 7) # multiplication
Print (2/4) # Division, wait until a floating-point number
Print (2//4) # Division, get an integer
Print (17 3) # take remainder
Print (2 * * 5) # exponentiation
Print ("========================================")
# string
str = ' Aleen '
Print (str) # output string
Print (str[0:-1]) # outputs all characters from the first to the penultimate
Print (str[0]) # The first character of the output string
Print (Str[2:5]) # Output characters from the third start to the fifth
Print (str[2:]) # outputs all characters from the beginning of the third
Print (str * 2) # output String two times
Print (str + "TEST") # connection string
Print ("========================================")
# list (lists)
list = [' ABCD ', 786, 2.23, ' Runoob ', 70.2]
Tinylist = [123, ' Runoob ']
Print (list) # output complete list
Print (list[0]) # The first element of the output list
Print (List[1:3]) # Output from the second start to the third element
Print (list[2:]) # Outputs all elements starting from the third element
Print (Tinylist * 2) # output two-time list
Print (list + tinylist) # connection list
A = [1, 2, 3, 4, 5, 6]
A[0] = 9
A[2:5] = [13, 14, 15]
Print (a)
A[2:5] = [] # sets the corresponding element value to []
Print (a)
Print ("========================================")
# tuple tuples
Tup1 = (1,2,3,4)
tup2 = (' asd ', ' zxc ')
Tup3 = () # Empty tuple
Tup4 = (20,) # Zanzuri only one element needs to be appended to the value, comma
Print (tup1) # output Full tuple
Print (tup1[0]) # output tup1 The first point in the tuple
Print (Tup1[1:3]) # Output starts from the second element to the third element
Print (tup1[2:]) # Outputs all elements starting from the third element
Print (TUP2 * 2) # output two tuples
Print (Tup1 + tup2) # connection tuple
For x in (A-Z):
Print (x)
L = [' ssss ', ' sddd ', ' AAAAA ']
Print (l[2])
# Reward table converted to Ganso
tuple = tuple (l)
Print (tuple)
# dictionary d = {key1:value1,key2 = value2}
Dict1 = {' aaa ': ' 123 ', ' BBB ': ' 456 ', ' CCC ': ' 789 '}
# A value in the print dictionary that requires a named variable plus the printed value is written in the [']
Print ("dict1[' aaa ']:", dict1[' AAA ')
Print ("dict1[' BBB ']:", dict1[' BBB '))
Print (dict1[' AAA ')
# Modify Dictionary
dict1[' bbb '] = ' 666 '
Print (dict1[' BBB ')
# Create a dictionary from Dict
s = dict (Dict1)
Print (s)
# Formatting strings for dictionaries
D = {' A ': 1, ' B ': 3, ' C ': 9, ' d ': 18} # The integer value in the dictionary can be added without quotation marks, the characters are added
Print (d)
# print (' This is% (3) s. '%d)
# clear function: Clears all items in the dictionary
s = {' name1 ': ' Aleen ', ' name2 ': ' Bob ', ' name3 ': ' CiCi '}
S3 = {' Name4 ', ' name5 ', ' Name6 '}
# Print (S.clear ())
S1 = s.copy ()
Print (S1)
# Fromkeys (Specify a list, use the value in the list as the dictionary key, generate a dictionary)
Print (Dict.fromkeys (s3,26))
# get (Specify key to get the corresponding value)
Print (S.get (' Name3 '))
# items (returns a list of elements that are composed of "key-value pairs")
Print (S.items ())
# keys (get all the keys in the dictionary)
Print (S.keys ())
# pop (Gets the value of the specified key and is removed in the dictionary)
SA = s.pop (' name2 ')
Print (sa,s)
# Popitem (Randomly get a key-value pair and delete it in the dictionary)
SD = S.popitem ()
Print (S,SD)
# SetDefault (Gets the value of the specified key, if key does not exist, it is created)
SF = S.setdefault (' Name6 ')
Print (sf,s)
# Update (add key-value pairs to dictionary)
S.update ({' Name4 ': ' Dog '})
Print (s)
# dictionary built-in functions & methods
s = {' name1 ': ' Aleen ', ' name2 ': ' Bob ', ' name3 ': ' CiCi '}
#len () computes the number of dictionary elements, that is, the total number of keys.
Print (len (s))
# str (dict) output dictionary, expressed as a printable string.
Print (str (s))
The # type (variable) returns the type of the input variable, and returns the dictionary type if the variable is a dictionary.
Print (type (s))
python3.6 Basic Data-list-Ganso-string-dictionary 02