Lesson Three: Python basic data Type lectures (3/3)

Source: Internet
Author: User


One, type
1. Immutable type string, int, tuple
2. Variable Type list, Dict

>>> a = "test"
>>> A[0]
' t '
>>> a[0]=e
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' e ' is not defined
>>> a = ["T", "E", "s", "T"]
>>> A[0]
' t '
>>> a[0]= "E"
>>> A
[' E ', ' e ', ' s ', ' t ']

Second, to investigate the string
What exactly is a sequence? The Eagle and the chick
1. The difference between three symbols "," "," "" "" ""
Print "This is 1 '"
print ' This is 2 '
Print "" "
' This is 1 '
"This is 2 '"
"""
2. Offset starting from 0
3. How to change the string's replace find

From the following statement, we want to know that a will not change after a.replace, if the need to change the value must be assigned value.

>>> A = "This is the world"
>>> A.replace ("This", "that")
' That's world '
>>> A
' This was World '
>>> a = A.replace ("This", "that")
>>> A
' That's world '
>>> A.find ("World")
8
>>> a.find ("www")
-1
>>> A[8]
' W '
>>> A[8:]
' World '


What should I do if we match multiple values?
>>> Help (A.find)
Help on built-in function find:

Find (...)
S.find (Sub [, Start [, end]]) int

Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
Arguments start and end is interpreted as in slice notation.

Return-1 on failure.

>>> a = "World, World, World"
>>> A.find ("World")
0
>>> A.find ("World", 11)
14

Third, the format of the scrutiny
1.% formatting method (placeholder)

>>> a = "World, World, World"
>>> A.find ("World")
0
>>> A.find ("World", 11)
14
>>> A = "This is a%s%s"% (4, 10)
>>> Print a
This is a 4 10


2. Format Formatting method

Call mode B = "This is {} {}". Format ("Apple", "my")
Location mode B = "This is {1} {0}". Format ("Apple", "my")
Identifier mode B = "This is {whose} {fruit}". Format (fruit = "Apple", whose = "my")


3. Why use Format
4. There is another way, the dictionary comes

A = "This is% (whose) s% (fruit) s"% {' whose ': ' Apple ', ' fruit ': ' My '}


Iv. further discussion open file

Close the file after you have written the file.
A = open ("Tmp1.txt", "W")
A.write ("This is a Apple")
A.close ()

To open and read files between read files, assign files to variables
A = open ("Tmp1.txt", "R")
A.read (20)
A.seek (0)
A.read (20)

Standard library Introduction Linecache

>>> a.write ("HAHA\NHAHA\NSFASDDA\NSDFSA")
>>> A.close ()
>>> Import Linecache
>>> print Linecache.getline ("Tmp2.txt", 1)
haha

>>> print Linecache.getline ("Tmp2.txt", 2)
haha

>>> print Linecache.getline ("Tmp2.txt", 3)
Sfasdda

>>> print Linecache.getline ("Tmp2.txt", 4)
Sdfsa

>>> lines = Linecache.getlines ("Tmp2.txt")
>>> Print Lines
[' haha\n ', ' haha\n ', ' sfasdda\n ', ' sdfsa\n ']
>>> Help (Linecache)
NAME
Linecache-cache lines from files.

FILE
d:\python27\lib\linecache.py

DESCRIPTION
This is intended to read lines from modules imported--hence if a filename
is not found, it would look down the module search path for a file by
That name.

FUNCTIONS
Checkcache (Filename=none)
Discard cache entries that is out of date.
(This isn't checked upon each call!)

ClearCache ()
Clear the cache entirely.

Getline (filename, Lineno, Module_globals=none)

DATA
__all__ = [' getline ', ' clearcache ', ' Checkcache ']

You can view the source code,

Look at the source code, in fact, is quite simple. Do not fear, nothing can not understand, once fear will produce a sense of distance.
Practice more, not optical theory. Bite the bullet and look at something, ask more questions. (the teacher said that this sentence I still very much agree with)

Lesson Three: Python basic data Type lectures (3/3)

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.