菜鸡学Python

前言
大一暑假疫情原因推迟开学,希望两周之内能够自学Python的基本操作。后续开学时期会学习爬虫相关内容。每天学习Python并编写blog打卡记录自己的学习生活。

下载和安装

Python3下载地址
pycharm下载地址

安装教程

本来是想自己写的,但菜鸟教程YYDS!
请移步菜鸟教程学习详细安装步骤

打卡代码

demo1 :各种输出方式

# 开发时间:2021/8/24 11:18
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 博主链接:https://blog.csdn.net/qq_45148277
print("hello world")
print(1+2)
print(3.5)
fp=open('D:/text.txt','a+')
print('hello world',file=fp)
fp.close()
print('hello','world','python!')

demo2:转义字符的复习

# 作者:邵昱原
# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/24 15:54
print("hello\nworld")
print("hello\tworld")
print("hello\rworld")
print("hello\bworld")
print("http:\\\\www.baidu.com")
print("老师说:\"大家好\"")
#原字符,在字符串前加r或R使字符串内的反斜杠无效,但最后一个字符不能是反斜杠(可以有两个)。

demo3:关键字的记录

# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/24 16:13
import keyword
print(keyword.kwlist)

demo4:浮点数的计算


# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/25 15:47
n1=2.2
n2=1.1
n3=2.1
print(n1+n2)
print(n2+n3)
from decimal import Decimal
print(Decimal('1.1')+Decimal('2.2'))

demo5:强制类型转换

# 作者:邵昱原
# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/25 15:51
name='邵昱原'
age=21
num='123'
print('我叫'+name+',今年'+str(age)+'岁')
print(int(num))

demo6:输入语句


# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/25 16:14
a=input()
b=input()
a=int(a)
b=int(b)
print(a+b)

demo7:关于运算符中除和取余与C++有何不同


# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/25 16:43
#运算时要向下取整
print(9//4)
print(-9//-4)

print(-9//4)
print(9//-4)

print(9%-4)  #公式 余数=被除数-除数*商 9-(-4)*(-3)=-3
print(-9%4)

打卡完毕

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这是一个经典的问题,也称为“狼、羊、菜和农夫过河问题”。在这个问题中,农夫需要带着一只狼、一只羊和一些菜过河,但是船只只能容纳农夫和另外一只物品。如果农夫不在场,狼会吃羊,羊会吃菜。因此,农夫需要找到一种方法,使得所有物品都能安全地过河。 以下是一个简单的Python实现,其中使用了深度优先搜索算法来解决问题: ``` # 定义初始状态和目标状态 start_state = ['left', 'left', 'left', 'left'] goal_state = ['right', 'right', 'right', 'right'] # 定义有效状态 def is_valid(state): if state[0] != state[1] and state[1] != state[2]: return True return False # 定义搜索函数 def dfs(state, path): if state == goal_state: return path for i in range(8): new_state = state.copy() if i == 0: new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 1: if new_state[3] == new_state[0]: new_state[1] = 'right' if new_state[1] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 2: if new_state[3] == new_state[1]: new_state[0] = 'right' if new_state[0] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 3: if new_state[3] == new_state[2]: new_state[2] = 'right' if new_state[2] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 4: if new_state[3] == 'right': new_state[3] = 'left' elif i == 5: if new_state[3] == new_state[0]: new_state[1] = 'right' if new_state[1] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 6: if new_state[3] == new_state[1]: new_state[0] = 'right' if new_state[0] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 7: if new_state[3] == new_state[2]: new_state[2] = 'right' if new_state[2] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' if is_valid(new_state) and new_state not in path: new_path = dfs(new_state, path + [new_state]) if new_path: return new_path return None # 执行搜索并输出结果 result = dfs(start_state, [start_state]) if result: for state in result: print(state) else: print('No solution found.') ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值