Python Learning Day5

Source: Internet
Author: User

1. String formatting
Percent semicolon formatting
s = "I am%s"% ' Wyx ' #通过位置传递参数, formatted string type

s = ' I am% (name) s '% {' name ': ' Wyx '} #通过命名传递参数

s = ' I have percent%.2f '% 1.111 #小数点类型

s = ' I have percent% (p). 2f '% {' P ': 1.1} #小数点命名传递参数


Format formats
#索引传参
S1 = ' I am {0} age {1} age{1} '. Format (' Wyx ', 123)

#名字传参, and format: s for string formatting:d representing numeric formatting
S2 = ' I am {name:s} age{age:d} '. Format (name= ' Wyx ', age=18)

#位置传递参数, {} indicates that any parameter can be accepted.
S3 = ' I am {} age {} {} '. Format (' Wyx ', ' haha ')

# *[] Pass each parameter in the list to the location via a list
S4 = ' I am {} age {} {} '. Format (*[' Wyx ', ' haha ')

# **{} passes value to the corresponding key value: s represents character formatting:d number formatting
S5 = ' I am {name:s} age {Age:d} really {name:s} '. Format (**{"name": ' Wyx ', ' Age ': 18})

#: 2f stands for two digits after the decimal point
S6 = ' I have {percent:.2f} '. Format (percent=1.13233244)

#通过元素索引来传递参数
S7 = ' I am {0[0]} Age{0[0]} '. Format ([' Wyx ', ' cxl ', [18,19]])

#:b binary: o octal:d integers: x 16 binary:% converted to percent
S8 = ' numbes:{:b}{:o}{:d}{:x}{%} '. Format (15,15,15,15,15w)


Generator
def func ():
Print (111)
Yield 1
Print (2222)
Yield 2
Print (333)
Yield 3

ret = func ()
R1 = ret.__next__ () #进入函数找到yield, gets the data after yield 1 #相当于return yield after the data 1
Print (R1)
r2 = ret.__next__ ()
Print (R2)
R3 = ret.__next__ ()
Print (R3)

Def myrange (ARG): #自己写的range函数
Start = 0
While True:
If Start>arg:
Return
Yield start
Start+=1
ret = myrange (10)
R=ret.__next__ ()
Print (R)


Iterators

def func (N):
N+=1
If n>5:
Return ' End '
return func (N)
ret = func (1)
Print (ret)

Module
Existence mode:
Files folder

Using modules:
Use after import first

Modules: Built-in modules third-party module custom modules

The basis for importing modules:
Import Sys
Sys.path

Import Module Mode:
Import ModuleName
From xx import xx as oo

Module naming rules: cannot be the same as the built-in module name

Module installation
SOURCE installation: Download the source code via python.x setup.py Install
PIP Installation: PIP3 install ModuleName

1. Serialization related

Import JSON
Import Pickle

DIC = {' A ': ' B '}
#json. Dumps (DIC) serialization: Converting a Python base type to a string

DiC = ' {' A ': ' B '} '
#json. Loads (DIC) deserialization: Converting a string to a Python base type

Import Pickle
Li = [11,22,33]
r = Pickle.dumps (LI)
Print (R)

result = Pickle.loads (r)
Print (Result)

Json/pickle
JSON is more suitable for cross-language/pickle only for Python
+-------------------+---------------+
| Python | JSON |
+===================+===============+
| Dict | Object |
+-------------------+---------------+
| List, tuple | Array |
+-------------------+---------------+
| str | string |
+-------------------+---------------+
| int, float | number |
+-------------------+---------------+
| True | true |
+-------------------+---------------+
| False | False |
+-------------------+---------------+
| None | null |

Time Module
Time&datetime

Python Learning Day5

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.