Python Study Notes (for your own use)

Source: Internet
Author: User

Number = 23; # define variables
Guess = 22;
If number = guess: # If judgment, note: no less
Print 'yse'; # note that indentation in python can be used only when it is included.
Elif number> guess: # note that it is not else if but Elif
Print 'Big ';
Else:
Print 'no ';
Run = true;
I = 0;
While I> 3: # While loop also note:
Print "loop ";
Print I;
I = I + 1;
If I = 5:
Run = false;
Else: # Use the running statement if the condition is not met
Print "Not while! ";
For I in range (): # For Loop range indicates that the period from 1 to 5 ends.
Print I;
# Break; # break ends
Continue; # Skip this loop
Else: # statements executed when the loop ends
Print "end ";
Def name (): # Function Definition
Print "Hello! ";
# Function end # no end
Name (); # function usage
X = 50;

# Def echo (x ):
# Print X;
# X = 2; # valid in the local Variable Function
# Return X;
# Echo (X );

Def echo ():
Global X # Use global variables
Print X;
X = 2;
Return X;
Echo ();
Print echo. _ Doc __, X; # use to output multiple variables, separated
Def null ():
'''Asdfasdfsdfasdf # function annotation, which I understand in this way
Adfadfasdfasdfasdfa ''' # End of comment. The content in the middle of ''' can be divided into multiple lines '''
Pass; # indicates that the function does not have a statement
Print null (); # output none if the function is null
# Print none. _ Doc __; # comment on the output function (which I understand)

######################################## ##########
Import sys; # use the system's SYS module
Print 'the command line arguments are :'
For I in SYS. argv: # Output System Parameters
Print I

Print '/n/nthe pythonpath is', SYS. Path, '/N' # output environment variables in the system
# Note that it is best to use the PyC file name for character encoding in the module to speed up loading and compile part of the code first.
Print _ name __; # Module name, which is generally used when determining whether it is the main module. The name of the main module is _ main __
######################################## ##########

Raw_input ('input: '); # receives data from the screen and displays input in the output. If it is accepted directly, raw_input can be used.

##############################
# Use of Classes
Class person: # declare the class name
Pop = "123"; # define the variables in the class as public
Name = '';
# Pass # An empty Block
Def _ init _ (self, name): # constructor. The method name is fixed. Self is required for input, required, followed by Parameters
Self. Name = Name;
Def sayhi (Self): # define a method
Print "woea:", self. Name;
Def echopop (Self ):
Print self. Pop;
Self. Pop = 'Come on baby ';
Print self. Pop;
Def _ del _ (Self): # destructor with fixed method names
Pass;

Class pchild (person): # inherit the person class
Def _ init _ (Self ):
Person. _ init _ (self, "sanshi"); # Use the parent class Method
Def _ del _ (Self ):
Pass
Def saychild (Self ):
Print "Child:", self. Name;
##############################

Import cpickle as P # introduce the package, which is written in C language and fast
# Import pickle as P

Shoplistfile = 'shoplist. data' # file name
# The name of the file where we will store the object

Shoplist = ['apple', 'mango', 'carrot']

# Write to the file
F = file (shoplistfile, 'w') # open the file
P. Dump (shoplist, f) # serialize the file and write it to the media. We write the file.
F. Close ()

Del shoplist # delete a list

F = file (shoplistfile)
Storedlist = P. Load (f) # Read serialized data from the file
Print storedlist
######################################## ##########
Exception Mechanism

Try:
Test code
Raise:
Throw an exception
Except t:
Capture Exception Handling
Else:
No exception
-------------------------------------------------------------
Try:
Test code
Finally:
Execute if any exceptions exist.

######################################## ############
There are a lot of other things, but I didn't take notes anymore.
Note that two packages, one sys and one OS, have completed a lot of work.

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.