Python dictionary functions global variable code resolution

  • 2020-06-15 09:45:26
  • OfStack

The dictionary


dict1 = {'name':'han','age':18,'class':'first'}
print(dict1.keys())    # Print all key value 
print(dict1.values())   # Print all values value 
print("dict1['name']:",dict1['name'])   # print name The corresponding value value 
print(dict1.get('name'))  # Lexicographical get method name The corresponding value value 

dict1['age']=28  # A dictionary change is equivalent to a new assignment!! 

dict1['address']='beijing'  # The increase in dictionaries is: dict[key] = value  In this form 

del dict1['name']  # Delete from the dictionary 1 An element 
dict1.clear()    # Clear the dictionary and return 1 An empty dictionary 
# del dict1     # Delete the dictionary, and the dictionary is completely deleted 

Note for dictionary usage:

1, the key is not allowed to be the same, if the same, the latter will overwrite the former

2. Keys are immutable, so they can only be used as Numbers, strings and tuples


dict2 = {(1,2):5," tuples ":(4,5)}  # Dictionary elements can only use immutable data types!! 
print(dict2)
print(dict2[' tuples '])
print(dict2[(1,2)])

for i in dict2.keys():      # The value out is cleaner!! Remember what your teacher told you 
  print(" In the dictionary key Value is: ",i)
for j in dict2.values():
  print(" In the dictionary values Value is: ",j)

function

1. Definition of functions

A function is a set of statement blocks encapsulated to implement a specific function and can be called by the user

2. Classification of functions

Custom functions; Predefined functions (come with the system, come with lib)

3. Benefits of using functions

Reduces the difficulty of programming, breaks down big problems into small ones, and can be called multiple times

4. Function syntax

define

def function name (argument):

The body of the function

The return statement # return without an expression is equivalent to returning none

call

The function name

Here are some of the functions:


 # Define a function with the best name _ segmentation 
 def print_str(str):
   print(str)
   return
 #  Call a function 
 print_str(" call 1 Under the ")

#  You pass 
# All parameters are in python Are passed by reference 
#1 Sentence: want to change all change!! 
def charge_me(mylist):
  mylist.append([1,2,3,4])
  print(" Function value: ",mylist)
  return
mylist = [10,20,30]
charge_me(mylist)
print(" Value outside the function: ",mylist)   # Out-of-function and in-function printing is the same!! 

# An assignment reference to a function 
def print_info(name,age=3):
  print("name",name)
  print("age",age)
  return
# print_info(name="xiao",age=18)
print_info(age=50,name="xiao")    #python Upside down is ok!! 
print_info(name='haha')

# The variable length argument of a function 
def p_info(arg1,*vartuple):
  print(" Output: ",arg1)
  for var in vartuple:
    print(var)
  return
p_info(10)
p_info(70,60,50,40,30)

 Anonymous functions lambda , just understand 
 # 1 , lambda just 1 An expression instead of 1 Block of code, function body ratio def It's a lot easier. Only in the lambda Expressions encapsulate limited logic 
 # 2 , lambda[arg1[,arg2,...argn]]:expression
 sum1 = lambda arg1,arg2:arg1+arg2
 print(" The added value is: ",sum1(10,20))

# return statements 
def sum2(arg1,arg2):
  total = arg1+arg2
  print(" Function: ",total)
  return total           # the total I'll go back after I get rid of it none
abc = sum2(10,40)
print(" Functions: ",abc)

Global and local variables
Global variables are more likely to cause problems. Use them if you can


total = 0
def sum3(a,b):
  total = a+b
  print(" Within the function ( A local variable ) The value is: ",total)
  return total
# total = sum3(10,400)
sum3(20,70)
print(" Outside the function ( The global variable ) The value is: ",total)

count = 1
def do_st():
  global count     # Global variables: global
  for i in (3,4,5):  # cycle 3 time 
    count += 1
    # print(count)
do_st()
print(count)
#  Idea: when count=1 When entering the cycle +1 And assigned to it count
#      in for cycle 3 After time for 3+1=4
#     count Is a global variable, and the final printout is 4

Little practice


dict2 = {(1,2):5," tuples ":(4,5)}  # Dictionary elements can only use immutable data types!! 
print(dict2)
print(dict2[' tuples '])
print(dict2[(1,2)])

for i in dict2.keys():      # The value out is cleaner!! Remember what your teacher told you 
  print(" In the dictionary key Value is: ",i)
for j in dict2.values():
  print(" In the dictionary values Value is: ",j)
0

No return function, what is returned?

Answer: There is no return function in the function, return none

How to set a global variable in 1 function?

A: Define a global function global in the body of the function

# Two ways to randomly generate captCha:


dict2 = {(1,2):5," tuples ":(4,5)}  # Dictionary elements can only use immutable data types!! 
print(dict2)
print(dict2[' tuples '])
print(dict2[(1,2)])

for i in dict2.keys():      # The value out is cleaner!! Remember what your teacher told you 
  print(" In the dictionary key Value is: ",i)
for j in dict2.values():
  print(" In the dictionary values Value is: ",j)
1

dict2 = {(1,2):5," tuples ":(4,5)}  # Dictionary elements can only use immutable data types!! 
print(dict2)
print(dict2[' tuples '])
print(dict2[(1,2)])

for i in dict2.keys():      # The value out is cleaner!! Remember what your teacher told you 
  print(" In the dictionary key Value is: ",i)
for j in dict2.values():
  print(" In the dictionary values Value is: ",j)
2

conclusion

That's it for the Python dictionary, functions, and global variable code parsing. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: