Practice1 solution
中文版:练习 1 参考答案
Review of variable and operator
- create an variable, name is a, value is 10
a=10- create two variables, names are b and c, values are 9 and 11
b=9; c=11- save value 1 into a variable, print it
a=1
print(a)- save “hello world”, (create a variable whose type is string)
a="hello world"- print the result: Is a greater than b?
print(a>b)Try by yourself 1
- save value 1 to 10
a=[1,2,3,4,5,6,7,8,9,10]- save value 2,3,45,67,11,12,45,78,99 and print them all
a=[2,3,45,67,11,12,45,78,99]
print(a)using “List” to save and process a group of value (refer to 06)
- create a empty list
a=[]- save the group of value: 2,3,45,67,11,12.
print the length of the list.
print how many values save in the list.
a=[2,3,45,67,11,12]
print(len(a)) #to print the length of list a- add a new value 1000 at the end of this list
a.append(1000)- print the 3th value in the list
print(a[2]) # index of a list from 0- delete the 3th value in the list, print the new list
del a[2]- print the result: is the 4th value smaller than the 1st value
print(a[3]<a[0])Try by yourself 2
- print 1,2,3…,10
for i in range(1,11):
print(i)- print the variable a 10 times, print “hello world” 10 times
for i in range(10):
print("hello world")- create an int variable n, calculate 1+2+3+…+n
#using while loop
n=5
i=1
result=0
while i<=n:
result+=i
i+=1
print("cumulation: ", result)using “Loop” to do the same line multiple times (refer to 07)
- print 1,2,3,4,…,10 using “for” or “while”
# using while loop
i=1
while i<=10:
print(i)
i+=1- print the variable a 10 times using “for”
a=1
for i in range(10):
print(a)- print “hello world” 10 times using “while”
i=0
while i<10:
print("hello world")
i+=1- create an int variable n, calculate 1+2+3+…+n, using “for”
n=5
result=0
for i in range(n+1):
result+=i
print("cumulation:", result)Try by youself 3
- create two variables A and B, compare A and B, print the greater value
a=1
b=4
if a>b:
print(a)
else:
print(b)using “if” to do the conditional structure
- create two int variables A and B, compare A and B, print the name and value of the greater variable
a=1
b=4
if a>b:
print("a is greater")
print(a)
else:
print("b is greater")
print(b)- consider the special condition: if A=B, print “A is equel to B” (using elif)
a=1
b=4
if a>b:
print("a is greater")
print(a)
elif a==b:
print("a==b")
else:
print("b is greater")
print(b)- try A=10, B=20
# plz modify the value of a and b by your own for exercise 24 to 26-
try A=100, B=99.5
-
try A=12, B=12