Beginner's mind Continental-----Python Treasure Fourth Chapter

Source: Internet
Author: User

The new story begins again.

Continue to learn.

Lists and tuples, eat and eat, eat them all.

In the beginner's mind continent, the most basic data structure is the sequence, each element of the sequence is assigned an ordinal, that is, the position of the element, also known as the position of the element. The first index is 0, the second one is 1, and so on.

The last element in the sequence is marked as-1, and the second-lowest element is-2

And so on


General normal thinking seems to start with 1, this seems to be 0 start, a single beginner's mind mainland has her rules, according to his come.

As for why the last is-1 and so on, I guess it's easier to be able to identify from the back.

There are 6 built-in sequences in the beginner's mind continent, because just getting started, just know the tuples and lists.

Focus on: List and tuple differences: Lists can be modified and tuples cannot be modified. In general, in almost all cases, the list can be substituted for tuples, (there is an exception, it is not clear at the moment, need to follow-up learning to know).

When you need to manipulate the values, the sequence is very useful, you can use a sequence to represent a person's information in the database, the first element is the name,

The second element is age. Write code based on this requirement

>>> edward = [' Edward Gumby ', 4]

>>> John = [' John Smith ', 5]

>>> databse = [Edward,john]

>>> databse

[[' Edward Gumby ', 4], [' John Smith ', 5]]

>>>

Just write it over and over again, looks like the storage relationship with the database is a bit similar, can also store related users and other information.

Very similar, continue to learn to see if there are more fun things

Looks pretty good to understand.

A generic sequence operation.

All sequence types can perform certain operations that include: index (Indexing), Shard (sliceing),

Add (adding), multiply (multiplying), and check whether an element belongs to a member of a sequence, except that, my goodness, a lot of knowledge. Beginner's mind continents can also calculate the length of a sequence to find the maximum, minimum value of the built-in function.

Another important aspect is iteration: repeat some operations on each element of the sequence in turn.


Index. :
For example, the simplest:

>>> greeting = ' Helloworld '

>>> Greeting[0]

The first element that the ' H ' index 0 points to is the letter H, because the sequence starts at 0.

>>>

>>> Greeting[-1]

The last position of ' d ' is denoted by-1 instead of-0

>>>

The following content let little p teacher to demonstrate it




months = [

' January ',

' February ',

' March ',

' April ',

' May ',

' June ',

' July ',

' August ',

' September ',

' October ',

' November ',

' December ',

]

# # #st + nd + rd + 17 x th+ St + nd + RD + 7th+st

endings = [' st ', ' nd ', ' Rd '] + + * [' th ']\

