1.注释
注释是我们自己语言来描述一段代码的实现逻辑,介绍这段代码具体实现的什么功能,方便我们维护和清晰地了解这段代码,增强程序的可读性1.1、单行注释 :用于注释一行文字或者代码,单行注释格式以 # 号开头
# 打印一行文字
print("hello world")
# print("hello world")1.2、多行注释 :用于注释多行文字或者代码块,格式以三个英文单引号(''')开头,三个英文单引号(''')结束
'''使用多行注释打印一行文字'''
print("hello world!")
2.关键字与标识符2.1、关键字
False、None、True、and、as、assert、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、yield2.2、标识符
开发者自定义的一些符号和名称等称为标识符(如:变量名、函数名、类名等),标识符由字母、数字、下划线组成。
定义标识符的注意事项:不能以数字开头
标识符区分大小写
不能使用Python内置关键字作为标识符名称
为了提高可读性,见名知其意
3.变量
变量就是一个装有不同类型数值的容器3.1、变量定义
#定义姓名变量
name="张三"
#定义年龄变量
age=20
#定义性别变量
sex="男"
#打印信息
print("姓名: ",name)
print("年龄: ",age)
print("性别: ",sex)3.2、赋值
# 如果不是第一次出现,表示给这个变量重新赋值
#定义年龄变量
age=20
print("年龄: ",age)
#年龄增加2岁
age=age+2;
print("重新赋值后的年龄: ",age)
4.数据类型
python提供的标准数据类型:数据类型(number)、字符串类型(string)、列表(list)、元组(tuple)、字典(dictionary)、集合(set),
数据类型包括:整型(int)、浮点型(float)、复数类型(complex)。
python中定义的变量不需要显示指定数据类型,解释器会根据变量值自动推断该变的数据类型4.1、标准数据类型
#整型
a=10
#浮点型
b=1.23
#复数类型
aComplex = 1.56 + 1.2j
#字符串
str="abc"
#列表
list=["a","b","c"]
#元组
tup = "a", "b", "c", "d"
#字典
dict = {'a': 1, 'b': 2, 'b': '3'}
#集合
set={"a","b"}4.2、类型转换函数函数名说明
int(x)将对象x转换为整型
float(x)将对象x转换为浮点型
str(x)将对象x转换为字符串类型
tuple(a)将序列a转换为元组
list(a)将序列a转换为列表
set(a)将序列a转换为集合,并对序列a中元素去重4.3、布尔类型:只有True和False,分别表示真和假,python中的布尔类型值首字母要大写
5.输入与输出5.1、输入函数(input)
#接受用户输入
name=input("请输入姓名:")
age=input("请输入年龄:")
#打印信息
print("姓名: ",name)
print("年龄: ",age)5.2、输出函数(print)
#打印变量
str="hello world"
print(str)
#打印多个变量
a=1
b=2
print(a,b)
#无换行打印(python中print函数默认参数end='\n',打印后换行)
print("hello",end="")
print("world",end="")
print("!",end="")
#转义字符打印
#没使用转义字符
print("hello\nworld")
#使用转义字符
print("hello\\n world")
6.运算符6.1、算术运算符 :运算符说明
+加
-减
*乘
/除
**幂
%取模
//取整6.2、比较运算符 :运算符说明
==等于
!=不等于
>大于
>=大于等于
<=小于等于6.3、赋值运算符 :运算符说明
=a=1,表示给变量a赋值为1
+=a=1,a+=5(a=a+5);结果:a=6
-=a=1,a-=1(a=a-1);结果:a=0
*=a=1,a*=5(a=a*5);结果:a=5
/=a=10,a/=5(a=a/5);结果:a=2
%=a=10,a%=3(a=a%3);结果:a=1
//=a=10,a//=3(a=a//3);结果:a=36.4、逻辑运算符运算符说明
not非运算
and与运算
or或运算6.5、运算符优先级运算符优先级算术运算符 > 比较运算符 > 逻辑运算符
算术运算符优先级幂 >乘、除、取模、取整 >加、减
7.字符串7.1、字符串定义和格式化
# 定义字符串变量(一对单引号或者一对双引号括起来)
a="hello world"
b='hello world'
'''字符串格式化1. 使用+号拼接2. 使用格式化符号3. 使用format格式化函数'''
#使用+号拼接
str="hello"+"--"+"world"
print(str)
#使用格式化符号
name="张三"
print("姓名:%s"%name)
age=20
print("年龄:%d"%age)
high=175.5
print("身高:%f"%high)
print("姓名:%s,年龄:%d,身高:%f"%(name,age,high))
# 浮点类型保留1位小数
print("姓名:%s,年龄:%d,身高:%.1f"%(name,age,high))
#使用format格式化函数
print("姓名:{},年龄:{},身高:{:.1f}".format(name,age,high))7.2、字符串内置方法
# 1.find(str[,satrt,end])
str="hello world"
print(str.find("he")) #查找he字符
print(str.find("l")) #查找l字符,返回第一个l的下标
print(str.find("l",4)) #从下标4开始向后找l字符
print(str.find("nihao")) #没找到字符串返回-1
# 2.count(str[,start,end])
str="hello world"
print(str.count("l")) #str中包含l字符的个数
# 3.replace(old,new[,count])
str="hello world"
print(str.replace("l","x")) # 替换str中所有l字符串为x字符串
print(str.replace("l","x",1)) # 只替换str中的一个l字符串为x字符串
# 4.split(sep[,maxsplit])
str="hello world 你好吗"
print(str.split(" ")) #按照空格分割字符串
print(str.split(" ",1)) #按照空格分割字符串,只对第一个空格分割
# 5.startswith(prefix[,start,end]) 与 endswith(suffix[,start,end])
str="hello world"
print(str.startswith("hello")) #str字符串是否以hello开头
print(str.endswith("world")) #str字符串是否以world结尾
# 6.upper()与lower()
str="hEllo woRld"
print(str.upper()) #将str字符串所有字符大写
print(str.lower()) #将str字符串所有字符小写
# 7.join(序列)
list=["a","b","c"]
print("-".join(list)) #将序列list中所有元素以-拼接成一个字符串
# 8.strip()
print(" abc ".strip()) #去除头尾的空白字符
print("\nabc\n".strip()) #去除制表符
print("\tabc\t".strip()) #去除换行符
print("--abc--".strip("-")) #去除头尾的-字符
8.if条件判断语法格式
if 表达式1:
......
elif 表达式2:
......
elif 表达式3:
......
elif 表达式4:
......
else:
......
if条件判断中,if语句是必选表达式,elif语句是可选表达式,可以有多个elif语句,else语句是可选表达式,有且只能有一个else语句。
age=10
if age < 18:
print("未成年人")
else:
print("成年人")
9.while循环语法格式
while 表达式:
.....
num =1
while num < 10: #当num大于10时退出循环
print(num)
num+=1break跳出while循环
i=1
while i<10:
if i==3:
# 遇到i=3时退出
break
print(i)
i+=1continue跳出当次循环
i=1
while i<10:
if i%2==0:
# 遇到偶数跳过
continue
print(i)
i+=1
10.for循环语法格式:
for 临时变量 in 序列:
......break跳出for循环
for i in range(0,10):
if i==3:
#当i=3时跳出for循环
break
print(i)continue跳出当次for循环
for i in range(0,10):
if i%2==0:
#当i是偶数时跳出当次for循环
continue
print(i)blog地址:随笔记starsea.51vip.biz
更多推荐
true可以作为python标识符吗_【Python自学记】2.基础
发布评论