Python Basics (6)--Practice

Source: Internet
Author: User

1, Show list all elements

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650; src=/e/ U261/themes/default/images/spacer.gif "style=" border:1px solid rgb (221,221,221); Background-image:url ("/e/u261/ Lang/zh-cn/images/localimage.png "); background-position:50% 50%;background-repeat:no-repeat;" alt= "spacer.gif"/ >650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/EF/wKiom1bzTvvSooNiAABSmw7i1DQ200.png "title=" 1. PNG "alt=" Wkiom1bztvvsooniaabsmw7i1dq200.png "/>

Reverse Print

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m02/7d/eb/wkiol1bzt7sswyruaabivy9pp5w529.png "title=" 2.png "alt=" Wkiol1bzt7sswyruaabivy9pp5w529.png "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

Description

The Len () function counts the number of sequence strings.

The pop () function defaults to printing from the last step of the sequence.

L1[0] means the No. 0 element is printed; L1.pop (0) pops up, and the No. 0 element pops up and prints. -1 represents the index of the largest list.

Count = 0

While L1:

If l1[count]%2 = = 0:

L1[count]


2, combining sequences L1 and L2 into dictionaries

L1 = [0,1,2,3,4]

L2 = ["Sun", "Mon", "Tue", "Wed", "Thu"]

D1 = {}

Count = 0

While Count < Len (L1):

D1[l1[count]] = L2[count]

Count + = 1

Print D1

3,1 to 100 and

sum = 0

For I in Xrange (1,101): #或者range, Range is a sequence in memory, then participates in the statement calculation, and xrange is used to generate a

sum + = i

Print sum

Bash Shell:

#!/bin/bash

Sum=0

For i in ' SEQ 1 100 '

Do

sum=$ ((sum+i))

Done

Echo $sum

4, user input added to the set up list, q exit

Test = []

While True: #死循环

x = Raw_input (' Eenter an entry: ') #提示输入, prompt character

if x = = ' Q ' or x = = ' Quit ':

Break

Else

Test.append (x) #添加列表的函数.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

5, display the k,v in the dictionary individually

D1 = {' A ': 1, ' B ': 2, ' C ': 3}

#d1. Items () #遍历d1

#Out [A]: [(' A ', 1), (' C ', 3), (' B ', 2)]

for (K,V) in Di.items (): #使用元组的方式定义变量.

Print K,v

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

6, the list content index to the base of the print out.

L1 = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

For I in range (1,len (L1), 2): #range (1,len (L1), 2) starting from 1, to the length of the list, step 2

Print L1[i]

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

7,Will belong to the list l1=["Sun","Mon","Tue"," Wed ","Thu"," Fri ","Sat"], but does not belong to the list l2=[" Sun "," Mon "," Tue "," Thu "," Sat "all elements defined as a new list L3;


L3 = []

For I in range (0,len (L1)): #使用索引数字序列.

If L1[i] not in L2:

L3.append (L1[i])


L3 = []

For I in L1:

If I not in L2:

L3.append (i)

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang /zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

8, known list namelist=[' stu1 ', ' stu2 ', ' stu3 ', ' stu4 ', ' stu5 ', ' stu6 ', ' stu7 '], delete list removelist=[' Stu3 ', ' stu7 ', ' stu9 '] , remove each element that belongs to the Removelist list from the NameList (removelist, but not the namelist ignore);

For I in Removelist:

If I in NameList:

Namelist.remove (i) #remove () function deletes an element. Pop is an index sequence that pops up

Namelist.pop (4)

' Stu6 '

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

, 9, parallel iteration

L1 = [A]

L2 = [' x ', ' y ', ' Z ']

Zip (l1,l2)

[(1, ' X '), (2, ' Y '), (3, ' Z ')]


Python Basics (6)--Practice

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.