python textbook with exercises

Discover python textbook with exercises, include the articles, news, trends, analysis and practical advice about python textbook with exercises on alibabacloud.com

08:python Basic Exercises

1, while loop implementation output 2-3 + 4-5 + 6 ... + 100 and# use the while loop to implement output 2-3 + 4-5 + 6 ... + 100 and s = = 2 while i : if i % 2 = = 0 :+ = I else: -= I + = 1print (s,i) # operation Result: Wuyi 101the while loop implements output 2-3 + 4-5 + 6 ... + 100 and2, for loop implementation 9 by 9 multiplication table forIinchRange (1,10,1): forJinchRange (1,i+1,1): #\ t represents a tab end= "" to indicate the end of the print objec

Python, looping exercises

From turtle import *fillcolor ("Red") Begin_fill () for I in range (5): Up () Goto (0,-20*i) down () Circle ( 20*i) End_fill ()First, Concentric circleSecond, the sun flowerFrom turtle import *color (' Red ', ' yellow ') the Begin_fill () while True: forward (+) right (in) if ABS (POS ( )) Third, flagFrom turtle Import *setup (640,440,0,0) color ("yellow") bgcolor ("Red") FillColor ("Yellow") def llf_goto (x, y): Up () goto (x, y) down () def Llf_draw (r): Begi

Python Learning-Exercises (6)

"" "Title: Fibonacci sequence. Program analysis: Fibonacci sequence (Fibonacci sequence), also known as the Golden Section, refers to a sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 、......。 All Fibonacci numbers here are counted as "" "Import Functoolsdef Answer1 ():" "" in output 100000 Fibonacci sequence: return: "" "Print (" \ n Output One ", end=" : ") A = 0 print (A, end=", ") b = 1 print (b, end=", ") C = a + B while C   Python Learning-

Python Learning-Exercises (5)

multiple assignments of one variable Aram arr:: Return: "" "Print ( "Output Five", end= ":") if arr[0] > arr[1]: arr[0], arr[1] = arr[1], arr[0] if arr[0] > arr[2]: arr[0], ARR[2] = arr[2], arr[0] if arr[1] > arr[2]: arr[1], arr[2] = arr[2], arr[1] Print (arr) def sort6 (arr): "" "Use the Min function to lose:p ARAM arr:: Return: "" "Print (" Output six ", end=": ") while Len (arr) > 0:a = min (arr) Arr.remove (a) If Len (arr) = = 0:print (a) else:print (A, end= ",") def Answer (): "" "first

