Python self-learning (1) and python self-learning

Source: Internet
Author: User

Python self-learning (1) and python self-learning

1. Python advantages

Simple, elegant, clear

Powerful third-party module library

Easy to transplant

Diagonal Orientation

Scalable

 

2. Disadvantages

Code cannot be encrypted

Slow execution speed

 

3. variable definition

The first letter must be case or underline in the alphabet. It cannot start with a number.

1) variable assignment example

Eg:

> X = 123

>>> Y = x

>>> Id (x)

22582176

>>> Id (y)

22582176

> X = 100

>>> Id (x)

22580736

>>> Id (y)

22582176

>>> Tast-date = 1

File "<stdin>", line 1

SyntaxError: can't assign to operator; variables cannot be defined with non-case underscores

>>> Tast_date = 1

2) variable value type

Boolean: true, false

Eg:

If True: print 'ddd'

Ddd

Integer, long integer, floating point type:

Eg:

>>> Type ()

<Type 'long'>

>>> A = 2 ** 34

>>> Type ()

<Type 'int'>

>>> A = 1.34

>>> Type ()

<Type 'float'>

>>> A = 3

>>> Type ()

<Type 'int'>

>>> B = 2.3

>>> Type (B)

<Type 'float'>

String:

>>> Name = 'wang'

>>> Type (name)

<Type 'str'>

Sequence type:List, array ......

>>> Name_list = ['wang', 'ba', 'GU']

>>> Type (name_list)

<Type 'LIST'>

 

4. Operation

A) "/" Division. The default value is only an integer, which can be followed by the decimal point.

>>> 3/2

1

>>> 3.0/2

1.5

B) Get the Divisor

>>> 10 // 2

5

>>> 10 // 4

2

>>> 10 // 3

3

C) addition, subtraction, multiplication, division

+ = "C ++ = a equals c = c +"

* = "C * = a = c *"

** = "C ** = a is equal to c = c **"

 

D) and Operations &:

10 and 15 operations

1010 1111

10 20

1010 10100

>>> 10 & 20

0

>>> 10 & 15

10

E) or operation:

10 20

1010 10100 11110 30

>>> 10 | 20

30

 

 

5. Notes

Single line comment :#

Multi-line comment: three chapter quotation marks ''' or three double quotation marks ". The other one can also be used as a formatted output.

Tip: there is no difference between single quotes and double quotes.

 

6. Understand character encoding

Three character levels: ASSIC(One byte by default)Unicode(Two bytes)UTF-8(Variable byte, three Chinese characters)

Overview:One byte, 8-bit, 111111, 256 words, two bytes, 16-bit, 65536 words

1024 bytes = 1KB

1) (ACCIS)

Example of a byte:

>>> Ord ('A ')

97; a corresponds to a certain number of 8 digits

>>> Ord ('A ')

65; A corresponds to A certain number of 8 digits

It is equivalent to a one-to-one correspondence between each of the 256 S.

 

2) UTF-8 coding: Variable

English is saved in one byte, and Chinese characters are saved in three bytes.

>>> A = 'wang'

>>> Type ()

<Type 'str'>

>>> A = u'wang'

>>> Type ()

<Type 'unicode '>

>>>

U'wang'

>>> Name = u'wang xiaoge'

>>> Type (name)

<Type 'unicode '>

>>> Name

U' \ u738b \ u67cf \ u8d35'

 

>>> Name = "Wang xiaoge"

>>> Name

'\ Xe7 \ x8e \ x8b \ xe6 \ x9f \ x8f \ xe8 \ xb4 \ xb5'

>>> Name = u "Wang xiaoge"

>>> Name

U' \ u738b \ u67cf \ u8d35'

 

3) unicode conversion to UTF-8

>>> Name = u'wang xiaoge'

>>> Name

U' \ u738b \ u67cf \ u8d35'

>>> Name. encode ('utf-8 ')

'\ Xe7 \ x8e \ x8b \ xe6 \ x9f \ x8f \ xe8 \ xb4 \ xb5'

 

4) UTF-8 to unicode

>>> Wang = "wang xiaoge"

>>> Wang

'\ Xe7 \ x8e \ x8b \ xe6 \ x9f \ x8f \ xe8 \ xb4 \ xb5'

>>> Wang. decode ('utf-8 ')

U' \ u738b \ u67cf \ u8d35'

 

Tip:

In the python system, the default ASSIC code encoding format corresponds to one byte. Therefore, if you store Chinese Characters in python, you should convert them to UTF8 encoding format.

# _ * _ Coding: UTF-8 _*_

Name = u "Chinese"

Print name

Tip:

The default unicode format is read in the system memory, saved to the hard disk can be saved in the UTF-8 format, because unicode by default is saved in two bytes, occupied space.

 

8. Import Module

Three import methods:

1) import modulename

2) from module import sayHi

3) import moduleName as newname

Eg:

Import sys

Print sys. argv

Or

From sys import argv

Print argv

Or

From sys import *; not recommended

Or

Import multiprocessing as nulte

======================================

Eg:

A) call system commands

>>> Import OS

>>> OS. system ('df ')

Filesystem 1K-blocks Used Available Use % Mounted on

/Dev/sda3 18423556 1691736 15795936 10%/

Tmpfs 405824 0 405824 0%/dev/shm

/Dev/sda1 198337 29668 158429 16%/boot

0;The Return Value of the previous command is returned by default.

==> Store and output the returned values

>>> Cru_dir = OS. system ('pwd ')

/Root/python/day01

>>> Print cru_dir

0

B) How to output the output results?

Import commands Module

>>> Import commands

>>> Res = commands. getstatusoutput ('pwd ')

>>> Res

(0, '/root/python/day0 ')

 

3) import the import sys module

[Root @ localhost day01] # cat test1.py

Import sys

Print sys. argv

Print sys. argv [2]

 

[Root @ localhost day01] # python test1.py a B c

['Test1. py', 'A', 'B', 'C']

B

9. User Interaction

1) raw_input

[Root @ localhost day01] # cat test2.py

#! /Usr/bin/env ptyhon

# _ * _ Coding: UTF-8 _*_

Name =Raw_input('Please input your name :')

Age = raw_input ('Age :')

Print name, age

 

[Root @ localhost day01] # cat test2.py

#! /Usr/bin/env ptyhon

# _ * _ Coding: UTF-8 _*_

Name = raw_input ('Please input your name :')

Age = raw_input ('Age: '); raw_input is interpreted as a string regardless of input.

Job = raw_input ('job: '); it can be converted to a number through int (raw_input ('Age:') or directly using input ('Age: '). Note: imput is followed by the original ecology, what was previously, and what type must be specified, otherwise there will be errors.

Salary = raw_input ('salary :')

Print '''

