AITC Wiki

Practice1 solution

练习 1 参考答案

Practice1 solution

中文版:练习 1 参考答案

Review of variable and operator

  1. create an variable, name is a, value is 10
a=10
  1. create two variables, names are b and c, values are 9 and 11
b=9; c=11
  1. save value 1 into a variable, print it
a=1
print(a)
  1. save “hello world”, (create a variable whose type is string)
a="hello world"
  1. print the result: Is a greater than b?
print(a>b)

Try by yourself 1

  1. save value 1 to 10
a=[1,2,3,4,5,6,7,8,9,10]
  1. 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)

  1. create a empty list
a=[]
  1. 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
  1. add a new value 1000 at the end of this list
a.append(1000)
  1. print the 3th value in the list
print(a[2]) # index of a list from 0
  1. delete the 3th value in the list, print the new list
del a[2]
  1. print the result: is the 4th value smaller than the 1st value
print(a[3]<a[0])

Try by yourself 2

  1. print 1,2,3…,10
for i in range(1,11):
 print(i)
  1. print the variable a 10 times, print “hello world” 10 times
for i in range(10):
 print("hello world")
  1. 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)

  1. print 1,2,3,4,…,10 using “for” or “while”
# using while loop
i=1
while i<=10:
 print(i)
 i+=1
  1. print the variable a 10 times using “for”
a=1
for i in range(10):
 print(a)
  1. print “hello world” 10 times using “while”
i=0
while i<10:
 print("hello world")
 i+=1
  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

  1. 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

  1. 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)
  1. 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)
  1. try A=10, B=20
# plz modify the value of a and b by your own for exercise 24 to 26
  1. try A=100, B=99.5

  2. try A=12, B=12