python tutorial with exercises

Read about python tutorial with exercises, The latest news, videos, and discussion topics about python tutorial with exercises from alibabacloud.com

Python exercises--Traversal of the list

Exercise One:# Compare two elements in the list, find the different elements and save in list 3L1 = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] L2 = ["Sun", "Mon", "Tue", "Tue", "Thu", "Sat"] L3 = [] #先定义一个空列表 for i in L1: #遍历L1列表中的每一个元素 If I not in L2: #如果L1中的元素不在L2中 l3.append (i) #把元素写入L3中print(L3) #输出L3中的内容Exercise two:# Use the For statement to iterate through the contents of the following list:list = [1, 2, 3, 5, ' SD ', [1, ' se ', ' ds '], ' FS '

Exception handling exercises in Python

.write ('%s%s\n'% (Fileinfo.newtime,self.filename))5 defines a function func (urllist), urllist as a list of URLs, such as: [' http://xxx.com ', ' http:///xxx.com ', ' http://xxxx.cm ' ...]. The functions are as follows:5.1 Open the URL in turn5.2 Print the URL corresponding to the content5.3 If the URL is not open, then the URL is logged to the log file, skip the continued access to the next URL1 #-*-coding:utf-8-*-2 ImportUrllib3 ImportLogging4 5 6Logger =Logging.getlogger ()7HDLR = logging. F

Python function exercises

indexes of the incoming list or tuple object, and return it to the caller as a new list. def f6 (*args): = 0 = [] for in args: +=1 if count%2 ==1: li.append (i) return= f6 ( 11,22,33,44)print(ret)7, write the function, check the length of each value passed into the dictionary, if greater than 2, then only the first two length of the content, and the new content is returned to the caller. defF7 (ARG): RET= {} forKey,valueinchArg.items ():ifLen (value) >

"Python Programming Quick Start" 8.9.2 practical exercises

First.txtThe adjective Panada walked to the NOUN and then VERB. AnerbyNOUN was unaffected by these events.#!python3#-*- coding:utf-8 -*-#8.9.2疯狂填词游戏#用户自定义词语,修改打开文件中的ADJECTIVE、NOUN、VERBimport ref=open(‘first.txt‘,‘r+‘)files=f.read()print(files)f.close()changelist=[‘ADJECTIVE‘,‘NOUN‘,‘VERB‘]for i in changelist: change_reg=re.compile(r‘%s‘ % i) text=input("Enter a %s :" % i) files=change_reg.sub(text,files)fi_2=open(‘third.txt‘,‘w‘)fi_2.write(files)fi_2.close()"

2nd Python data types exercises & assignments

): print ("%s.%s%s "% (Index,product[0], PRODUCT[1]))14. Use the list in the title, write a loop, constantly ask the user what to buy, the user selects a product label, the corresponding product added to the shopping cart, the end user input Q exit, print the shopping cart list of items.Products = [[' Iphone8 ', 6888], [' MacPro ', 14800], [' Xiaomi 6 ', 2499], [' Coffee ', 31],[' book ', 80],[' Nike shoes ', 799]]shopping_ Cart = [] # defines an empty shopping cart Exit_flag = false# while T

Python read and Write Excel exercises _ remove garbled lines in Excel and add columns

Requirements:Put the data inside the App_student.xls,1, if this line of data has garbled (and contains?), then delete2, plus a column, whether graduation3, if the class is a Scorpio, graduation this column is written as a graduate4, the other classes are written not graduatedRaw DATA:implementation:ImportXlrd,xlwtexcel_name='App_student.xls'defDelete_messy_code (Excel_name):#Remove garbled charactersBook =Xlrd.open_workbook (excel_name) sheet=book.sheet_by_index (0) data= [] forIinchRange (s

"Python Exercises" program 12

#title: Determine the number of primes between 101-200 and the output of all primes. #the method of judging primes: to remove 2 to sqrt (this number) with a number, if divisible, indicates that the number is not a prime, and vice versa. fromMathImportSqrtm=0n= 1 forIinchRange (101,201): K= Int (sqrt (i+1)) forXinchRange (2,k+1): ifi%x = =0:n=0 Break ifn = = 1: Print(i) M+ = 1N= 1Print('Total is %s'% m)"Python

"Python Exercises" program 13

# Title: Print out all the "daffodils", the so-called "Narcissus number" refers to a three-digit number, its number of cubes and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three times the square +5 of the three +3 Times Square. for in range (100,1000): = int (i/100) = Int ((i-x*100)/10) = i-x*100-y*10 if i = = (x**3+y**3+z**3) :print(' C17>%s is the number of Narcissus '%i ')"Python

"Python Exercises" program 1

# title: there are 1, 2, 3, 4 digits, how many different and non-repeating numbers of three digits can be composed? How much are they? count = 0 for i in range (1,5 for J in for k in range (1,5 if i! = J and J! = k and i!= K: print (i*100 + j *10 + K) Count + = 1print (count) "Python Exercises" program 1

Python path--DAY13---Functions--ternary expressions, recursion, anonymous functions, built-in functions-----exercises

'name': i[0],7 'Sex': i[1],8 ' Age': Int (i[2]),9 'Salary': Int (i[3])Ten } One data.append (DIC) A - #[{' Name ': ' Egon ', ' sex ': ' Male ', ' age ': ' Salary ' : - #{' name ': ' Alex ', ' sex ': ' Male ', ' age ': $, ' salary ': 30000}, \ the #{' name ': ' Wupeiqi ', ' sex ': ' Female ', ' age ': +, ' salary ': 20000},\ - #{' name ': ' Yuanhao ', ' sex ': ' Female ', ' age ': +, ' salary ': 10000}] - -Res=max (data,key=Lambdax:x['Sal