Name: % s

Age: % s

Job: % s; % s indicates a string, % d indicates a number, and % f indicates a floating point.

Salary: % s

-----------------

''' % (Name, age, job, salary)

 

 

Imput example:

[Root @ localhost day01] # vim test2.py

#! /Usr/bin/env ptyhon

# _ * _ Coding: UTF-8 _*_

Name = raw_input ('Please input your name :')

Age = input ('Age :')

Job = raw_input ('job :')

Salary = raw_input ('salary :')

Print type (age)

Print '''

Name: % s

Age: % s

Job: % s

Salary: % s

-----------------

''' % (Name, age, job, salary)

 

[Root @ localhost day01] # python test2.py

Please input your name: wangbaigui

Age: 28

Job: it

Salary: 2 w

<Type 'int'>

 

Name: wangbaigui

Age: 28

Job: it

Salary: 2 w

-----------------

[Root @ localhost day01] # vim test2.py

#! /Usr/bin/env ptyhon

# _ * _ Coding: UTF-8 _*_

 

AGE = 28

Name = raw_input ('Please input your name :')

Age = input ('Age :')

Job = raw_input ('job :')

Salary = raw_input ('salary :')

Print type (age)

Print '''

Name: % s

Age: % s

Job: % s

Salary: % s

-----------------

''' % (Name, age, job, salary)

 

[Root @ localhost day01] # python test2.py

Please input your name: wangbaigui

Age: AGE

Job: it

Salary: 3 w

<Type 'int'>

 

Name: wangbaigui

Age: 28

Job: it

Salary: 3 w

 

 

10. Process Control

1) if... Else... Example:

#! /Usr/bin/env ptyhon

# _ * _ Coding: UTF-8 _*_

Name = raw_input ('Please input your name :')

Age = input ('Age :')

Job = raw_input ('job :')

Salary = raw_input ('salary :')

 

If age> 30:

Meg = 'you are so old ...'

Elif age> 20:

Meg = '... '

Else:

Meg = 'you are so yongest ...'

 

Print '''

Name: % s

Age: % d

Job: % s

Salary: % s

-----------------

% S

''' % (Name, age, job, salary, meg)

 

2) for Loop

[Root @ localhost day01] # cat test4.py

#! /Usr/bin/env ptyhon

# _ * _ Coding: UTF-8 _*_

Name = raw_input ('Please input your name :')

Job = raw_input ('job :')

Salary = raw_input ('salary :')

 

Relea_age = 28

For I in range (10 ):

Age = input ('Age :')

If age> 30:

Meg = "think small ..."

Elif age = 28:

Meg = "good !, You are right ."

Break

Else:

Meg = "go to think"

Print meg

Print "you have only % s times to trye" % (9-I)

 

Print '''

Name: % s

Age: % d

Job: % s

Salary: % s

-----------------

% S

''' % (Name, age, job, salary, meg)

 

3) while loop

[Root @ localhost day01] # cat test5.py

Slect_num = input ('which num you want :')

Count = 0

While count <100:

If count = slect_num:

Print 'you slect right: % s' % (slect_num)

Choice = raw_input ('You want go on or contine (Y/N )')

If choice = 'y ':

While True:

Slect_num = input ('which num you want agan :')

If slect_num <= count:

Print "lookup alred past..., ples input newest num! "

Else:

Break

Continue

Else:

Break

Else:

Print 'lookup ', count

Count + = 1

Else:

Print 'alread more then 100 .. so Bye! '

 

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.