Python learning ------- control flow statement and control statement in python
Compile the function object and test the for statement. Use the range Function to print the letters of an even string, use the zip function for parallel iteration, and use zip to construct the two lists into dictionaries.
#! /Usr/bin/pythone
# Encoding: utf8 # Avoid Chinese garbled error: SyntaxError: Non-ASCII character '\ xe6' in file
Def testwhile (var1 ):
Cnt = 1;
While cnt <= var1:
Print (cnt)
Cnt = cnt + 1
Else:
Print (cnt, "is done ")
Def testfor ():
List1 = [(1, 'A'), (3, 'B'), (4, 'C')]
Dict1 = dict (list1)
For key in dict1:
Print (key, '=>', dict1 [key])
Else:
Print ("testfor dict is done ")
# List2 = list (dict. items ())
For (a, B) in list (dict. items (dict1 )):
Print (a, "=>", B)
Else:
Print ("testfor items is done ")
Def testrange ():
For cnt in range (-3, 2 ):
Print (cnt, str (cnt) + "aaa ")
Else:
Print ("testrange is done ")
# Print the letter of an even string, using the len () function
Strs = "weojslkjdfvcnzd"
Def print2char ():
Print (range (1, len (strs), 2 ))
Newstr =''
For pos in range (1, len (strs), 2 ):
Print (pos, "->", strs [pos])
Newstr + = strs [pos]
Else:
Print ("new word is", newstr)
Print ("print2char is done ")
# Use zip for parallel iteration, and use zip to construct the two lists into dictionaries
Key = [1, 3, 5, 7]
Value = ['a1', 'a2 ', 'a3', 'a4']
Def double_iteration ():
Print (zip (key, value ))
Print ("double_iteration is done ")
"Each module has a built-in attribute named _ name _. python automatically sets this attribute. If the file is executed as a top-level program file, __name _ is set to string _ main __"""
If _ name __= = '_ main __':
Testwhile (10)
Testfor ()
Testrange ()
Print2char ()
Double_iteration ()
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.