Python basics-Loops and modules

Source: Internet
Author: User

Cycle:

1.While Cycle

While condition:

? ? ? ? ? Statement

Eg: Quit after retrying 3 errors

Count?=?0_number=15while?count?<3:???? Guess_age?=?int (Input ("Guess?age:"))???? If?? Guess_age?==_number:???????? Print ("Yes")???????? Break???? Elif?? Guess_age?>?_number:???????? Print ("Think? Smaller ")???? else:????? Print ("Think? Bigger ")????? Count+=1else??:???? Print ("Too? Many? Times ")


2.For Cycle

Continue is returned to the beginning of the loop

For I in range ("lower limit, upper limit, step Size"):

EXECUTE statement

Eg: program optimization, add judgment input after count=count+1

If?count?==?3:??? Try?=input ("Do?you?want?to?try?")??? if?try?! = "N"??? Count=0


Module:

1. Mainly divided into standard library and third-party library (need to download and install)

2. Import library name and file name not the same

General standard library Path: Python_path----base----Lib

The third party libraries path: ... Lib---site-packages

SYS module-->import SYS

Sys.path:Python Environment variables

SYS.ARGV: Relative path

OS module: Import os

Os.system: Call system cmd, result output directly on screen, cannot save

Os.popen: Call system command to save

eg

Cmd_res=os.popen ("dir"). Read () print ("--", cmd_res)


Os.mkdir: Creating a Directory

eg

Os.mkdir ("New dir")

You can write your own module in the directory or environment variables to facilitate other program calls


PYc

Python, like Java, is a compiled and interpreted language. When the program runs, the results will be saved in the PYc file, and when the second program runs, it will first look for the existence of the PYc file, if there is no need to compile direct interpretation, improve efficiency.


Python basics-Loops and modules

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.