python小实验(根据莫烦python视频)


print(‘apple’)
print(apple)

a,b,c=1,2,3
print(‘a,b,c=’)
print(a,b,c)

自变量定义&d打印

”’
阿斯达斯
”’

循环实验,按顺序打印0-9;注意,while下面必须要比while后一格

condition=0
while condition<10:
print(‘condition=%s’ %condition)
condition+=1

for循环打印1-10,i为受体,即每次的list元素会逐一放到i中打印出来

list=[1,2,3,4,5,6,7,8,9,10]
for i in list:
print(‘i=%s’ %i)
print(‘end counting’)

for range 使用方法 range(起始位,<最大数值(到不了该数值),步长)

for j in range(0,11,2):
print(‘j=%s’ %j)

print(‘end counting’)

if (else) 使用方法,可以搭配for使用限制其范围输出 1—10 仅输出前四个

x=0
list=[1,2,3,4,5,6,7,8,9,10]
for p in list:
print(‘p=%s’ %p)
x+=1
if x==4:
print(‘end counting’)
break
else:
print(‘continue p counting’)

def 定义函数accumerate=加法 saleCar()卖车

def acc(a,b):
print(‘this is a function’)
c=a+b
print(‘c=’,c)
def saleCar(price,color,brand,is_sec_hand):
print(‘price=’,price,
‘color=’,color,
‘brand=’,brand,
‘is sec hand?=’,is_sec_hand,)

saleCar(price=10000,color=’red’,brand=’TOYOTA’,is_sec_hand=’TURE’)

定义全局和局部变量

def fun():
a=10
print(a)
return a+100

#局部-全局 A&a past 为全局 a son 为局部
A=100
a=0
print(A)
print(‘a past=’,a)
def fun1():
a=20
print(‘a son=’,a)
return a+A
print(‘a global=’,a)
print(‘fun1()=’,fun1())
print(‘a past=’,a)

读写文件

text=’This is my first test. This is the second line. This the third ’
print(text) # 无换行命令

“””
This is my first test. This is the second line. This the third
“”“

text=’This is my first test.\nThis is the second line.\nThis the third line’
print(text) # 输入换行命令\n,要注意斜杆的方向。注意换行的格式和c++一样

my_file=open(‘e:\demo\MyFile.txt’,’w’) #用法: open(‘绝对路径文件名’,’形式’), 其中形式有’w’:write;’r’:read.
my_file.write(text) #该语句会写入先前定义好的 text
my_file.close() #关闭文件

file = open(‘e:\demo\my file.txt’,’r’)

while 1: #读文件 快速
lines = file.readlines()
if not lines:
break
for line in lines:
print(line)
pass

`
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值