Python First class exercises

1. Use while loop input 1 2 3 4 5 6 8 9 10num = 1 while num : print(num) = num + 1 while num : c8/>= num + 1 print(num)2. For all numbers of 1-100num = 1= 0 while num : = num + count = num + 1print(c Ount)3. All odd numbers in output 1-100num = 1 while num : = num% 2 if a! = 0 :print" c9> is odd ") Else: pass # print (num, "is even") num = num + 14. All even numbers in output 1-100num = 1 while num : = num% 2 if a! = 0 :pass #

Python OJ from getting started to getting started basic exercises 10 questions

contains uppercase and lowercase letters and numbers, randomly appear. User input Verification code is not case-sensitive, as long as the characters appear in the correct order to pass validation. Please write a program to complete the verification Code matching verification, assuming that the current display of the verification code is ' qs2x '. If the user enters the correct code, the output "Verification code is correct", the input error output "code error, please re-enter". s = "Qs2

Python exercises 1

Title: Enter a certain day of the year, judging the day is the first of the year? Analysis: Taking March 5 as an example, the first two months should be added together, and then add 5 days, that is, the first day of the year, specialcase, Leap year and enter the month greater than 3 o'clock need to consider more than one day. datetype= input (' Please enter month and day format: yyyymmdd: ') y = Int (datetype[:4]) m = Int (Datetype[5:6]) d = Int (datetype[7:]) rn = Falseif (y%1 xx = = 0 and y%40

Old boy Python basic knowledge exercises (I.)

. Please loop out all the keys and valuedic={' K1 ': "v1", "K2": "V2", "K3": [11,22,33]}For i,v in Dic.items (): Print (I,V)D. Add a key-value pair in the dictionary, "K4": "V4", and output the added dictionarydic={' K1 ': "v1", "K2": "V2", "K3": [11,22,33]}dic[' K4 ']= ' v4 'Print (DIC)E. In the modified dictionary, the value of "K1" corresponds to "Alex", Output the modified dictionarydic={' K1 ': "v1", "K2": "V2", "K3": [11,22,33]}dic[' K1 ']= ' Alex 'Print (DIC)F. Please append an element

Python exercises returns the subscript of the second small element in the list

# Third question: Returns the subscript of the second small element in the list# 1. The parameter is a list, the elements are all integers# 2. Returns the subscript of the second small elementdef seconde_min (LT):n = len (LT)if lt[0]Yixiao = lt[0]Erxiao=lt[1]Else:Yixiao=lt[1]Erxiao=lt[0]For i in range (2,n):if Lt[i]Erxiao=yixiaoYixiao=lt[i]Elif YixiaoErxiao=lt[i]Return Lt.index (Erxiao)lt=[3,2,4,5,6,1,6]Print (Seconde_min (LT))Python

Python Learning-Exercises (13)

) + Pow (int (s[1]), 3) + POW (int (s[2]), 3) = = I:print (i, end= ",") Answer3 () def answer4 (): "" "The collar uses the Divmod function to calculate : Return: "" " Print ("\ n Output four", end= ":") for I in range (+): M = i sumnum = 0 while m > 0: M, n = Divmod (M, ten) Sumnum + = n * * 3 if Sumnum = = I:print (i, end= ",") Answer4 () def answer5 ( ): "" is implemented using the list for if usage: return: "" "Print (" \ n Output five ", end=": ") L = [I for I in range (+) if (I//1 * *

Python File processing Exercises

1, Analog CP command Copy file (image, video) to other pathsWithopen (' a.txt ', ' RB ') asf,open (' B.txt ', ' WB ') aswrite: #选择rb模式, support all copies, the corresponding write mode is WB, directly processing is the bytes type for lineinf: #遍历源文件的每一行 write.write (line) #写入新文件write中 However, this does not support the user to pass parameters, so the final code is as follows:Import Sysif len (SYS.ARGV) 2, Python simulation tail command, displ

Python Learning-Exercises (8)

"" "Title: Output 9*9 multiplication table. "" "Def Answer1 ():" " in the most common double loop to output : return:" "" print ("Output One:") for I in range (1): For j in range (1, i + 1): print ("%d x%d =%2s"% (i, J, I * j), end= " ") print () answer1 () def answer2 (): The "" " while loop, with another formatting note for print percent (%) formatted output, sets the output format for the flag with%, and the fomate format is formatted with a colo

Brother Lian Learning python----form production exercises

Form ExerciseRegistration Information Form  Brother Lian Learning python----form production exercises

Python Data type Exercises

":"Mouse"," Price": 10}, {"name":"Yacht"," Price": 20}, {"name":"Beauty"," Price": 998},]Print("enter 0 to buy a computer, enter 1 to buy a mouse, enter 2 to buy a yacht, enter 3 to buy a beauty, enter another value, settle the cart") Li=[]total= Input ("Please enter the total amount:") whileTrue:num= Input ("Enter the serial number you want to purchase:") ifint (num) = =0:li.append (goods[0][' Price']) elifint (num) = = 1: Li.append (goods[1][' Price']) elifint (num) = = 2: Li.ap

Total Pages: 15 1 .... 11 12 13 14 15 Go to: Go

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.