Python self-study (i)

Source: Internet
Author: User
Tags ord

1. Python Benefits

Simple, elegant, clear

Powerful module third-party library

Easy to transplant

Facing diagonal

Can be extended

2. Disadvantages

Code cannot be encrypted

Slow execution speed

3. Variable definition

The first letter must be a case, or underscore, in the alphabet. Cannot start with a number.

1) Example of variable assignment

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; You cannot define a variable with a non-casing underline

>>> tast_date = 1

2) Variable value type

Boolean type: True,false

eg

If true:print ' DDD '

Ddd

Integral type, long integer, float type:

eg

>>> type (a)

<type ' Long ' >

>>> A = 2**34

>>> type (a)

<type ' int ' >

>>> a=1.34

>>> type (a)

<type ' float ' >

>>> a=3

>>> type (a)

<type ' int ' >

>>> b=2.3

>>> type (b)

<type ' float ' >

String:

>>> name= ' Wang '

>>> type (name)

<type ' str ' >

sequence Type: list, array ...

>>> name_list=[' Wang ', ' Bai ', ' GUI '

>>> type (name_list)

<type ' list ' >

4. Arithmetic

A) "/" division, the default is only the whole, can be followed by a decimal point.

>>> 3/2

1

>>> 3.0/2

1.5

b) Take dividend

>>> 10//2

5

>>> 10//4

2

>>> 10//3

3

C) Subtraction method

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

*= "C*=a equals C=c*a"

**= "C**=a equals C=c**a"

d) and Operation &:

10 and 15 and the operation

1010 1111È10

10 20

1010 10100è

>>> & 20

0

>>> & 15

10

e) or operation:

10 20

1010 10100È11110 30

>>> 10 | 20

30

5. Comments

Single-line Comment: #

Multiline Comment: Three chapter quotes "" "or three double quotes" "" "", and the other can be formatted output

Tip: No difference between single and double quotes

6. Understanding character encoding

three character levels:assic(Default one byte) Unicode(two bytes) UTF-8(variable byte, kanji three bytes)

Overview: One byte 8 bits 111111 256 words, two bytes 16 bits 65,536 characters

1024 bytes =1kb

1) (ACCIS)

An example of a byte:

>>> Ord (' a ')

97; A corresponds to a few bits of 8 bits

>>> Ord (' A ')

A corresponds to a certain number of 8 bits.

Corresponds to each of each of the corresponding 256-digit counterparts.

2) UTF-8 Code: Variable

Save in English with a byte, the Chinese character is stored in three bytes.

>>> a= ' Wang '

>>> type (a)

<type ' str ' >

>>> a=u ' Wang '

>>> type (a)

<type ' Unicode ' >

>>> A

U ' Wang '

>>> Name=u ' king brother '

>>> type (name)

<type ' Unicode ' >

>>> Name

U ' \u738b\u67cf\u8d35 '

>>> name = "Little Brother Wang"

>>> Name

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

>>> name = u "little brother Wang"

>>> Name

U ' \u738b\u67cf\u8d35 '

3) Convert Unicode to UTF-8

>>> name = U ' King little Brother '

>>> Name

U ' \u738b\u67cf\u8d35 '

>>> name.encode (' Utf-8 ')

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

4) UTF-8 converted to Unicode

>>> wang= "little brother Wang"

>>> Wang

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

>>> wang.decode (' Utf-8 ')

U ' \u738b\u67cf\u8d35 '

Tips:

Python system By default is the Assic code encoding format, corresponding to a byte, so in Python in Chinese, there will be problems, should be converted to UTF8 encoding format

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

Name = u "Chinese"

Print Name

Tips:

In the system read into memory default is Unicode format, storage to the hard disk can be stored in UTF-8 format, because the Unicode default is two bytes of storage, occupy space.

8. Import Module

Three ways to import:

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) invoking 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 default output returns the return value of the previous instruction

==> The return value to be stored and output

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

/root/python/day01

>>> Print Cru_dir

0

b) How to output the output?

Pour into import commands module

>>> Import Commands

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

>>> Res

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

3) Pour into the import sys module

[email protected] day01]# cat test1.py

Import Sys

Print SYS.ARGV

Print Sys.argv[2]

[[email protected] day01]# python test1.py a b C

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

B

9. User interaction

1) raw_input

[email protected] 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

[email protected] day01]# cat test2.py

#!/usr/bin/env Ptyhon

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

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

Age = Raw_input (' Age: '); raw_input whatever input is interpreted as a string

Job = Raw_input (' job: '), can be converted to a number by int (raw_input (' Age: '), or directly with input (' Age: '), note: The imput is followed by the original ecology, what was before, is what, must indicate what is Type, etc., or there will be errors.

Salary = Raw_input (' Salary: ')

Print ""

Name:%s

Age:%s

Job:%s,%s for string,%d for number,%f for floating point

Salary:%s

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

"% (name,age,job,salary)

imput Example:

[Email protected] 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)

[email protected] day01]# python test2.py

Please input your Name:wangbaigui

Age:28

Job:it

salary:2w

<type ' int ' >

Name:wangbaigui

Age:28

Job:it

salary:2w

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

[Email protected] 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)

[email protected] day01]# python test2.py

Please input your Name:wangbaigui

Age:age

Job:it

salary:3w

<type ' int ' >

Name:wangbaigui

Age:28

Job:it

salary:3w

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're so old ... '

Elif Age >20:

Meg = ' ... '

Else

Meg = ' You're so yongest ... '

Print ""

Name:%s

Age:%d

Job:%s

Salary:%s

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

%s

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

2) for Loop

[email protected] 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 is right."

Break

Else

Meg = "Go to think"

Print Meg

Print "You had 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

[email protected] 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! '

Python self-study (i)

Related Article

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.