Python Learning-Exercises (12)

): Numlist.append (str (i )) Sqrtnum = Int (MATH.SQRT (i)) for J in range (2, Sqrtnum + 1): Findtimes + = 1 if i% J = 0:nu Mlist.pop () Break print (",". Join (Numlist), end= ",") print ("%d"% len (numlist), end= ",") Print ("Total Compared%d "% findtimes" Answer5 () class Answer6: "" "" "" "" "" "" "" "Def __init__ (self):" "Constructor" " "Self.sumcount = 0 self.findtimes = 0 Self.startnum = 102 Self.endnum = + print (" Output Six ", end=": ") def __iter__ (self):" "" Iterator: Return: "" Retu

Python Learning-Dictionary exercises: simple Address Book

': theUserphone = input ('Please enter contact Phone:') +Addressbook[name] =Userphone A Print(AddressBook) the Continue + Else: - Continue $ Else: $Userphone = input ('Please enter contact Phone:') -addressbook[name]=Userphone - Print('Contact added successfully! ') the Print(AddressBook) - ContinueWuyi ifitem = = 3:#Delete a contact the ifNameinchAddressBook: - delAddr

"Python Programming Quick Start" 7.18.2 practical exercises

# -*- coding:utf-8 -*-#strip正则表达式#if 只字符串,不带参数,去首尾空白#else 去除参数text=input("Input a text:")import redef st(text): #原样返回检测 textReg=re.compile(r"^\w+$") #前后或前或后,空白检测 blankReg=re.compile(r‘^\s+(\w+)\s+$|^\s+(\w+)|(\w+)\s+$‘) check_blank=blankReg.search(text) #参数检测 argsReg=re.compile(r‘(\w+)\s+\.*‘) check_args=argsReg.search(text) if textReg.search(text): return(text) elif check_blank: #groups检测,直接通过groups输出 for i in check_blank.groups(): if i: return(i) elif check_args: return(check_args.group(1)) el

"Python Programming Quick Start" 7.18.1 practical exercises

# -*- coding:utf-8 -*-# 7.18.1# 强口令检测# 写一个函数,使用正则表达式,确保传入的口令字符串是强口令# 长度不少于8个字符,同时包含大小写,至少有1个数字import repassd=input("Input your password:")ch_len=re.compile(r‘.{8,}‘)ch_uppercase=re.compile(r‘[A-Z]{1,}‘)ch_case=re.compile(r‘[a-z]{1,}‘)ch_number=re.compile(r‘\d{1,}‘)if ch_len.search(passd): if not ch_case.search(passd) or not ch_uppercase.search(passd) or not ch_number.search(passd) : print("弱密码") else: print("strong enough")else: print("length less then 8")"P

"Python Programming Quick Start" 8.9.3 practical exercises

#!python3# -*- coding:utf-8 -*-# 8.9.3#打开指定路径中所有.txt文件#用户输入正则,将匹配行输出,输出文件名import re,ospat=input("输入要处理的文件夹绝对路径:")lis_dir=os.listdir(pat)lis_txt=[]for x in lis_dir: #取出.txt结尾的文件名,加进列表 if re.search(r‘\.txt$‘,x): lis_txt.append(x)text=input("输入你的自定义正则表达式:")regCom=re.compile(text)#regCom=re.compile(‘.*name.*‘)#循环处理文件for fi in lis_txt: fi=os.path.join(pat,fi) fi_open=open(fi) for fi_line in fi_open.readlines(): fi_reg=regCom.search(fi_line) if fi_reg: print(fi_reg.group()) fi_open.close()print("查找完成"

"Python Programming Quick Start" 8.9.1 practical exercises

#!python3#运行 py mcb.pyw save spam ,剪贴板内容就关键字spam保存#py mcb.pyw spam 加载spam 对应内容到剪贴板#py mcb.pyw list 关键字列表到剪贴板#扩展多重剪贴板:#增加delete "Python Programming Quick Start" 8.9.1 practical exercises

Python Introductory Exercises

1. Use while loop input 1 2 3 4 5 6 8 9 10Code:0 While I : 1 if7: continue Print (i)Results:2. For all numbers of 1-100Code:i = 0sum = 0while iResults:3. All odd numbers in output 1-10:i = 0while I Results:4. All even numbers in the output 1-1000 while One : 1 if 2 0 : print (i)Results:5, Beg 1-2+3-4+5 ... 99 of all numbers of the andi =0J_sum=0O_sum=0sum=0 whileI About: I+=1 ifI2==1: J_sum= J_sum +IifI2==0: O_sum= O_sum +I sum=j_sum-o_sumprint (j_sum) prin

Python Learning-exercises (1)

= [] R = Range (1, 5) for a in r:for B in R:for C in R: If a! = B and a! = C and b! = C:d.append ("%d%d%d"% (a, B, c)) print (d, end= ",") print ("%d"% Len (d)) Answer3 () def answer4 (): "" "in the list use the for and if operations to iterate the data" "" Print ("Answer four", end= ":") arr = [1, 2, 3, 4] NewList = ["%d%d%d"% (I, j, K) for I in Arr. J in arr. K in arr if I! = J and I! = k and J! = K] Print (NewList, End= ",") print ("%d"% len (newlist)) answer4 () def answer5 ():

"Python Exercises" program 14

#title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5. #My method should be more concise than the Internet, but recursion may be slown = input ('Please enter a positive integer:') n=int (n) X=str (n) m= []defZhengchu (n): forIinchRange (2,n+1): ifn% i = =0:k= Int (n/i) m.append (i)returnZhengchu (k)Else: Continuey= (str (x) forXinchm)Print('%s ='X'*'. Join (y)) Zhengchu (n)"Python

"Python Exercises" program 20

# title: A ball from the height of 100 meters of freedom falling, each landing back to the original height of half, and then down, to ask it to fall on the 10th time, the total number of meters? How high is the 10th time rebound?#这个答案可以自定义任何高度和反弹次数, come to the conclusiondef Dist (x,y,s): while y >0: + = (1.5*x )= 0.5*x = y-1 return Dist (x,y,s) Print (x,s) dist (100,10,0)"Python

"Python Exercises" program 3

3, Title: An integer, it plus 100 is a complete square number, plus 168 is a complete square number, what is the number?x+100 = N*nx+100+168 = m * mSo(m+n) * (m-n) =168#title: An integer, which plus 100 is a complete square number, plus 168 is a complete square number, what is the number? I= Range (1,100) forMinchI: forNinchi:k= (m+n) * (M-N)ifK = = 168 andM >N:#print (m,n)x = (n * n-100) y= (M * m-268) ifx = =y:Print(x)"Python

"Python Exercises" program 19

#title: If a number is exactly equal to the sum of its factors, this number is called the "end number". For example, 6=1+2+3. Programming to find all the finished numbers within 1000. defsuoyou (n): #找到所有因子 s= [] forIinchRange (1,n+1): ifn% i = =0:s.append (i) s= S[:len (s)-1] x=0#print (' All silver ', s) forIinchs:x+=IreturnxdefZhengchu (n): #因式分解 forIinchRange (2, n + 1): ifn% i = =0:k= Int (n/i) m.append (i)returnZhengchu (k)Else: Continue ifm = =[]: N= n

"Python Exercises" program 2

2, Title: The bonus awarded by the enterprise according to the profit commission. Profit (I) less than or equal to $100,000, the bonus can be raised by 10%, the profit is higher than $100,000, less than $200,000, the portion of less than 100,000 yuan by 10% commission, higher than the portion of 100,000 yuan, a commission of 7.5%, 200,000 to 400,000, higher than 200,000 yuan, can commission 5% ; Between 400,000 and 600,000 is higher than the portion of 400,000 yuan, can commission 3%, 600,000 to

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 some exercises (beginner)

to program,,,,") the Break the elif "*" in passwd: the print ("The password cannot contain the ' * ' number!") You also have ", Count," chance ", end=") - Continue in Else: the Print ("Password entered incorrectly!") You still have ", count-1, ' Chance!" ', end= ') the count-=1" " About the ####################### Tri-color Ball the the " "print (' Red\tyellow\tblue ') + For red in range (0,4): - For yellow in range (0,4): the For Blue in range (2,7):Bayi if red + yellow + Blue ==8: the prin

Python basics and data type exercises

illegal characters, then replace the illegal characters with the same number of * (such as Wang er), and then add to the user_list, if there is no illegal characters, then added directly to User_list, each time the addition of the successful, the new user name, the password was added.# user_list = [# {' username ': ' Barry ', ' Password ': ' 1234 '},# {' username ': ' Alex ', ' Password ': ' Asdf '},# ]# board = [' Zhang San ', ' Li Xiaoxi ', ' King two leper ']# while 1:# username=input (' use

Total Pages: 15 1 .... 8 9 10 11 12 .... 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.