Python Basics Tutorial

Source: Internet
Author: User
Learn python basic syntax try to write a simple Python program

1 count = 0 2 age_to_guess = 3  4 while count<3:5     age_guessed = Int (Input ("Age:")) 6     if age_guessed = = A Ge_to_guess:7         Print ("Congratulations") 8 break         9     elif age_guessed > Age_to_guess:10         print ("Too Big ") One     else:12         print (" Too small ")     count = Count +     if count = = 3:15         confirm_input = input (" Do you want to play more? ")         if confirm_input! = "N": +             count = 0

1 age_to_guess = 232 age_guessed = Int (Input ("Age:")) 3 4 if age_guessed = = age_to_guess:5     print ("Congratulations") 6 Elif age_guessed > Age_to_guess:7     print ("Too big") 8 else:9     Print ("Too small")

1 Import sys 2  3 "4 print (Sys.path) #打印环境变量 5 print (SYS.ARGV) #相对路径 can be appended with parameter 6" ' 7  8 #操作系统模块 9 Import os10 11 Cmd_result = Os.system ("dir") #执行命令 do not save results for page print (cmd_result) #输出0 why? The Os.system is output directly on the frequency screen and does not return to Cmd_result_internal_storage = Os.popen ("dir") #打开的是一个内存地址15 print (Cmd_result_internal_ Storage) Cmd_result_content = Os.popen ("dir"). Read () #读操作17 print (cmd_result_content) os.mkdir ("New_dir") # Create a catalog msg = "I love Beijing Tian ' an gate" msg_bytes= msg.encode ("Utf-8") in print (msg_bytes), msg_str = Msg_bytes.decode ("Utf-8 ") Print (MSG_STR)

