Xuefeng Liao Website-Learn Python Basics (ii)

Source: Internet
Author: User

1. List

"""one of the data types built into Python is the list: lists. A list is an ordered set of elements that can be added and removed at any time. """Classmates= ['Michael','Bob','Mary']Print('Classmates', classmates)Print('len (classmates) =', Len (classmates))Print('classmates[0]=', Classmates[0])Print('classmates[1]=', classmates[1])Print('classmates[-1]=', classmates[-1])Print('classmates[-2]=', classmates[-2])Print('classmates[-3]=', classmates[-3]) Classmates.append ('Mike')#add end of list elementPrint('classmates1', classmates) Classmates.insert (1,'Jack')#element is inserted at the specified locationPrint('Classmates2', classmates) classmates.pop ()#Delete List End elementPrint('Classmates3', classmates) Classmates.pop (1)#deletes the element at the specified positionPrint('Classmates4', classmates) classmates[1] ='Sarah'           #specify location to replace other elementsPrint('classmates5', classmates)

2. Tuple

"""Another ordered list is called a tuple: a tuple. Tuple and list are very similar, but a tuple cannot be modified once initialized"""Classmates= ('Michael','Bob','Tracy')Print('classmates =', classmates)Print('classmates[0]=', Classmates[0])Print('classmates[1]=', classmates[1])Print('classmates[2]=', classmates[2])Print('Classmates[-1]', classmates[-1])#classmates[0] = ' Adam ' # cannot modify element#print (' classmates1 ', classmates)T= (1, 2)#defining a tuple element must determinePrint('t1=', T) t= ()#define an empty tuplePrint('t2=', T) t= (1)#This is 1 numbers, and the definition is not just one element.Print('t3=', T) t= (1,)#define only one elementPrint('t4=', T) t= ('a','b', ['A','B'])#"variable" tuple:T[2][0] ='X't[2][1] ='Y'Print('T5', T)

Xuefeng Liao Website-Learn Python Basics (ii)

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.