python基础语法

Posted on 2018-10-29 21:32  郑幸福  阅读(146)  评论(0编辑  收藏  举报

关于python基础语法, 我之前已经学过了,这次又重新回顾了一下,并且打包到博客中,也算是不枉此行,我的收获还是挺大的!!!

 

python 开发IDE:

  pycharm、 eclipse

 

1.运算符:

+ - * / ** % //

  判断某个东西是否在某个东西里面包含:

    

  in 、  not in 

   结果 : 布尔值

 ==  
	 > 
	 < 
	 >=
	 <=
	 != 不等于
	 <>	不等于、

  逻辑运算:

and 
	or
	not 非
	v = user == 'alex' and pwd = '213' or 1==1 and ped =='6799'
	优先级:
	
	
	v = not False
	v = user == 'alex' or pwd == 123

  注意:
  布尔值:
   真: True
   假: False

基本数据类型:
 数字:
 int  整型
  -int
  将字符串转换成数字:

a = '123'
            print(type(a), a)
        b = int(a)
            print(type(b), b)
            
        num = '0011'
        v = int(num, base=16)#按照base进制转换十进制
        print(v) 
    
        age = 1
        #bit_length 当前数字的二进制,至少用几位表示
        r = age.bit_length()
        print(r)

字符串:str  魔法

s1 = 'alex'
    s1.capitalize()# 首字母大写
    s1.lower()
    s1.casefold()
    
    s1.center(20) #一共20个位置 把内容居中
    s1.center(20, '*') #一共20个位置 把内容居中,空白位置*填充
    
    test = “alsdka”
    v = test.count('a')#计算当前个数出现几次
    v = test.count('a', 5)#从第5位开始计算当前个数出现几次
    
    test = "alex"
    v = test.endswith('ex')#以什么什么结尾,
    v = test.startswith('al')#以什么什么开始
    
    
    test = 'alexalex'
    
    v = test.find('ex') # 从开始往后找,找到第一个,获取其位置
    v = test.index('ex')#从开始往后找,找到第一个,获取其位置,找不到,直接忽略
    
    
    #格式化, 将一个字符串中的占位符替换为指定的值
    test = 'i am {name}, age{a}' 
    print(test)
    v = test.format(name = 'alex', a = 19)
    v = test.format_map({"name":'alex', 'a':19})
    print(v)

    #expandtabs断句, \t
    test = 'username\temail\tpassword\nlaiying\ying@q.com\t132'
    t=test.expandtabs(20) 
    print(t)
#是否是数字
    test = "123"
    t = test.isalnum()
    print(t)
#是否是字母
    test = 'as2df'
    v = test.isalpha()
    print(v)
    
#将字符串中的每一个元组按照指定的分隔符进行拼凑     
     test = '你是谁?'
     t = '-'
     v =t.join(test)
    print(v)     

#移除空白
    test = ' alex'
    test.lstrip()#左边
    test.rstrip()#右边
    test.strip()#两边
    #也可以去掉指定的字符
    
    test = ' alexa'
    test.lstrip(a)
    test.rstrip(e)
    test.strip(a)
    
    
    
    
    
    name1 = "shizhengwen"
    v = name1.upper() #变成大写
    
    
    
    列表:list
    元组:tuple
    字典:dict