python-2

这篇博客介绍了Python的基础操作,包括浮点数的除法、导入future模块改变除法行为,以及索引、切片、重复、连接、成员操作符和for循环在列表中的应用。通过实例展示了Python编程的一些基本概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用的是 ipython
In [1]: 5/2
Out[1]: 2

In [2]: 5/2.0
Out[2]: 2.5

In [4]: from future import division 引用模块

In [5]: 5/2
Out[5]: 2.5

for i in range(5):
    print("hello") 

或者
count=0
while count<5:
    print("hello")
    count +=1


/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
hello
hello
hello
hello
hello

Process finished with exit code 0
In [3]: range(2,101,2)  偶数
In [4]: range(1,100,2)   奇数

count =0
while True:
    print("hello word")
    count +=1
    if count==5:
        break

/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
hello word
hello word
hello word
hello word
hello word

Process finished with exit code 0
while 1:
    cmd=input("comm>>:")
    if cmd=="":
        continue
    elif cmd=="quit":
        break
    else:
        print(cmd)


/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
comm>>:
comm>>:
comm>>:
comm>>:
comm>>:
comm>>:ls
ls
comm>>:quit

Process finished with exit code 0
In [5]: import os     引用模块

In [6]: os.system("date")
Wed May 23 20:57:32 CST 2018
Out[6]: 0
import os
while 1:
    cmd=input("comm>>:")
    if cmd=="":
        continue
    elif cmd=="quit":
        break
    else:
        os.system(cmd)




comm>>:date
Wed May 23 21:00:44 CST 2018
print("hello",end="\n")   end指定分隔符的,默认情况下为\n
print("hello python")
print("hello java",end=" ")  可以进行编辑
print("hello python")


/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
hello
hello python
hello java hello python

Process finished with exit code 0
print("hello c++")
print("""
            student
        1.add
        2.delete
""")


/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
hello c++

            student      进行编辑
        1.add
        2.delete


Process finished with exit code 0
print('"python"')
print("'python'")

单双引号的运用
/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
"python"
'python'

Process finished with exit code 0

索引

s='hello'
print(s[0])
print(s[4])
name="hfdughriwjeocj"
print(name[-1])  反向第一个


/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
j

Process finished with exit code 0

切片

s='hello'
print(s[0:3])     0-2字符
print(s[:3])       0-2字符
print(s[2:])        2-最后的字符
print(s[0:4:2])      开始到4   隔一个出一个
print(s[:])         所有
print(s[::-1])    反向操作
print(s[-2:])     倒数第二   到最后

/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
hel
hel
llo
hl
hello
olleh
lo
Process finished with exit code 0

重复

print('*'*10+'student'+'*'*10)    *10


/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
**********student**********

Process finished with exit code 0

连接

name='Go'
print("hello"+"world")
print("hello %s" %(name))
print("hello"+name)


/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
helloworld
hello Go
helloGo

Process finished with exit code 0

成员操作符

s='hello'
print('h' in s)    查找
print('hol'in s)
print('hol'not in s)



/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
True
False
True

Process finished with exit code 0

for循环

for i in range(10):
    print(i)



/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
0
1
2
3
4
5
6
7
8
9

Process finished with exit code 0
s='hello'
for i in s:
    print(i,end=',')



/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
h,e,l,l,o,
Process finished with exit code 0

s='hello'
for i in s:
    print(i+'a',end='|')    添加



/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
ha|ea|la|la|oa|
Process finished with exit code 0

#!/usr/bin/python3
num=input("Num:")
print('true' if num == num[::-1] else "fale")

判断回文数

/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
Num:65
fale

Process finished with exit code 0
#!/usr/bin/python3
s="hello123@"
print(s.islower())     判断是否都是小写
print(s.isupper())     判断是不是都为大写字母
print(s.isalnum())     判断是否都是字母或数字
print(s.isalpha())  判断是否都是字母
print(s.istitle())  判断是不是都是标题
print(s.isdigit())  判断是否都是数字
print(s.isspace())  判断是不是都是英文空格


/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
True
False
False
False
False
False
False

Process finished with exit code 0
#!/usr/bin/python3
var="hello@"
if var[0].isalpha() or var[0]=='_':
    for item in var[1:]:
        if not(item.isalnum() or item=="_"):
            print("Error:%s变量不合法" %(var))
            break
    else:
        print("合法")
else:
    print("Error:第一个字符不合法")


判断字符是否合法

/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
Error:hello@变量不合法

Process finished with exit code 0
#!/usr/bin/python3
trycount=0
while trycount<3:
    name=input("username:")
    passwd=input("passwd:")
    trycount +=1
    if name=='root' and passwd=='westos':
        print('ok')
        break
    else:
        print('not ok')
else:
    print("超过三次登陆机会")



/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
username:root
passwd:westos
ok

Process finished with exit code 0
#!/usr/bin/python3
for i in range(3):
    name=input("username:")
    passwd=input("passwd")
    if name=='rooot' and passwd=='westos':
        print('ok')
        break
    else:
        print('user or passwd error')
