在这里插入代码片
# print('hello python')
#1.python跨平台的, 是面向对象编程的解释性语言(脚本语言)、胶水语言(python中可以执行c语言和java语言)
# python 没有分号; 语句块是以缩进的方式实现(一个tab或者4个空格为准)
# python是交互式编程,写一句执行一句;提供idle工具,不适合编写复杂结构
#2.输入 input(prompt=None, /)
# a=input('a=') # 只有一个参数prompt;用于提醒用户输入,默认为空
#//input无论输入什么,都是str字符串
# a = input('a=')
# print(type(a),a)
# input('a=') 有线程阻塞功能,不回车,就不会执行后面的代码
#3.输出 print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
# print(a)
# c,v=1,2
# # sep=' '控制自身输出变量之间的间隙,不写默认有一个间隔
# print(c,v,sep='-')
# print(c,v,sep='+')
# end=" " 控制与下一个输出之间的间距;不写默认\n换行
# print(c,v,end="n")
# print(c,v)
#4.变量
# 变量定义:
# 1.变量名的第一位必须是字母或者下划线
# 2.变量中间不允许使用特殊字母
# 3.区分大小写
# 4.不能使用关键字(已经被系统使用的变量名keyword)
# 关键字:
# _a=1
# _=1
# # am=123
# import keyword
# print(keyword.kwlist)这里面就是系统定义好的变量名,不能使用,
# ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
#1.逻辑运算符 :and or not
#2.boolean :False True
# True和False是系统关键字,已经定义好的变量,
# a=True
# print(type(a)) #显示bool类型
# a=None
# print(type(a)) #显示NoneType类型
#3.'break', 'continue'循环控制
#4.成员运算符 : in is
# in 判读是否在字符串中,必须是连续的
# s='hello'
# s2='o'
# print(s2 in s)
# id函数查看变量的内存地址,
# a is b 判断地址是否相等( a is b 的时候,其实相当于检查 id(a) == id(b))python中,默认-5->256的数字,在定义的时候使用相同的地址,
# s='hello'
# s2='hello'
# print(id(s),id(s2))
#5.空:表示不存在于内存 None python中没有null
#6.取别名 as import random as rd
#7.assert 断言 assert的异常参数,
# 其实就是在断言表达式后添加字符串信息,用来解释断言并更好的知道是哪里出了问题。
# a=1;
# assert a>1;
#8'class' 定义类的,'def' 定义函数或者方法def xxx():
# del 删除所有变量
# a=1;
# # del a
# print(a)
#9.global 定义全局变量
#10.lambda 定义匿名函数
#5.循环结构 for...else while...else if...else if...elif
#6.异常处理 try...except...else
#7.库的引入,from import
# # from random import randint 从random类中中导入randint函数 也可以*导入所有
# import random #直接导入类random,使用就要random.
# a=random.randint(1,11)
# print(a)
# a=2
# b=2
# print(a is b)
#
# a=2561
# b=2561
# print(a is b)
#在这里就是一样
# a='asd c'
# b='asd c'
# print(a is b)
# print(id(a),id(b))
# 在cmd中不一样
# >>> a='asd c'
# >>> b='ads c'
# >>> id(a)
# 52168864
# >>> id(b)
# 52169856
# import sys
# print(sys.version)
#8.格式化输出
#占位符:
# %s(可以放字符串,数字)
# %d(匹配的必须是数字,字符串报错,小数四舍五入)
# %f(默认保留小数后六位,可%.2f精确位数),%%表示一个%
# str='123'
# str2=11
# str3=11.11
# print("%s=%d=%f"%(str,str2,str3))
# f-string输出
# qa='asd'
# b=23
# print(f'{qa}={b}')
# format输出
# qa = 'asd'
# b = 231
# print('{}={}'.format(qa,b))
#9.运算符
# 算术运算符:
# + - *(2**3,表示2的3次方) / % //(直接取整,不是四舍五入)
# print(10+1)
# print(10-1)
# print(10*2)
# print(10/3)
# print(10//3) 输出3
# print(10**3) 输出1000
# print(10000**0.5)输出100
#
# 赋值运算符:
# = += -= *= /= //=
# 比较运算符:
# ==(判读数据类型和值,都相等才true) < > <= >= !=
# 逻辑运算符:
# and or not
# a=11
# b=11
# print(not (a==b or False))
#10.数据类型:
# 1.数字类型:int(正整数,0,负整数) float(正浮点型,负浮点型)
# 类型转换:
# int1='111'
# print(int(int1))
# i='123.1'
# print(float(i))
# a=11.45111
# print(round(a))默认取整(四舍五入),可以round(a,2)保留2位,少于2位就全输出
# a=11.45111
# print('%f'%(a-1))
# 2.字符串类型:str ,可以使用'或者";
# 类型转换:
# a=23;
# print(type(str(a)))
# 字符串改变
# st='hello'
# newst=st.capitalize()首字母大写
# print(newst)
# newst2=st.title()首字母大写
# 全部转换为大写
# print(newst2)
# newst3=st.upper() 全部转换为大写
# print(newst3)
# 全部转换为小写
# newst4=newst3.lower()全部转换为小写
# print(newst4)
# +串联拼接字符串(只能是同意的数据类型)
# st1='????'
# st2='asd'
# print(st1+st2)
# print(st1+'qwe')
# len()获取长度,包括字符数字符号
# st='11 aa ,, !'
# print(len(st))输出10
# count('l',0,4) 统计出现的次数,python中所有取值范围都采用,左闭右开[),这种是默认-1
# st='hello'
# print(st.count('o',0,4))
# 判断开头结尾,返回bool
# st='hello'
# print(st.startswith('h'))判断开头
# print(st.endswith('o'))判断结尾
# find.index 在字符串中查找字符的位置索引,
# 两者区别,出错index会停止,find不会停止,会显示-1;
# # 正数是0,1,2;倒着数是-1,-2,-3
# st='hello'
# # 第一次出现
# print(st.index('l'))
# print(st.find('x'))
# # 最后一次出现
# print(st.rindex('l'))
# print(st.rfind('x'))
# print(st[-1])
# # join
# # 将列表中的字符串拼接成字符串
# li=['a','b','c']
# print(''.join(li))输出abc
# split() 拆分字符串,统计某个字符出现的次数
# st='lololol'
# # print(st.split('l'))输出['', 'o', 'o', 'o', '']
# print(len(st.split('l'))-1)
# print(st.count('l'))
# # replace() 替换字符串
# st='https://www.baidu.com/?tn=62095104_26_oem_dg'
# st1=st.replace('?',' ')
# print(st1)
# st11=st1.replace('=',' ')
# print(st11)
# st111="{"+st11+"}"
# print(st111)
# strip 去除字符串里的开头和结尾的空格和换行,和首尾固定字母(中间不能去除)
# st=' as d'
# print(len(st))
# print(st.strip('d'))
# 判断首字母是否大写,判断是否为空格,判断是否全是大写,小写
# st='aaaa'
# # print(st.istitle())
# # print(st.isspace())
# print(st.isupper())
# print(st.islower())
# 判断是纯数字isnumeric()还是纯字母isalpha() 返回bool,
# 判断有字母或者数字isalnum()
# st='asd.11'
# print(st.isnumeric())
# print(st.isdigit())
# print(st.isdecimal())
#
# print(st.isalpha())
#
# print(st.isalnum())
# 例子
# a=input('a=')
# if (a.isnumeric()):
# print(int(a)+1)
# elif(a.isalpha()):
# print(a+' hello')
# else:
# print(a.upper())
# 字符串索引
# python索引可以反向取值,正数是0,1,2;倒着数是-1,-2,-3
# st='hello zsjs'
# print(st[4])
# print(st[len(st)-1])
# print(st[-1])
# print(st[-len(st)])
# 字符串切片 虽然是[],但是左闭又开[).取不到最后一个
# str='abcdefg'
# print(str[1:])#从索引为1位置切到最后 bcdefg
# print(str[:2])#取索引为2之前的 ab
# print(str[1:4])#只取索引为1,2,3的 bcd
# # 步长
# print(str[::3])# 隔3-1个步 取值 adg
# print(str[1:4:2])#bd
# print(str[::1])# 隔1-1=0个步 取值,就相当于没写 abcdefg
# # 反向
# print(str[::-1])#反向输出 gfedcba
# print(str[1:3:-1])#这样没有结果
# print(str[-1:-3:-1])#这样有结果 gf
# print(str[3::-1])#取索引为3之前的 dcba
# print(str[:3:-1])#取索引为3之前的 gfe
# 3.列表 list
# 4.元组 tuple
# 5.集合 set
# 6.字典 dict
python基础
最新推荐文章于 2023-04-21 16:13:07 发布