Python learning ------- control flow statement and control statement in python

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.