Python Learning Code

Source: Internet
Author: User

#Encoding=utf-8ImportSYSImport Time#I want to enter information from the keyboardName ="'    #assignment is emptyPrintName#output NULLName=raw_input ("Please enter your name:")Print 'Hello:'+name#slicing a stringST1 ='HelloWorld'PrintSt1[0]PrintSt1[0:5]#concatenation of StringsSt2 ='I Love Python'Printst1+St2#formatted output of string%s%d%f%cST3 ='Lilei'St4= 18Print('My name is%s and I\ ' am%d'%(ST3,ST4))#common functions for strings#Len () gets the lengthPrintst3+'length', Len (ST3)#strip () remove spaces at both endsST5 ='My name is'Print 'remove spaces at both ends', St5.strip ()#Lstrip () Remove left spacePrint 'Remove left space', St5.lstrip ()#Rstrip () Remove the right spacePrint 'Remove Right space', St5.rstrip ()#Replace the middle spacePrint 'Replace the middle space', St5.replace (' ',"')#Replace () the number of substitutions, eg: replace 2 timesSt6 ='AAABBCC'PrintSt6.replace ('a','b', 2)#Upper () uppercase of the stringPrintSt6.upper ()#lower () lowercase of stringPrintSt6.lower ()#Find () string lookup, returns the index value foundPrintSt6.find ('C'),"\ n"# ---------------------------------------Print 'list: Ordered---------------------------------------'#ListLis_1 = [12, 45.55,'adaded', [2, 3,"DFA"]]PrintLen (lis_1)#list slicesvar_1 =Lis_1[0]Printvar_1var_2= Lis_1[2:3]PrintVar_2lis_1[0]='12 has been modified'PrintLis_1lis_1= [12,124,1241]lis_1.append ('name')PrintLis_1lis_1.insert (2,'Insert')Print 'Inserting Data', Lis_1var_pop=lis_1.pop (2)Print 'the data with a popup index of 2 is:', Var_pop,'Original list', Lis_1lis_1.remove (12)Print 'after the list is deleted:', Lis_1lis_1.reverse ()Print 'inverted list, content base change', Lis_1Print 'values are reversed and values are taken.', Lis_1[::-1],"\ n"# ---------------------------------------Print 'tuples: Ordered---------------------------------------'Tup1= (12,45.55,'3434dfrte', [23, 55,'fet344dr45'])PrintTup1[1]PrintTup1[3][1]PrintTup1[1:3]"""the similarities and differences between tuples and lists: 1. Common: Both are ordered data types, elements can be of any type 2. Different points: A single element in a list can repeat an assignment for a single element of a tuple that does not allow duplicate assignment"""#how to implement adding an element to a tuple, first convert the tuple to a list, manipulate the list, and then convert the Narimoto groupReplace_tuple =list (tup1) Replace_tuple.insert (2,'Zhangsan') New_tup1=tuple (replace_tuple)PrintNew_tup1#Dictionary: An unordered data type with a single element made up of Key:valueDic1 = {'name':'Lilei',' Age': 18}PrintLen (DIC1)PrintDic1#keys in the dictionary are not allowed to be duplicated, not allowed to be modified, value can be modifieddic1['name']='Xiaohong'#add an element to the dictionaryDic1.update ({'class': 101,'Count': 56})PrintDic1#the element with the key name in the popup dictionaryPrintDic1.pop ('name')#returns all keys in the dictionaryPrintDic1.keys ()#returns all the value in the dictionaryPrintdic1.values ()#empty The elements in the dictionarydic1.clear ()Print "\ n"# ---------------------------------------Print 'data type Time---------------------------------------'#The number of seconds from January 1, 1970 00:00:00 to the current system datePrinttime.time ()#local timePrinttime.localtime ()#output dates in a specified formatPrintTime.strftime ('%y-%m-%d%h:%m:%s', Time.localtime ())#output dates in a specified formatPrintTime.strptime ("1992-8-20",'%y-%m-%d')

Python Learning Code

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.