Basic exercises for getting started with Python (54 questions)
Table of contents
Section 1, Python Basic Keywords and Grammar
1. Use single quotation marks to enclose the content that needs to be printed out
2. Use double quotes to enclose the content that needs to be printed out
3. Use triple quotation marks to enclose the content that needs to be printed out
44. Take the maximum value, minimum value, length, and sum of the list
45. Count the number of occurrences of objects in the list
46. Return the first pointer of the list
47. Return the first pointer in the list, specify where to start searching
48. Return list pointer, specify where to start searching
51. Add an object at the end of the list
52. Delete an object in the list
53. Delete the object at the specified position in the list
Recently I heard the Chinese version of a Korean song "Alive", the lyrics are well written. As a cross-cultural and cross-language art form, music can transcend language and cultural barriers and convey emotions and ideas. Even if we don't understand the specific lyrics, the emotion and melody expressed by the music itself can resonate and move us. Some music can even become cultural heritage across time and space, widely sung and appreciated by people. Therefore, music has no borders. It can connect people from different countries and cultures, and let people feel common emotions and beauty in the world of music. The Chinese version of "Alive" The vicissitudes of life will never forget that belief, if our hearts are still there, we will stand up ...... There are always ups and downs in life, we have cried and laughed and we have all experienced it. ..... Sober in adversity 2023.6.11 |
Section 1, Python Basic Keywords and Grammar
The main primitive data types in python are numbers (integer and floating point), booleans and strings
Strings can be enclosed in 'string', "string", """string"""
1. Use single quotation marks to enclose the content that needs to be printed out
print('不要对身边的人太过苛责,世上的人有千千万万,彼此能够在人海中相遇,是修来的缘分。遇见了,请包容彼此的不完美。')
operation result:
Don't be too harsh on the people around you. There are tens of thousands of people in the world, and being able to meet each other in the sea of people is due to fate. When we met, please tolerate each other's imperfections.
2. Use double quotes to enclose the content that needs to be printed out
print("不要对身边的人太过苛责,世上的人有千千万万,彼此能够在人海中相遇,是修来的缘分。遇见了,请包容彼此的不完美。")
operation result:
Don't be too harsh on the people around you. There are tens of thousands of people in the world, and being able to meet each other in the sea of people is due to fate. When we met, please tolerate each other's imperfections.
3. Use triple quotation marks to enclose the content that needs to be printed out
print("""不要对身边的人太过苛责,世上的人有千千万万,彼此能够在人海中相遇,是修来的缘分。遇见了,请包容彼此的不完美。""")
operation result:
Don't be too harsh on the people around you. There are tens of thousands of people in the world, and being able to meet each other in the sea of people is due to fate. When we met, please tolerate each other's imperfections.
There are a number of special escape character sequences
\t (tab)
\n (newline)
4. Use of \n (line break)
print("保持应有的信任 \n 猜忌会让所有的关心变成别有用心。")
operation result:
maintain due trust
Suspicion will turn all concerns into ulterior motives.
5. Use of \t (tab)
print("保持应有的信任 \t 猜忌会让所有的关心变成别有用心。")
operation result:
Maintain due trust Suspicion will turn all concerns into ulterior motives.
6. The use of \
print('I\'m a big big girl 我是个大女孩')
operation result:
I'm a big big girl
7. The use of \
print("I can see the first leaf falling \"我能看见一片枫叶落下\" it\'s all yellow and nice \'那是多麽的金黄而美好\'")
operation result:
I can see the first leaf falling "I can see a maple leaf fall" it's all yellow and nice 'that's so golden and nice'
Create a variable to store the information to be referenced, call it in the program, and print it out.
8. Pass variables
Var1= 'when I open my eyes 当我睁开眼时'
print(Var1)
operation result:
when I open my eyes
Strings are a special type of python. As objects, within classes, methods on string objects can be invoked using the .methodname() notation. The string class is available by default in python, so no import statement is required to use the object interface for strings.
9. String manipulation
name = 'Adversity awake'
print(name.lower())
print(name.upper())
print(name.title())
operation result:
adversity awake
ADVERSITY AWAKE
Adversity Awake
10. String manipulation
name = 'Adversity awake'
print(name.lower)
print(name.upper)
print(name.title)
operation result:
<built-in method lower of str object at 0x7f7018228cb0>
<built-in method upper of str object at 0x7f7018228cb0>
<built-in method title of str object at 0x7f7018228cb0>
11. Split string
name = 'Adversity awake'
name.split(' ')
print(name.split(' '))
operation result:
['Adversity', 'awake']
12. Split string
name = 'Adversity awake'
a=name.split(' ')
print(a)
operation result:
['Adversity', 'awake']
13. String concatenation
a = 'Adversity awake'
name = ' '.join(a)
print(a)
print(name)
operation result:
Adversity awake
A d v e r s i t y a w a k e
14. String concatenation
print("Adversity" + " " + "awake")
operation result:
Adversity awake
15. Number string splicing
print("0" + "1")
operation result:
01
16. Number string splicing
print("0" * 8)
operation result:
00000000
Basic Mathematics
There are four number types: plain integers, long integers, floating point numbers, and complex numbers.
Booleans are a subtype of ordinary integers.
operator | describe |
+ | add - add two objects |
- | Subtract - get a negative number or subtract one number from another |
* | multiply - multiply two numbers or return a string repeated several times |
/ | Divide - x divided by y |
% | Modulo - returns the remainder of the division |
** | power - returns x raised to the power of y |
// | Round and divide - returns the integer part of the quotient (rounded down) |
17、数学运算
print(1+2+3+4+5)
运行结果:
15
18、数学运算
print(180-2.0)
运行结果:
178.0
19、数学运算
print(180-2)
运行结果:
178
20、数学运算
print(180/2 )
运行结果:
90.0
21、数学运算
print(180.0/2)
运行结果:
90.0
22、数学运算
print(2*3)
运行结果:
6
23、数学运算
print(2**3)
运行结果:
8
24、数学运算
print(9%3)
运行结果:
0
比较操作符 | |
< | 小于 |
<= | 小于或等于 |
> | 大于 |
>= | 大于或等于 |
== | 等于 |
!= | 不等于 |
if 语句
检查。。。是否为True,如果是,则执行此操作。如果它不是True(False),则不执行
25、if 语句
numb = 8
if numb == 8:
print(numb)
运行结果:
8
26、if 语句
numb = 8
if numb > 10:
print(numb)
else:
print('numb小于10')
运行结果:
numb小于10
27、if 语句
numb = 8
if numb % 8 == 0:
print("余数=0")
运行结果:
余数=0
28、if 语句
numb = 10
if numb % 5 == 0:
print("10%5== 0")
运行结果:
10%5== 0
29、if 语句
if True:
print("I will be right here waiting for you 为你此地守候");
运行结果:
I will be right here waiting for you 为你此地守候
逻辑操作符 | 描 述 |
and | 如果两个操作数均为True,则condition变为True. |
or | 如果两个操作数中的任何一个为True,则condition变为True. |
not | 用于反转逻辑(不是False变为True,而不是True变为False |
30、逻辑操作
num = 2
num > 0 and num < 20
运行结果:
True
31、逻辑操作
num = 2
num > 0 or num > 20
运行结果:
True
32、逻辑操作
num = 10
not num < 20
运行结果:
False
33、逻辑操作
num = 1
if num > 8 :
print("今天是星期八,你信吗?")
else:
print("今天是星期",num)
运行结果:
今天是星期 1
34、逻辑操作
num = 10
if num > 8 :
print("今天是星期八,你信吗?")
else:
print("今天是星期",num)
运行结果:
今天是星期八,你信吗?
35、if...else
num = 5
if num % 2 == 0:
print("您的整数是偶数")
else:
print("你的整数是奇数")
运行结果:
你的整数是奇数
elif 语句必须在if语句之后。
elif语句语句允许您检查True的多个表达式,并在其中一个条件求值为True时立即执行代码块。
与else类似,elif语句是可选的。但是最多只能有一个语句,if后面可以有任意数量的elif语句。
36、if...elif 语句
num = 102
if num > 100:
print('num 大于100')
elif num == 73:
print('num = 73')
else:
print('num小于100,但有很多可能')
运行结果:
num 大于100
37、if...elif 语句
dayvalue = 5
if dayvalue == 1:
print('今天是星期{}. 天晴朗那花儿朵朵绽放!'.format(dayvalue))
elif dayvalue == 2:
print('今天是星期{}. 闻花香我想起年幼时光!'.format(dayvalue))
elif dayvalue == 3:
print('今天是星期{}. 我今天陪爸爸带着全家去玩耍!'.format(dayvalue))
elif dayvalue == 4:
print('今天是星期{}. 池塘边荷叶下躲着一只小青蛙!'.format(dayvalue))
elif dayvalue == 5:
print('今天是星期{}. 车窗外雨好大青蛙一个人在家!'.format(dayvalue))
elif dayvalue == 6:
print('今天是星期{}. 山青青水蓝蓝看日出看云海!'.format(dayvalue))
elif dayvalue == 7:
print('今天是星期{}. 彩虹桥路弯弯牵着手儿不怕摔!'.format(dayvalue))
else:
print('没有上面的条件 (if elif) ,难道是星期八?')
运行结果:
今天是星期5. 车窗外雨好大青蛙一个人在家!
38、列表
z = [5,7,6,1,8,2]
z[0]
运行结果:
5
39、列表
z = [5,7,6,1,8,2]
z[4]
运行结果:
8
40、列表
z = [5,7,6,1,8,2]
z[-5]
运行结果:
7
41、切分列表
z = [5,7,6,1,8,2]
z[0:2]
运行结果:
[5, 7]
42、切分列表
z = [5,7,6,1,8,2]
z[:3]
运行结果:
[5, 7, 6]
43、切分列表
z = [5,7,6,1,8,2]
z[1:]
运行结果:
[7, 6, 1, 8, 2]
44、取列表的最大值, 最小值, 长度, 以及总和
z = [5,7,6,1,8,2]
print(min(z), max(z), len(z), sum(z))
运行结果:
1 8 6 29
45. Count the number of occurrences of objects in the list
mlist = [5,7,6,0,6,7]
mlist.count(6)
operation result:
2
46. Return the first pointer of the list
mlist = [5,7,6,0,6,7]
mlist.index(0)
operation result:
3
47. Return the first pointer in the list, specify where to start searching
mlist = [5,7,6,0,6,7]
mlist.index(6, 1)
operation result:
2
48. Return list pointer, specify where to start searching
mlist = [5,7,6,0,6,7]
mlist.index(7, 5, 6)
operation result:
5
49. Sort the list
#对列表进行排序
mlist = [5,7,6,0,6,7,9,3]
mlist.sort()
print(mlist)
operation result:
[0, 3, 5, 6, 6, 7, 7, 9]
50. Sort the list
mlist = [5,7,6,0,6,7,9,3]
mlist.sort(reverse = True)
print(mlist)
operation result:
[9, 7, 7, 6, 6, 5, 3, 0]
51. Add an object at the end of the list
mlist = [5,7,6,0,6,7,9,3]
mlist.append(4)
print(mlist)
operation result:
[5, 7, 6, 0, 6, 7, 9, 3, 4]
52. Delete an object in the list
mlist = [5,7,6,0,6,7,9,3]
mlist.remove(7)
print(mlist)
operation result:
[5, 6, 0, 6, 7, 9, 3]
53. Delete the object at the specified position in the list
mlist = [5,7,6,0,6,7,9,3]
mlist.pop(3)
print(mlist)
operation result:
[5, 7, 6, 6, 7, 9, 3]
54. Merge lists
mlist = [5,7,6,0,6,7,9,3]
mlist1 = [11, 8, 7, 3, 2, 3]
mlist.extend([4, 5])
print('mlist+mlist1=',mlist+mlist1)
operation result:
mlist+mlist1= [5, 7, 6, 0, 6, 7, 9, 3, 4, 5, 11, 8, 7, 3, 2, 3]