Python list usage (add and revise):

 names = [,,,, (Names[2 (Names[1:3]) (Names[2 (Names[:-1 (names[-3:-1)) n Ames.append () Names.insert (2, Names.insert (4, names[2] = Names.remove (names[3 names.pop () (Names.ind Ex ((Names[names.index ((Names.count (Names.reverse () names.sort () Names2 = [1, 2, 3, 4 (names) execution result: 
1 D:\python\python\python.exe d:/xampp/htdocs/python/day2/list_cut.py 2 num1 3 num3 4 [' num2 ', ' num3 '] 5 [' num3 ', ' num4 ', ' NUM3 '] 6 [' Num1 ', ' num2 ', ' num3 ', ' num4 '] 7 [' num3 ', ' num4 '] 8 [' num1 ', ' num2 ', ' num3 ', ' num4 ', ' num3 ', ' num10086 '] 9 [' N Um1 ', ' num2 ', ' num_ ', ' num3 ', ' num4 ', ' num3 ', ' num10086 ']10 [' num1 ', ' num2 ', ' num_ ', ' num3 ', ' _num ', ' num4 ', ' num3 ', ' NUM1 ' 0086 ']11 [' num1 ', ' num2 ', ' change2 ', ' num3 ', ' _num ', ' num4 ', ' num3 ', ' num10086 ']12 [' num1 ', ' num2 ', ' num3 ', ' _num ', ' num4 ' , ' num3 ', ' num10086 ']13 [' num1 ', ' num2 ', ' num3 ', ' num4 ', ' num3 ', ' num10086 ']14 [' num1 ', ' num2 ', ' num3 ', ' num4 ', ' num3 ']15 216 num317 218 [' num3 ', ' num4 ', ' num3 ', ' num2 ', ' num1 ']19 [' num1 ', ' num2 ', ' num3 ', ' num3 ', ' num4 ']20 [' num1 ', ' num2 ', ' num 3 ', ' num3 ', ' num4 ', 1, 2, 3, 4]21 []22 23 process has ended, exit code 0

Python list usage (replication loop):

 1 #author F 2 Import copy 3 4 5 #copy list Shallow copy 6 7 names = ["Name1", "name2", "Name3", ["Alibaba", "Blili"], "name4", "NA Me5 "] 8 Names2 = names.copy () #浅copy: Copy only first-tier address reason: pointer address of second-level list copied 9 print (names) print (names2) one-page names[1] =" Name_chan GE "# Print (names) # print (names2) names[3][1] =" Blili "(names) print (names2) + [' name1 ', ' Nam E2 ', ' Name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' name5 ']22 [' name1 ', ' name2 ', ' name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' name5 ' ]23 [' name1 ', ' name_change ', ' name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' name5 ']24 [' name1 ', ' name2 ', ' name3 ', [' Alibaba ', ' B LILI '], ' name4 ', ' name5 ']25 ' 26 27 # = List copy of names = ["Name1", "name2", "Name3", ["Alibaba", "Blili"], "name4", "Name5" ]29 names2 = names30 print (names) to print (Names2) + names[1] = "Name_change" # Print (names) # print (names2)-Notoginseng NA MES[3][1] = "Blili" (names) in print (Names2) + "" [' name1 ', ' name2 ', ' name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' Nam E5 ']42 [' name1 ', ' name2 ', ' Name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' name5 ']43 [' name1 ', ' name_change ', ' name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' Nam E5 ']44 [' name1 ', ' name_change ', ' name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' name5 ']45 ' ' 46 47 # Module Copy names = ["Name1", "N Ame2 "," Name3 ", [" Alibaba "," Blili "]," name4 "," name5 "]49 names2 = copy.deepcopy (names) #深copy: Takes up two separate memory spaces with caution with the print (n Ames) (NAMES2) [names[1] = "Name_change" (names) # print (Names2), names[3][1] = "Blili" Print ( names) Names2 [' name1 ', ' name2 ', ' name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' name5 ']62 [' name1 ', ' name2 ', ' n Ame3 ', [' Alibaba ', ' Blili '], ' name4 ', ' name5 ']63 [' name1 ', ' name_change ', ' name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' name5 ' ]64 [' name1 ', ' name2 ', ' name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' name5 ']65 ' ' #循环68 ' for I in names:69 print (i) 70 Print (names[0:-1:1]) #[' name1 ', ' name_change ', ' name3 ', [' Alibaba ', ' Blili '], ' Name4 ']72 print (names[0::1]) #[' name1 ', ' name_change ', ' Name3 ', [' Alibaba ', ' Blili '], ' name4 ', ' name5 '] 

Tuple Shallow copy Supplement

1 person = ["Name", ["Saving", 123]] 2 #浅copy的三种实现方式 3 "4 P1 = copy.copy (person) 5 P2 = person[:] 6 p3 = list (person) 7 "' 8 P1 = person[:] 9 P2 = person[:]10 one p1[0] = ' ale1 ' p2[0] = ' Feng ' p1[1][1] = 5015 print (p1) #联合账号的用法17 1 8 #元组19 names = (' 231 ', ' 21314 ') 20 # tuples only index and Count methods

 1 what_he_does = ' plays ' 2 his_instrument = ' Guitar ' 3 his_name = ' Robert jhonson ' 4 Artist_intro = his_name + what_he_ Does + his_instrument 5 6 print (Artist_intro) 7 print (Type (what_he_does)) 8 9 num = 111 string = ' 1 ' Print (Num+int ( string)) * Word = ' word ' words = word*316 print (words) + Word = ' word ' num = + ASTR = Word * (len (word)-num) Print (ASTR), Word = ' friends ' find_the_evil_in_your_friends = \25 word[0] + word[2:4] + word[-3:-1]26 print (find_ The_evil_in_your_friends) Tel = ' 135-8866-9555 ' Hide_tel = Tel.replace (Tel[:9], ' * ' *9)-print (Hide_tel)-Sear  ch = ' 234 ' num_a = ' 135-8855-2345 ' num_b = ' 123-2346-2346 ' Print (search + ' are at ' + str (num_a.find (search)) + ' to ' + str (num_a.find (search) +len (search)) + ' of Num_a ') (search + ' at ' + str (num_b.find search) + ' to ' + st R (Num_b.find (search) +len (search)) + ' of Num_b ') Notoginseng print (' {} A word she can get what she {} for. ') Format (' With ', ' came '))-print (' {Prepositon} A word she can get what she {verb} for. Format (prepositon= ' with ', verb= ' came '))-print (' {0} A word she can get what she {1} for. Format (' With ', ' came ')) * # city = input ("Write down the name of City:") # URL = "http://api.baidu.com/wather?city={}" ". Format (city) # Print (URL) f_c def (c): + F = c*9/5 + 3250 return str (f) + ' °f ' Fahrenheit = F_c (35 ) Print (Fahrenheit)

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.