《3天学习python》笔记

              Example&exercise:conditions代码

注意事项 :注意缩进,和原来的代码对齐,tab和空格不能混用

 

>>> a=[24,16,54]

>>> b=[]

>>> if a[0] < a[1] and a[0]< a[2]:

...    b.append(a[0])

...    if a[1] < a[2]:

...        b.append(a[1])

...        b.append(a[2])

...    else:

...        b.append(a[2])

...        b.append(a[1])

 

 

 

>>> #example of for and range

... #default start of range is 0

... #default step of range is 1

... for i inrange(2,10,3):

...     print(i)

...     l=i**2

...     print(l)

 

清屏操作:

import os

os.system( 'cls' )

 

由此也可见,想要执行任何一条系统命令,都只需要执行os.system( 'COMMAND' )就行了.

 

由于缩进总出错,不好编辑,不用anaconda prompt编辑器,用Py’Charm

 

 

找到第一个能被17整除的数,300-350

#search the firstnumber which can be divided by 17 in [300,350]
for i in range(300,351):
    
if i%17==0:
         
print(i)
         
break
     else
:
         
continue

1-100偶数的和

a=0

for i in range(1,101):

     if i%2==0:

          a+=i;



print(a)
定义函数
def MaxOfTwo(x1,x2):

     if x1>=x2:

          return x2

     else:

          return x1



a=1

b=2

c=MaxOfTwo(a,b)

print(c)
关于文件的操作
创建txt文本
file=open('newfile.txt','w')

file.write('Ian created for .\n')

file.write('how about you!')

file.close()
 
逐行读入txt文件,以字符串的方式
file=open('newfile.txt','r')

for line in file:

          print(file.readline())
          print(file.readlines())

file.close()
 
以全部读入的方式:
file=open('newfile.txt','r')

for line in file:

     print(line)

file.close()
文件加入内容:
file=open('newfile.txt','a')

file.write('\n I am back again.\n')

file.close()
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值