else:
    print('超过三次登陆机会')

  登陆认证

/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
username:rooot
passwdwestos
ok

Process finished with exit code 0
s='hello python, oj diyswifuyre8fyhew   kd python'
print(s.find('python'))  正向操作  ( 找到字符串  返回最小的索引值)
print(s.rfind('python'))    反向操作
print(len(s))     打印所有的个数

  查找字符串   空格也算

/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
6
40
46

Process finished with exit code 0
s='hello python, oj diyswifuyre8fyhew   kd python'
print(s.replace('python','wes tos'))
print(s.replace('python',''))

 替换  也可用作删除

/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
hello wes tos, oj diyswifuyre8fyhew   kd wes tos
hello , oj diyswifuyre8fyhew   kd 

Process finished with exit code 0
#!/usr/bin/python3
s='\t\t\t    hello  \n\n\t'
print(s)
print(s.strip())


去掉多余的东西        呈现本质

/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
                hello  


hello

Process finished with exit code 0
s='             hello      '
print(s.lstrip())      删除字符串左边的空格
print(s.strip())       删除空格
print(s.rstrip())       删除字符串右边的空格



/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
hello      
hello
             hello

Process finished with exit code 0
name=input('username:').strip()     忽略空格
print(name)
s='qwqwqwq erer rttrrwe we w e'
print(s.replace('w',''))

 替换   相当于删除

/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
qqqq erer rttrre e  e

Process finished with exit code 0
print('*'*10+'student'+'*'*10)
s='student'
print(s.center(20))    20个    剩下的用空格补齐
print(s.center(30,'*'))       30个   剩下的 用*  补齐

在中间添加   s

/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
**********student**********
      student       
***********student************

Process finished with exit code 0
print('*'*10+'student'+'*'*10)
s='student'
print(s.ljust(30,'*'))    s  在左边
print(s.rjust(30,'*'))      s  在右边



/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
**********student**********
student***********************
***********************student

Process finished with exit code 0
s='hello'
print(s.count('l'))       计数 l
print(s.count('llo'))    计数  llo


/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
2
1

Process finished with exit code 0
info=input('出勤记录:')
print(info.count('A') <=1 and info.count('LLL')==0)

  出勤记录编程
/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
出勤记录:LL
True

Process finished with exit code 0
user1='http://www.baidu.com'
print(user1.startswith('http://'))   以   开头
print(user1.endswith('.com'))         以    结尾




/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
True
True

Process finished with exit code 0
import os            导入命令模块
for filename in os.listdir('/var/log'):
    if filename.endswith('.log'):
        print(filename)




/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
yum.log
Xorg.1.log
wpa_supplicant.log
boot.log
Xorg.0.log

Process finished with exit code 0
#!/usr/bin/python3
ip=input('ip:')
ip_s=ip.split('.')   以什么为分隔符
if len(ip_s)==4:      统计个数
    for item in ip_s:
        item=int(item)
        if not 0<item<255:
            print("%s不合法" %(ip))
            break
    else:
        print("%s合法" %(ip))
else:
    print("%s不为4段" %(ip))
info='23+12+90'
print(eval(info))
new_info=info.replace('+','*')
print(eval(new_info))      求值



/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
125
24840

Process finished with exit code 0
info='23+12+90'
print(info.split('+')[::-1])    反向操作


/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
['90', '12', '23']

Process finished with exit code 0
num1=6
num2=4
min_num=min(num1,num2)
for item in range(min_num,0,-1):
    if num1%item==0 and num2%item==0:
        res=item
        break
print("%s%s的最大公约数为%s" %(num1,num2,res))
print("%s%s的最小公倍数为%d" %(num1,num2,(num1*num2)/res))




/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/untitled/day02/10_boke.py
64的最大公约数为2
64的最小公倍数为12

Process finished with exit code 

列表

l=[1,2e+8,2j+9,True,'hello']
print(l,type(l))




/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
[1, 200000000.0, (9+2j), True, 'hello'] <class 'list'>

Process finished with exit code 0
l=[[1,2,3,4],2,3,4]
print(l,type(l))
print(l[0])
print(l[0][-1])
print(l[-1])

索引
/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
[[1, 2, 3, 4], 2, 3, 4] <class 'list'>
[1, 2, 3, 4]
4
4

Process finished with exit code 0
print(list(range(5)))
print(range(5))

强制转换为列表

/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
[0, 1, 2, 3, 4]
range(0, 5)

Process finished with exit code 0
print(list(range(5)))
l=list(range(5))    强制转换
print(l[:-1])     切片
print(l*3)       重复
l1=[1,2,3]
print(l+l1)      相加

print(1 in l)      成员操作
print(1 not in l)



/usr/local/python3/bin/python3.6 /root/PycharmProjects/untitled/day0/05_zuoye.py
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 1, 2, 3]
True
False

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值