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

来源:互联网 发布:三亚南山寺旅游数据 编辑:程序博客网 时间:2024/06/05 06:24

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

`
原创粉丝点击