python入门的120个基础练习(附资料答案)_python入门作业(1)

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Python全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img



既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Python知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024c (备注Python)
img

正文

print('hello world!')
	print('hello', 'world!')  # 逗号自动添加默认的分隔符:空格
	print('hello' + 'world!')  # 加号表示字符拼接
	print('hello', 'world', sep='***')  # 单词间用***分隔
	print('#' * 50)  # *号表示重复50遍
	print('how are you?', end='') # 默认print会打印回车,end=''表示不要回车

03-基本运算

运算符可以分为:算术运算符、比较运算符和逻辑运算符。优先级是:算术运算符>比较运算符>逻辑运算符。最好使用括号,增加了代码的可读性。

print(5 / 2)  # 2.5
print(5 // 2)  # 丢弃余数,只保留商
print(5 % 2)  # 求余数
print(5 ** 3)  # 5的3次方
print(5 > 3)  # 返回True
print(3 > 5)  # 返回False
print(20 > 10 > 5)  # python支持连续比较
print(20 > 10 and 10 > 5)  # 与上面相同含义
print(not 20 > 10)  # False

04-input

number = input("请输入数字: ")  # input用于获取键盘输入
print(number)
print(type(number))  # input获得的数据是字符型

print(number + 10)  # 报错,不能把字符和数字做运算
print(int(number) + 10)  # int可将字符串10转换成数字10
print(number + str(10))  # str将10转换为字符串后实现字符串拼接

05-输入输出基础练习

username = input('username: ')
print('welcome', username)   # print各项间默认以空格作为分隔符
print('welcome ' + username)  # 注意引号内最后的空格

06-字符串使用基础

python中,单双引号没有区别,表示一样的含义

sentence = 'tom\'s pet is a cat'  # 单引号中间还有单引号,可以转义
	sentence2 = "tom's pet is a cat"  # 也可以用双引号包含单引号
	sentence3 = "tom said:\"hello world!\""
	sentence4 = 'tom said:"hello world"'
	# 三个连续的单引号或双引号,可以保存输入格式,允许输入多行字符串
	words = """
	hello
	world
	abcd"""
	print(words)
	
	py_str = 'python'
	len(py_str)  # 取长度
	py_str[0]  # 第一个字符
	'python'[0]
	py_str[-1]  # 最后一个字符
	# py_str[6]  # 错误,下标超出范围
	py_str[2:4]  # 切片,起始下标包含,结束下标不包含
	py_str[2:]  # 从下标为2的字符取到结尾
	py_str[:2]  # 从开头取到下标是2之前的字符
	py_str[:]  # 取全部
	py_str[::2]  # 步长值为2,默认是1
	py_str[1::2]  # 取出yhn
	py_str[::-1]  # 步长为负,表示自右向左取
	
	py_str + ' is good'  # 简单的拼接到一起
	py_str * 3  # 把字符串重复3遍
	
	't' in py_str  # True
	'th' in py_str  # True
	'to' in py_str  # False
	'to' not in py_str  # True

07-列表基础

列表也是序列对象,但它是容器类型,列表中可以包含各种数据

**alist = [10, 20, 30, 'bob', 'alice', [1,2,3]]
len(alist)
alist[-1]  # 取出最后一项
alist[-1][-1]  # 因为最后一项是列表,列表还可以继续取下标
[1,2,3][-1]  # [1,2,3]是列表,[-1]表示列表最后一项
alist[-2][2]  # 列表倒数第2项是字符串,再取出字符下标为2的字符
alist[3:5]   # ['bob', 'alice']
10 in alist  # True
'o' in alist  # False
100 not in alist # True
alist[-1] = 100  # 修改最后一项的值
alist.append(200)  # 向**列表中追加一项

08-元组基础

元组与列表基本上是一样的,只是元组不可变,列表可变。

atuple = (10, 20, 30, 'bob', 'alice', [1,2,3])
	len(atuple)
	10 in atuple
	atuple[2]
	atuple[3:5]
	# atuple[-1] = 100  # 错误,元组是不可变的

09-字典基础

# 字典是key-value(键-值)对形式的,没有顺序,通过键取出值
adict = {'name': 'bob', 'age': 23}
len(adict)
'bob' in adict  # False
'name' in adict  # True
adict['email'] = 'bob@tedu.cn'  # 字典中没有key,则添加新项目
adict['age'] = 25  # 字典中已有key,修改对应的value

10-基本判断

单个的数据也可作为判断条件。 任何值为0的数字、空对象都是False,任何非0数字、非空对象都是True。

if 3 > 0:
    print('yes')
    print('ok')

if 10 in [10, 20, 30]:
    print('ok')

if -0.0:
    print('yes')  # 任何值为0的数字都是False

if [1, 2]:
    print('yes')  # 非空对象都是True

if ' ':
    print('yes')  # 空格字符也是字符,条件为True

11-条件表达式、三元运算符

a = 10
b = 20

if a < b:
    smaller = a
else:
    smaller = b

print(smaller)

s = a if a < b else b  # 和上面的if-else语句等价

print(s)

12-判断练习:用户名和密码是否正确

import getpass  # 导入模块

username = input('username: ')
# getpass模块中,有一个方法也叫getpass
password = getpass.getpass('password: ')

if username == 'bob' and password == '123456':
    print('Login successful')
else:
    print('Login incorrect')

13-猜数:基础实现

import random
	
	num = random.randint(1, 10) # 随机生成1-10之间的数字
	answer = int(input('guess a number: '))  # 将用户输入的字符转成整数
	if answer > num:
	    print('猜大了')
	elif answer < num:
	    print('猜小了')
	else:
	    print('猜对了')
	
	print('the number:', num)

14-成绩分类1

score = int(input('分数: '))

if score >= 90:
    print('优秀')
elif score >= 80:
    print('好')
elif score >= 70:
    print('良')
elif score >= 60:
    print('及格')
else:
    print('你要努力了')

15-成绩分类2

score = int(input('分数: '))

if score >= 60 and score < 70:
    print('及格')
elif 70 <= score < 80:
    print('良')
elif 80 <= score < 90:
    print('好')
elif score >= 90:
    print('优秀')
else:
    print('你要努力了')

16-石头剪刀布

import random

all_choices = ['石头', '剪刀', '布']
computer = random.choice(all_choices)
player = input('请出拳: ')

# print('Your choice:', player, "Computer's choice:", computer)
print("Your choice: %s, Computer's choice: %s" % (player, computer))
if player == '石头':
    if computer == '石头':
        print('平局')
    elif computer == '剪刀':
        print('You WIN!!!')
    else:
        print('You LOSE!!!')
elif player == '剪刀':
    if computer == '石头':
        print('You LOSE!!!')
    elif computer == '剪刀':
        print('平局')
    else:
        print('You WIN!!!')
else:
    if computer == '石头':
        print('You WIN!!!')
    elif computer == '剪刀':
        print('You LOSE!!!')
    else:
        print('平局')

17-改进的石头剪刀布

import random

all_choices = ['石头', '剪刀', '布']
win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
prompt = """(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2): """
computer = random.choice(all_choices)
ind = int(input(prompt))
player = all_choices[ind]

print("Your choice: %s, Computer's choice: %s" % (player, computer))
if player == computer:
    print('\033[32;1m平局\033[0m')
elif [player, computer] in win_list:
    print('\033[31;1mYou WIN!!!\033[0m')
else:
    print('\033[31;1mYou LOSE!!!\033[0m')

18-猜数,直到猜对

import random

num = random.randint(1, 10)
running = True

while running:
    answer = int(input('guess the number: '))
    if answer > num:
        print('猜大了')
    elif answer < num:
        print('猜小了')
    else:
        print('猜对了')
        running = False

19-猜数,5次机会

import random

num = random.randint(1, 10)
counter = 0

while counter < 5:
    answer = int(input('guess the number: '))
    if answer > num:
        print('猜大了')
    elif answer < num:
        print('猜小了')
    else:
        print('猜对了')
        break
    counter += 1
else:  # 循环被break就不执行了,没有被break才执行
    print('the number is:', num)

20-while循环,累加至100

因为循环次数是已知的,实际使用时,建议用for循环

sum100 = 0
	counter = 1
	
	while counter < 101:
	    sum100 += counter
	    counter += 1
	
	print(sum100)

21-while-break

break是结束循环,break之后、循环体内代码不再执行。

while True:
	    yn = input('Continue(y/n): ')
	    if yn in ['n', 'N']:
	        break
	    print('running...')

22-while-continue

计算100以内偶数之和。
continue是跳过本次循环剩余部分,回到循环条件处。

sum100 = 0
	counter = 0
	
	while counter < 100:
	    counter += 1
	    # if counter % 2:
	    if counter % 2 == 1:
	        continue
	    sum100 += counter
	
	print(sum100)

23-for循环遍历数据对象

astr = 'hello'
alist = [10, 20, 30]
atuple = ('bob', 'tom', 'alice')
adict = {'name': 'john', 'age': 23}

for ch in astr:
    print(ch)

for i in alist:
    print(i)

for name in atuple:
    print(name)

for key in adict:
    print('%s: %s' % (key, adict[key]))

24-range用法及数字累加

# range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
	# >>> list(range(10))
	# range(6, 11)  # [6, 7, 8, 9, 10]
	# range(1, 10, 2)  # [1, 3, 5, 7, 9]
	# range(10, 0, -1)  # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
	sum100 = 0
	
	for i in range(1, 101):
	    sum100 += i
	
	print(sum100)

25-列表实现斐波那契数列

列表中先给定两个数字,后面的数字总是前两个数字之和。

fib = [0, 1]
	
	for i in range(8):
	    fib.append(fib[-1] + fib[-2])
	
	print(fib)

26-九九乘法表

for i in range(1, 10):
    for j in range(1, i + 1):
        print('%s*%s=%s' % (j, i, i * j), end=' ')
    print()

# i=1 ->j: [1]
# i=2 ->j: [1,2]
# i=3 ->j: [1,2,3]
#由用户指定相乘到多少
n = int(input('number: '))

for i in range(1, n + 1):
    for j in range(1, i + 1):
        print('%s*%s=%s' % (j, i, i * j), end=' ')
    print()

27-逐步实现列表解析

# 10+5的结果放到列表中
	[10 + 5]
	# 10+5这个表达式计算10次
	[10 + 5 for i in range(10)]
	# 10+i的i来自于循环
	[10 + i for i in range(10)]
	[10 + i for i in range(1, 11)]
	# 通过if过滤,满足if条件的才参与10+i的运算
	[10 + i for i in range(1, 11) if i % 2 == 1]
	[10 + i for i in range(1, 11) if i % 2]
	# 生成IP地址列表
	['192.168.1.%s' % i for i in range(1, 255)]

28-三局两胜的石头剪刀布

import random

all_choices = ['石头', '剪刀', '布']
win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
prompt = """(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2): """
cwin = 0
pwin = 0

while cwin < 2 and pwin < 2:
    computer = random.choice(all_choices)
    ind = int(input(prompt))
    player = all_choices[ind]

    print("Your choice: %s, Computer's choice: %s" % (player, computer))
    if player == computer:
        print('\033[32;1m平局\033[0m')
    elif [player, computer] in win_list:
        pwin += 1
        print('\033[31;1mYou WIN!!!\033[0m')
    else:
        cwin += 1
        print('\033[31;1mYou LOSE!!!\033[0m')

29-文件对象基础操作

# 文件操作的三个步骤:打开、读写、关闭
	# cp /etc/passwd /tmp
	f = open('/tmp/passwd')  # 默认以r的方式打开纯文本文件
	data = f.read()  # read()把所有内容读取出来
	print(data)
	data = f.read()  # 随着读写的进行,文件指针向后移动。
	# 因为第一个f.read()已经把文件指针移动到结尾了,所以再读就没有数据了
	# 所以data是空字符串
	f.close()
	
文末有福利领取哦~
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

👉**一、Python所有方向的学习路线**

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。![img](https://img-blog.csdnimg.cn/c67c0f87cf9343879a1278dfb067f802.png)

👉**二、Python必备开发工具**

![img](https://img-blog.csdnimg.cn/757ca3f717df4825b7d90a11cad93bc7.png)  
👉**三、Python视频合集**

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。  
![img](https://img-blog.csdnimg.cn/31066dd7f1d245159f21623d9efafa68.png)

👉 **四、实战案例**

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。**(文末领读者福利)**  
![img](https://img-blog.csdnimg.cn/e78afb3dcb8e4da3bae5b6ffb9c07ec7.png)

👉**五、Python练习题**

检查学习结果。  
![img](https://img-blog.csdnimg.cn/280da06969e54cf180f4904270636b8e.png)

👉**六、面试资料**

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。  
![img](https://img-blog.csdnimg.cn/a9d7c35e6919437a988883d84dcc5e58.png)

![img](https://img-blog.csdnimg.cn/5db8141418d544d3a8e9da4805b1a3f9.png)

👉因篇幅有限,仅展示部分资料,这份完整版的Python全套学习资料已经上传




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip1024c (备注python)**
![img](https://img-blog.csdnimg.cn/img_convert/3368516d78c735ae41ea03ac0fdedb78.png)

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
习题**

检查学习结果。  
![img](https://img-blog.csdnimg.cn/280da06969e54cf180f4904270636b8e.png)

👉**六、面试资料**

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。  
![img](https://img-blog.csdnimg.cn/a9d7c35e6919437a988883d84dcc5e58.png)

![img](https://img-blog.csdnimg.cn/5db8141418d544d3a8e9da4805b1a3f9.png)

👉因篇幅有限,仅展示部分资料,这份完整版的Python全套学习资料已经上传




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip1024c (备注python)**
[外链图片转存中...(img-pVoGo0jY-1713351092841)]

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过提取码4idx获得小甲鱼的《零基础入门学习Python》全套课后作业答案。此外,你还可以获得一本非常实用的Python书籍《Python编程:从入门到实践》。在Python中,字符串是一种特殊类型的对象。你可以使用点号加方法名称的方式调用字符串对象上的方法。例如,如果你有一个名为name的字符串对象,你可以使用name.lower()方法将其转换为小写,name.upper()方法将其转换为大写,以及name.title()方法将其转换为首字母大写的形式。另外,如果你想检查一个条件是否为True,并在条件成立时执行相应的操作,你可以使用if语句。例如,如果numb等于8,你可以使用if numb == 8: print(numb)来打印出numb的值。希望这些信息对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [小甲鱼《零基础入门学习Python》全套课后作业答案.pdf](https://download.csdn.net/download/qq_43966957/87902941)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python入门基本练习题(54题)](https://blog.csdn.net/weixin_69553582/article/details/131216267)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值