+[' st ', ' nd ', ' rd ' + 7 * [' th ']\

+ [' st ']

Year= Raw_input (' Year: ') interactive input

month = Raw_input (' Month (1-12): ') Interactive input

Day = Raw_input (' Day (1-31): ') Interactive input


month_number = Int (month) converts the assigned month to an integral type

day_number = Int (day) converts the day of assignment to an integral type


Month_name = months[month_number-1] minus 1 months and days to get the correct index, why minus 1, not clear. Who knows can reply to inform under.

Ordinal = day + endings[day_number-1]

Print Month_name + ' + ordinal + ', ' + year

The result of the output is:

year:2015

Month (1-12): 12

Day (1-31): 31

December 31st, 2015

>>>

Then looked down, shards.

What is a shard: it can be understood that by using a shard operation to access a range of elements, a shard can be implemented by two indexes separated by colons.

>>> tag

' 123456789http://www.baidu.com1234556789 '

>>> Tag[9:-10]

Isn't that interesting?

I have just done another experiment and found that when the assignment was not preceded by a single quote in the Shard, there was no effect. This piece should be taken care of.

>>> tag = [' 123456789http://www.baidu.com1234556789 ']

[' 123456789http://www.baidu.com1234556789 ']

>>> Tag[9:-9]

There's one more thing to note about sharding. It is important to remember that the implementation of a shard operation requires two indexes as a boundary, the element of the first index is contained within the Shard, and the second is not contained within the Shard.

>>> numbers = [1,2,3,4,5,6,7,8,9,10]

>>> numbers [3:6]

[4, 5, 6]

>>> numbers [7:10]

[8, 9, 10] Here is explained, because there is no 11th element, so does not exist, but he is the last element, so you can display

>>>

>>> numbers

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> Numbers[-3:-1]

[8, 9]

>>>

What happens if I remove the 1 and add no value to the back?

>>> numbers[-3:]

[8, 9, 10] He will print all the values behind-3.

>>>

The same approach applies to the start sequence:

>>> Numbers[:3]

[1, 2, 3]

>>>


If you want to output an entire column:

>>> numbers[:]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Larger step size, what is this?

Usually the normal shard step is usually the default of 1, and shards are all elements that iterate through the sequence elements and then return to the end point by this step.
Probably more clearly.

>>> numbers[0:102]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> Numbers[0:10:2] 2 of this is the stride length.

[1, 3, 5, 7, 9]

>>>

If the step is set to 0 then it will not be executed downward. However, the step can be negative, that is, from right to left to extract elements.

Try it again.

Step 2 is extracted from the left guide. A step of 2 is a right-to-left extract element.

>>> Numbers[-5::2]

[6, 8, 10]

>>>

>>> Numbers[-5::-2]

[6, 4, 2]

>>>

The sum of the sequences.

>>> [1,2,3]+[4,5,6]

[1, 2, 3, 4, 5, 6]

>>> ' Hello ' + ' world '

' HelloWorld '

>>> ' Hello ' + [+ +]


Traceback (most recent):

File "<pyshell#50>", line 1, in <module>

' Hello ' + [+ +]

Typeerror:cannot concatenate ' str ' and ' list ' objects

>>>

Lists and strings cannot be joined together, only sequences of the same type can be connected.


Multiplication: Again the multiplication does not feel like a simple multiplication, inexplicable uneasiness arises.

Look at what I explained: The general meaning is that the sequence will be repeated x times

Try the operation again:

>>> ' python ' * 5

' Pythonpythonpythonpythonpython ' python don't multiply it by 5 times.

>>>

If you want to empty the value of a list, but you want to make up the value of the corresponding element, you can follow the following methods:

>>> seq = [None] * 10

>>> seq

[None, None, none, none, none, none, none, none, none, none] None can represent null values

>>>


Small p teacher again a small case: the skill to come:


Sentence = Raw_input ("sentence:")


Screen_width = 80

Text_width = Len (sentence)

Box_width = text_width + 6

Left_margin = (screen_width-box_width)//2


Print

print ' * left_margin + ' + ' + '-' * (box_width-4) + ' + '

print ' * left_margin + ' | ' + ' * text_width + ' | '

print ' * left_margin + ' | ' + sentence + ' | '

print ' * left_margin + ' | ' + ' * text_width + ' | '

print ' * left_margin + ' + ' + ' _ ' * (box_width-4) + ' + '

Print

The result of the output is:

+---------------------+

| |

|hello,world,python! |

| |

+_____________________+

Well, how to get to this paste back more out. There is no problem in the experiment.

Membership, it feels like a topic: it's a mistake.

Look at the following things:

In order to check whether a value in the sequence can use the in operator, and the operation of the data is consistent, the database inside the operation will also use in to find the parentheses in the existence of the data.

, a bit different here is to determine whether it exists, and it is true. Contrary to false.

Examples come:

>>> ABS = ' RS '

>>> si in Bas


Traceback (most recent):

File "<pyshell#58>", line 1, in <module>

Si in Bas

Nameerror:name ' Si ' is not defined

>>> ABS = ' RS '

>>> ' SF ' in ABS

False

>>> ' s ' in ABS

True

>>>

>>> users = [' Mlh ', ' foo ', ' Bar ']

>>> raw_input (' Enter your users name: ') in users

Enter your users NAME:MLH

True

>>>

Does it feel magical, the in method can be used in this way.

>>> subject = ' $$ $ffasfdasf $$$ '

>>> $$$ in subject special symbols need single quotes to amplify them

Syntaxerror:invalid syntax

>>> ' $$$ ' in subject original special symbol can also be used to find

True

>>>


Member Entry Eligibility:

For example:

Database = [

[' Albert ', ' 1234 '],

[' Dilbert ', ' 4242 '],

[' Smith ', ' 7524 '],

[' Jones ', ' 9843 ']

]

Username = raw_input (' User name: ')

Pin = raw_input (' Pin Code: ')


If [Username,pin] in Database:print ' Access granted '

Output results

User Name:albert

PIN code:1234

Access granted

>>>

Good tired ah, learned a lot of things, need to absorb, not necessarily learn fast is good, only the basic grasp, to better development.

But the learning passion is still in, looking at the last thing:

Length, minimum value, maximum value:

are built-in functions: Len,min,max

Specifically how to operate it:?

>>> numbers = [100,54,421]

>>> Len (Numbers)

3

>>> Max (Numbers)

421

>>> min (Numbers)

54

>>> Max (2,3,1)

3

>>> min (1,2,4)

1

>>>

It's all very well understood, the last 2 function parameters are not a sequence, but a number of numbers directly as parameters.

Look at what you are writing, where you need to add to your study. Go on with your study tomorrow and review the knowledge you have learned before studying.


Beginner's mind Continental-----Python Treasure Fourth Chapter

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.