Python-----常规命令

1.range命令

>>>range(5)

[0,1,2,3,4,]

>>>range(1,10)

[1,2,3,4,5,6,7,8,9]

>>>##拿出1~10之间偶数

... range(2,11,2)   

[2,4,6,8,10]

>>>##拿出1~7之间所有奇数

... range(1,11,2)

[1,3,5,7,9]

***range()函数

range(stop):       0~stop-1

range(start,stop):     start~stop

range(start,stop,step):start~stop-1,step为步长

例:输入用户名和密码

         判断用户名和密码是否正确(name=root,passwd=westos)

         为防止玻璃破解,登陆仅有三次机会,超过则报错提示

for    i   in   range(3)

         name = input("用户名")

          paswd = input("密码")

          if    name   ==   'root'   and   passwd   ==   'westos'

                 print("登陆成功")

                 exit()

           esle:

                  print("登录失败")

                   print(“你还剩三次机会” %(2-i))

print(“登陆次数超过三次,请等待100s再次登陆”)

2. break       ##跳出整个循环,不会再循环里边的内容

    continue   ##跳出本次循环,continue后面的代码不会在执行

     exit           ##结束程序的运行

python2:

               -range()即可生成数据,消耗时间并占用内存

               -xrange()生成一个xrange对象

python3:-range()  相当于python2中的xrange

3.for循环使用的语法

             for    变量   in    range(stop):

                       循环需要执行的代码

             

              for    变量   in    range(stop):

                         循环需要执行的代码

               else:

                          循环结束执行的代码

4.while条件语句

       while   条件语句:

                  满足条件执行的语句

        else:

                    不满足条件执行的语句

例:1+2+3+......+100累加和

sum = 0

i = 1

while   i <=100:

           sum +=i

            i += 1

print(sum)

 

5.转义字符

\n:   ##换行符

\"":     ##代表双引号本身

\t:       ##代表tab符

\'':       ##代表单引号本身

6.字符串的定义

str1 = 'our   company   is   westos'

str2 =" 'our   company   is   westos' '"

str3 = ""our   company   is   westos""

7.字符串操作

索引

>>>s = "hello"

>>>s[2]

'e'

>>>s = "hello"

>>>[-2]

'l'

切片

s[start : end :step]

从start开始到end-1结束,步长为step

start省略,则从头开始切片

end省略,一直切片到字符串最后

s[1:]

s[:-1]

s[::-1]            ##对于字符串反转

s[:]                 ##对字符串进行拷贝

成员操作符

>>>s ="hello"

'h'   in s

>>>True

判断子串

连接

>>>a = "hello"

>>>b = "python"

>>>print("%s %s" %(a,b))

hello python

>>>a + b

'hellopython'

>>>a + "  " + b

'hello  python'

计算长度

字符串的重复

>>>"*"*10 +"学生管理系统"+ “*”*10

***********学生管理系统**********

8. isalnum            ##判断一个字符是否由字母或者数字组成

     isalpha           ##检测是否为纯英文字母

     isdigit            ##检测是否为纯数字

     islower            ##检测是否将大写变成小写

     isspace            ##检测是否有空格

      istitle             ##是否每个字母都大写

       isupper        ##检测是否将小写变成大写

       lower            ##将大写变成小写

       upper          ##将小写变成大写

        title           ##首字母大写

>>>"HELLO".istitle()

True

>>>"hello".istitle()

False

>>>"HelloWorld".istitle

>>>"hello".upper()

'HELLO'

>>>"hello".lower()

'hello'

>>>"hello".title()

'Hello'

>>>"hello".swapcase

'HEllo'

9.  endswith     ##判断字符串以什么结尾

    startswith     ##判断字符串以什么开始

filename = "hello.log"

if   filename.endswith(".log"):

     print(filename)

else:

        print("error   file")

10. strip      ##去两边

lstrip          ##去左边

  rstrip       ##去右边

“去除左右两边空合格,空格为广义空格,包括、\n  \t  \r

>>> s ="       hello    "

'hello'

>>>s.lstrip()

'hello         '

>>>s.rstrip()

'               hello'

>>>s = "\nhello            "

>>>s.strip()

'hello'

>>>s = "\thello      "

>>>s.strip()

'hello'

s = "hello"

>>>s.strip()

'ello'

11.变量名合法性

变量名可以以字母,下划线或者数字组成

变量名只能以字母或者下划线开头

例:变量名第一个字符是否为字母或者下划线

        如果是继续判断

         如果不是报错;不合法

依次判断除第一个字符之外的其他字符

判断是否为字母,数字或者下划线

while   True:

             s  = input("变量名")

              if   s = 'exit':

                           break

               if   s[0].isalpha()  or s[0]=="-"

                            for   i   in   s[1:]:

                                         if   not (i.isalnum()   or   i=="-"):

                                                         print("%s变量名不合法"   %(s))

                                                          break

                               else:

                                             print("变量名合法"   %(s))

                    else:

                                  print("%s变量名不合法"   %(s))

12 find             ##从字符串左边开始查询子字符串匹配到的第一个索引

  rfind             ##从字符串右边开始查询子字符串匹配到的第一个索引

replace          ##字符串的替换

  count            ##字符串的出现次数统计

 split                ##字符串的分割

  jion                ##拼接字符串

>>> s ="hello   python,  learn   python"

>>>s.find('python')

6

>>>s.rfind('python')

19

>>>s.replace('python','linux')

"hello   linux, learn   linux"

'hello   python,   learn   python'

>>>s.count("python")

2

>>>s.count("p")

2

>>>s.count("i")

0

>>>ip =172.25.254.19

>>>ip.split('.')

('172','25','254'.'19')

>>>date ="2018-2-30"

>>>date.split("-")

('2018','2','30')

date.replace("-","/")

'2018/2/30'

>>>ip =('172','25','254','19')

>>>":".jion(ip)

"172:25:254:19"

13. min   max   range   len

>>>min(2,4)

2

>>>max(2,5)

5

>>>sum(range(1,101,2))

2500

>>>sum(range(2,101,2))

2550

>>>sum(range(1,101))

5050

>>>s ="hello"

>>>len(s)

5

14枚举

>>>for   i   in   s:

...   print("------>")

...

-------->h

--------->e

--------->l

--------->l

--------->o

>>>for   i   in   range(len(s)):

                  print(i,s(i))

(0,'h')

(1,'e')

(2,'l')

(3,'l')

(4,'o')

>>>s1 = 'abc'

>>>s2  ='123'

>>>for   i   in   zip(s1,s2):

          print(i)

('a','1')
('b','2')
('c','3')

>>>for   i   in   zip(s1,s2):

          print("",jion(i))

a1

b2

c3

>>>for   i   in   zip(s1,s2):

          print("-",jion(i))

a-1

b-2

c-3

 

 

 

 

 

                               

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值