Python Basic Learning Notes

Source: Internet
Author: User

1.Python Program Structure:

Input-process-output


Output statement:

Print () function

Format formatting function

Format (Val, ' M.nf ')

M.nf,m is a placeholder,

For example:

Print (Format (12.243, ' 3.2f ')), _12.24print (Format (0.1245, ' 2.2% ')), 12.45%


Input statement:

Raw_input ([Promot]) returns a string

Re = Raw_input () type (re), <type ' str ' ># gets the type of the variable nre = Int (re) type conversion


2. Variable resolution

A python variable is an identifier that points to the meaning of a pointer in C + +.

is mutable, so there is no need for a type definition.

The ID () function can get the address of the object

>>> x = 12>>> ID (x) 20148988>>> x = 13>>> ID (x) 20148976>>> y = x# y points to 13 data block as X >>> ID (y) 20148976>>>


3. Function library

Type ()# View Variable type

ID ()# Return object address

Print ()# Printing

Raw_input () # input character

Help ()# View function usage

function Library Import

Import Math


Math function library for math

Math.PI 3.1415 ...

Math.sin (MATH.PI/6) = 0.5

Math.pow (3, 4) = = 3**4 = 81


Import OS#系统相关函数库

OS.GETCWD () #获取当前目录

Os.listdir (OS.GETCWD ()) # Get folders and files under directory


Import Socket # Network library functions

ip = socket.gethostbyname (' www.baidu.com ')

Print IP-111.13.100.91



4. Using a third-party function library

Download third-party libraries on your network, such as Httplib2

Https://pypi.python.org/pypi/httplib2#downloads

Download the extract, execute the command python setup.py Install the third-party library can be installed.

Python added to environment variables


Import WebBrowser as Web # Importing library aliases


5. Custom Functions

Keyword def

# no parameters

Def test_fun ():

print ' Hell world '

# of 2 parameters

def test_fun_ret (Val1, Val2):

return val1 + val2# function returned value

# Pre-valued functions

def test_fun_ret (Val1, val2, Val3 = 5):

Return val1 + val2 + val3

# function can return multiple return values

def test_fun (N1, N2):

Print N1,

Print N2

n = n1 + N2

m = N1 * N2

p = n1-n2

E = N1 * * N2

Return n, M, p, E, ' return '


# When returning, you can get multiple return values in the following ways

Val1, Val2, Val3, val4, VAL5 = Test_fun (10, 2)

# or a variable directly, at which time re is a tuple (tuple)

Re = Test_fun (10, 2)

Print re[0], re[1], re[2], re[3]

Type (re)-<type ' tuple ' >


6.if statements

If condition:

Statement1

Statement2

Elif Condition2:

Statement1

Else

Statement1


7.while Loop statement

While condition:

Statement1

Statement2

..

[Break]

[Continue]

Else

Statement1

i = 0

s = 0

# and logical with, or logical OR

While 1 and I < 100:

s = s + I

i = i + 1

Print S

8. Chinese Notes

#-*-Coding:utf-8-*-

# python file to write in Chinese, you must add a line to declare the file encoding comments, otherwise Python will default to use ASCII encoding



Python Basic Learning Notes

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.