5+3>9-2
Is that 8>7,python is first calculated and then compared
Other arithmetic operations are performed before the boolean operator
Python supports comparisons such as:
aint = 5
0<=aint<=5
while Booleanexpression:
#suite 1
#else:
#suite2
When the while loop Boolean expression is false, the Else statement is entered. Note: A while inside of a break out of the loop also skips the Else statement.
Exercise 2
1.
#-*-coding:gbk-*-
for I in Range (100,1000):
if i%17==0:
print I,
2.
#-coding:gbk-*-
# (a)
Mynum=int (raw_input ("Please input a number:")
sum=0 while
mynum>=1:
Sum+=mynum
mynum-=1
print Sum
# (b) for
I in range (1,mynum+1):
sum=0
kong=0 str=
"" For
J in Range (1,i+1):
Sum+=j
if j==i:
str+=str (j) + "" * ((Mynum-kong) *2-1)
else:
Str+=str (j) + "+"
kong+=1
Print Str, "=", Sum
print
# (c) for
I in range (1,mynum+1):
sum=0
kong=0
str= ""
for J in Range (1,i+1):
sum+=j
if j==i:
str+=str (j) + "" * ((Mynum-kong) *2-1)
else:
Str+=str (j) + " + "
kong+=1
if sum%i==0:
print Str," = ", Sum
Print
3.
#-*-coding:gbk-*-
#method_1 from the back-forward output
moneystr=raw_input ("Input your To convert (NAT):")
Money=int ( MONEYSTR)
If money/29!=0:
if money%29!=0:
print Money%29, "Nat",
money=money/29
if money/17!=0:
if money%17!=0:
print money%17, "tin",
money=money/17
print money, "gallon"
else:
print Money, "Tin Can"
else:
if money!=0:
print Money, "Nat"
#method_2
x=int (moneystr)
if x/(29* !=0: #money数是否大于或等于29 *17 integer times, there are gallons of
print x/(29*17), "gallons",
x=x% (29*17) #得到加仑后的余数
if x/29!=0:# Whether the money number is greater than or equal to 29 integer times, there are tin
print x/29, "tin", #输出锡可
if x%29!=0: #有锡可后的余数
print X%29, "Nat"
else:# Money not enough a tin can, not zero output NAT
if x!=0:
print x, "Nat"
4.
#-*-coding:gbk-*-
#当因数加到一半就大于输入的数字, break
#SumofDivisor预先就设为1, divisor is 2, less one step
#更多详情请参考 "Mason Prime number"
Topnum=int (raw_input ("Please input the upper limit:") thenum=2 deficient=0 abundant=0 while
thenum <=topnum:
#print thenum
divisor=2
sumofdivisor=1 while
divisor<thenum: #不包括数字本身
if Thenum%divisor==0:
sumofdivisor+=divisor
if sumofdivisor>thenum:
break
divisor+=1
If Sumofdivisor==thenum:
print thenum,
elif sumofdivisor>thenum:
abundant+=1
Else:
Deficient+=1
thenum+=1
print
print abundant, "are abundant."
Print deficient, "are deficient."
5.
#-*-coding:gbk-*-
h=raw_input ("Please input your height (m):")
w=raw_input ("Please input your weight (kg):")
h=float (h)
W=float (w)
bmi=w/h**2
print "Your BMI is", BMI
if bmi>=30.0:
print Too Fat "
elif 25.0<=bmi<=29.9:
print" overweight "
elif 18.5<bmi<=24.9:
print" Normal "
elif bmi<=18.5:
print "Too Light"
6.
#-*-coding:gbk-*-
int=raw_input ("Input an Integer:") While not
Int.isdigit ():
int=raw_input ("Error:try Again. Input an integer: "
int=int (int)
print" "The" ", int
7.
#-*-coding:gbk-*-
#1) for
I in range (1,10):
str= "' for J-in
Range (1,i+1):
str+=str (j)
# Print str
print str, "*", 8, "+", I, "=", int (Str) *8+i
#2) for
I in range (1,10):
str= "" for
J in Range (1,i+1):
str+=str (j)
#print str
print str, "*", 9, "+", i+1, "=", int (STR) *9+ (i+1)
#3)
For I in Range (9,1,-1):
str= ""
for J in Range (9,i-1,-1):
str+=str (j)
#print str
print str, "*" , 9, "+", i-2, "=", int (STR) *9+ (i-2)
#4) for
I in range (1,10):
str= "" for
J in Range (1,i+1):
str +=STR (1)
#print str
print str, "*", str, "=", int (str) **2
8.
#-*-coding:gbk-*-
Import Math
mynum=int (raw_input ("Please input a number:")
i=2 while
i<= Math.sqrt (myNum):
if mynum%i==0:
print MyNum, "is not a prime."
Break
i+=1
else:
print MyNum, ' is a prime. '
9.
#-*-coding:gbk-*-
topnum=raw_input ("Upper limit:")
Topnum=int (topnum) for
thenum in range (2,topnum+ 1):
sumofdivisors=0 for
divisor in range (1,thenum):
if thenum%divisor==0:
sumofdivisors+= Divisor
if Sumofdivisors>thenum:
break
if thenum==sumofdivisors:
print Thenum, ' is perfect. '
Elif thenum<sumofdivisors:
print Thenum, "is abundant."
else:
print Thenum, "is deficient."
10.
#-*-coding:gbk-*-
Import Math
# (a) # (b)
expr= "Ax^2+bx+c"
print Expr
a=float (Raw_input ("A:")
b=float (Raw_input ("B:"))
C=float (Raw_input ("C:"))
delta=b**2-4*a*c
if delta==0:
res= (-b+ MATH.SQRT (Delta))/(2*a)
print Expr, "has one root."
Print "It is", res
elif delta>0:
res1= (-b+math.sqrt (Delta))/(2*a)
res2= (-b-math.sqrt (Delta))/(2* A)
print Expr, "has two roots."
Print "They are", Res1,res2
Else:
part1=math.sqrt (Math.fabs (delta))
Part2=-math.sqrt (Math.fabs ( Delta))
x=-b/2*a
y=part1/2*a
res1=x+eval (str (y) + "J")
Res2=x-eval (str (y) + "J")
Print EXPR, "has two imaginary roots."
Print "They are", res1,res2
11.
(a)
is odd but its cubic is not 27--1
is odd and its cubic is 27--3
is even but is less than equal to 10--2
is even but greater than 10--12
myvar=1,3,2,12
(b)
{x|x =2*i+1} (i is an arbitrary integer other than 1, i!=1)
{3}
{x|x=2*i} (i is any integer less than or equal to 5, i<=5)
{x|x=2*i} (i is any integer greater than 5, i>5)
12.
#-*-coding:gbk-*-
str=raw_input ("Input a string:")
s= "for
I in range (len (STR)):
if (i+1)%3==0:s+= Str[i]
print S
13. Slightly
14.is See id, = = See value
15. Insert "I-=1" between row 6th and line 7th, otherwise the dead loop
16. Inserting if...break inside a while loop
17.
#-*-coding:gbk-*-
Import Math
#初级版
#Num =raw_input ("Enter an integer greater than 2:")
#while Num.isdigit ():
# Num=raw_input ("Error.") It is isn't an interger. Try again: ")
#Num =int (Num)
#while num<=2:
# Num=raw_input (" Error. Integer should be greater than 2. Try again: ")
#i =0
#while num>=2:
# num=math.sqrt (Num)
# i+=1
# Print ( "%d:%.3f"% (i,num))
#高级版
#用函数什么的最方便了 =. =
def ifinputvalid (x):
try:
if int (x) >2: return
x
else:
print "Integer less than 2!" Return
0
except:
print "isn't an integer!!!"
return 0
num=raw_input ("Enter a integer greater than 2:") While not
Ifinputvalid (Num):
num=raw_input (" Enter an integer greater than 2: ")
num=int (num)
i=0 while
num>=2:
num=math.sqrt (num)
i+= 1
print ("%d:%.3f"% (i,num))
18. Slightly
(c)
20. Slightly
21.True; False
22. Iterators are a way to access elements within a collection. The iterator object is accessed from the first element of the collection until all the elements have been accessed once and end. e.g. range (); It=iter (LST), It.nest ()
23.continue ignore the statement after the loop, jump directly to the next loop; break, jump straight out of the loop body
24.
#-*-coding:gbk-*-
#检查一个数是否是回文数
def ifpalindrome (x):
l=len (x)
ok=1 for-
i in range (int (L/2.)):
if x[i]!=x[l-i-1]:
ok=0 break return
OK
x=10000 while
x<999999:
if Ifpalindrome ( STR (x) [-4:]):
num=x
y=x+1
if Ifpalindrome (str (y) [-5:]):
more=2 while
more<=10:
Z=y+more
if Ifpalindrome (str (z) [1:-1]):
break
more+=1
#print "Add" and more
k=z+3
if Len (str (k)) ==6:
if Ifpalindrome (str (k)):
print Num,more
x+=1
The actual number of miles to go when #后面的数字指的是 "over 3 miles"
25.
#-*-coding:gbk-*-for
num2 in range (10,100):
num3=num2**2
if 100<=num3<1000 and str (num3) [ -2:]== STR (num2):
print num2
Results: 625
26.
#-*-coding:gbk-*-
s,e,n,d,m,o,r,y=1,0,0,0,1,0,0,0 for
S in range (1,10):
s=s to E in
range (0,10):
e=e
for N in range (0,10):
n=n
to D in range (0,10):
D=d
m=1# logical Inference M must be 1 for
o in range (0,10) :
o=o for
R in range (0,10):
r=r to Y in
range (0,10):
y=y
#print s,e,n,d,m,o,r,y
Send=int (str (S) +str (e) +str (N) +str (D))
more=int (str (m) +str (o) +str (R) +str (e)) Money=int (str (
m) +str (o) + STR (N) +str (E) +str (Y))
if Send+more==money and Len (set ([S,e,n,d,m,o,r,y])) ==8:
print Send, "+", more, "=", Money
Results: 9567 +1085 =10652
Programming topics are later updated ... To be Continued ...