2. Python program Control structure (0530)

Source: Internet
Author: User

Condition test:

1.If condition test expression

String: A dictionary-by-character comparison in alphabetical order (by alphabet or ASCII code)

2. True and False in Python:

    • Any non-0 numeric and non-empty objects are true
    • Number 0, empty object, and special object none are False
    • Comparisons and equality tests are applied recursively to the data structure
    • The return value is TRUE or False

3. Combination condition test:

    • X and Y: with operations
    • X or Y: or operation
    • Not x: Non-operational

The syntax structure of the IF test

A = X if y else Z//if y condition satisfies, a=x, otherwise a=z, ternary expression

If Y:

A=x

Else

A=z

While and for loops

.......

While syntax format:

While Bool_expression:

While_suite

Else

Else_suite

Break: Jump out of the inner loop

Continue: Jumps to the beginning of the closest layer loop where it is located

Pass: Bit statement

Else code library: The loop terminates normally, and if the loop termination is caused by a break, else does not execute

Exercise 1: Display all the elements in the specified list individually;

Exercise 2: Ask for all even numbers within 100 and;

Contact 3: Displays all keys of the specified dictionary one by one, and displays the total number of keys after the end;

While true: Dead loop

Review:

1. Dynamic language

Sys.getrefcount ()

Increase the object's reference count scenario

When the object is created:

When adding objects to a container: Lis

T.append ()

When an object is passed as a parameter to a non-function

Create an additional variable name for an object

Reduce reference count

A variable name referencing this object is displayed for destruction: Del X

A variable name that references this object is re-assigned

When removing objects from a container, similar to List.pop ()

The container itself is destroyed

2.if

If boolean_exprssion:

....

Elif boolean_exprssion:

....

Else

....

3.while

While Boolean_exprssion:

While_suite

If Boolean_esprssion2:contine

If Boolean_esprssion3:break

Else

Else_suite

4.for

For Exprssion in object:

While_suite

If Boolean_esprssion2:contine

If Boolean_esprssion3:break

Else

Else_suite

Raw_input: Enter a content

Iteration: Doing one thing over and over again

List resolution:

Generate a new list based on an existing list

File object:

Text is a sequence of bytes

Var_name = open (File_name[mode,[bufsize]])

Mode

R

W

A

r+

w+

A +

b means that the file is opened in binary mode

Rb

Wb

Ab

rb+

wb+

ab+

Cache:

0 means no cache is used

A negative number means using the system default cache

1 means using the cache

integers represent buffers that use size

File.seek (Offset[whence])

Whence: Starting point

0: From the file header

1: From the current position

2: From the end of the file

Offset: Offsets

File system module: OS

Directory:

ChDir (): Switch working directory

Chroot (): Sets the root directory of the current process

Listdir (): Lists all filenames under the specified directory

mkdir (): Create the specified directory

Makedirs (): How long to create a directory

GETCWD (): Get working directory

RmDir (): Delete directory

Removedirs (): Delete Multilevel directory

File:

Mkfifo (): Create pipeline

Mknod (): Create a Device file

Remove (): Delete file

Unlink (): Delete linked file

Rename (): renaming

Stat (): Put back the status information of the file

Symlink (): Create a linked file

Utime (): Update timestamp

Tmpfile (): Create and open (W+B) a new temporary file

Walk (): Creating a directory tree

Access rights-related

Access (): Verify the permission mode for a user or group of users

chmod (): Modify Permissions

Chown (): Modify the owner and the genus Group

Umask (): Set default permission Mode

File Descriptor:

Open (): open () of the underlying operating system

Read ():

Write ():

Device files:

Makedev ():

Major (): Get the main device number

Minor (): Gets the secondary device number

Path Management:

Import Os.path

basename (): Path base Name

DirName (): Path directory Name

Join (): Consolidated file name

Split (): Returns DirName (), basename () tuple

Splitext (): Return (filename,extension) tuple

Information:

Getatime ()

Getctime ()

Getmtime ()

GetSize (): Returns the size of the file

Inquire

Exists (): Determines whether the specified file exists

Isabs (): Determines whether the specified path is an absolute path

Isdir (): Whether it is a directory

Isfile (): Whether it is a file

Islink (): Whether it is a symbolic link

Ismount (): Whether it is a mount point

Samefile (): Two paths pointing to the same file

Exercise: Determine if a file exists, open it

Allow users to repeatedly enter multiple rows of data through the keyboard

Append save to this file

#!/usr/bin/python

Import OS

Import Os.path

Filename=raw_input (' plz a file name: ')

If Os.path.isfile (filename):

Openname= open (filename, ' A + ')

While True:

Newline=raw_input (' plz a line: ')

if newline = = ' Q ' or newline = = ' Quit ':

Break

Else

Openname.write (newline+ ' \ n ')

Openname.close ()

Object Persistent Storage

Pickle

Marshal

DBM Interface

Shaelve Module

2. Python program Control structure (0530)

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.