一、python基础语法
1、Python注释,python中注释用“#”,windows下选中代码按“ctrl+/”即可注释代码
#注释
#ctrl+/
#print('python!')
#print('hello!')
2、代码分隔,python中代码分隔换行用“\”
# 代码分隔
prin\
t("hello!")
3、文本换行,文本换行用“\n”
# 文本换行
print('hello,\n python!')
4、单行与多行文本的初始化,单行字符串初始化用单引号,多行注释用三引号
# 单行与多行文本的初始化
str1='julyedu.com'
str2='''1-基本python类型、判断与循环流程等
2-python基本练习题
3-文件'''
print(str2)
5、一行里面写多行python代码,用“;”隔开
# 一行里面写多行python代码;
print('hello!');print('python!')
6、缩进,表示代码块
# 缩进
title='www.baidu.com'
score=59
if title=='www.baidu.com':
print('welcome to www.baidu.com')
if score==59:
print('you failed!')
7、空行pass,定义一个空的函数,就可以用pass去做,也可以不写为空
# 空行 pass
def myfunc():
pass
二、标识符的命名
python中的标识符由数字,字母,下划线构成,不能以数字开头命名,不能用class、for等关键字命名,命名时要尽量避开关键字,应尽量做到见名知意。
# 数字,下划线,字母构成
# 不能用class for 等关键字命名,避开关键字
# for='julyedu.com'
# 不能以数字开头
# 1a=8
# 标识符命名更有效率
student_name='david'
# 驼峰命名法
# StudentName
WebsiteUrl='http://www.julyedu.com'
三、标准操作符
1、算数运算符
# 算数运算符
a=100;b=3
a+b;a-b;a*b;a/b
# print(a+b);print(a-b);print(a*b);print(a/b);
# a//b 取商
# a%b 取余
# a**2 2次方
# a**0.5 开方
2、比较运算符
# 比较运算符
# a==b 两个等号是比较,返回bool类型的值
if a==b:
print('a equal b')
# a!=b 不等于
if a!=b:
print('a not equal b')
3、逻辑运算符
# 逻辑运算符
# <,>,>=
if a>=50:
print('a is grater than 50')
4、成员运算符in,not in
# 成员运算符in,not in
websiteUrl='julyedu.com'
if '.com' in websiteUrl:
print('.com in it')
5、赋值运算符
# 赋值运算符
a=100;b=200
# 多重赋值
a=b=100
# 多元赋值,是按照位置赋值的
a,b=100,200
# 交换赋值
a,b=b,a
print(a);print(b)
四、python基本数据类型
python的基本数据类型包括数值型,字符型,列表,元组,集合,字典
1、数值型
# 整型
a=100;b=200
# 进制转换
a=0b0101 #bin()
b=0o4 #oct()
c=0x28 #hex()
# 浮点型
a=3.1415
# 科学记数法
c=5e3
print(c)
# 数字的正负无穷
# float('inf') 正无穷
# float('-inf') 负无穷
if 999999999999999>float('inf'):
print('you win!')
# 复数
a=4.3+22j
type(a)
# 数值类型转换
a=4.444
b=8
int(a)
round(a,1)
2、字符型
# 字符串型
# 初始化及转义,如果引号内有引号就要用\转义
str1=''
str2=''''''
str3='I\'m a student'
# 访问,索引及切片访问
print(str3[0]) #第一个
print(str3[-1]) #最后一个
# 如果访问字符串中的字符串,索引就不适用了,就要用到切片了
print(str3[-3:]) # 切片访问:访问字符串后三位'end'
print(str3[:-4]) # 切片访问:访问字符串'Im a stud'
# str3[start:end:stried] 切片访问
# 切片访问的特点:左闭右开[)
print(str3[0:-1])
# 格式化访问
str3='http://{}.com'
companyName='julyedu'
# 把变量插入到字符串里面
print(str3.format(companyName))
# 查找与替换 tab方法补全
print(str3.find('a'))
# 替换
str4='www.baidu.com'
str4.replace(".com",".net")
print(str4.replace(".com",".net"))
# 统计
print(str4.count)
3、列表[]
# 列表
l1=[1,'china',['AI','www.baidu.com']]
print(l1)
l1.append('python!')
print(l1)
# dir(l1)
# help(l1.append)
# 列表既支持索引又支持切片
print(l1[1])
print(l1[2])
print(type(l1[2]))
4、元组()
# 元组(tuple),只读列表
t1=(1,2,3,[1,2])
print(t1)
dir(t1)
# 赋值
a,b,c,d=t1
print(a)
print(b)
print(c)
print(d)
5、集合set
# 集合set 无序的不重复列表,集合中的对象,通常叫key
s1=(1,1,1,2,3,4,234)
print(s1)
set(s1)
6、字典
# 字典:KV对应型{}dict
d1={'name':'david','gender':'male'}
print(type(d1))
# dir(d1)
print(d1.keys())
print(d1.values())
list(d1.items())
# help(d1.update())
7、声明与表达式
# statement声明,expression表达式
# exec,eval
a='www.baidu.com' #statement
a
exec('aaa=7') #从字符串里执行一个声明语句
aaa
eval('aaa') #从字符串里执行一个表达式
五、判断与循环
# 单if
age=25
if age<20:
print('-----')
# if else
if age<18:
print('teenage')
else:
print('adult')
# 三元表达式
# 'a' if a>50 else 'b'
# For循环与While循环
str1='www.baidu.com'
for item in str1:
print(item)
for item in range(7):
print(item)
i=0;
while True:
print(i)
i+=1
if i==3:
continue
if i>5:
break
六、BIFs自带电池
# BIFs自带电池
locals()
dir(__builtins__)
七、Python运行方式
python运行方式分为两种,一种是交互型,一种是解释型,交互型意思就是在notebook中编写代码,一步一步的执行,解释型的就是在notebook中执行.py文件,在notebook中解释型的运行方式如下:
八、列表推导式List Comprehension
九、可变对象与不可变对象
如下代码,在给a,b,c三个变量付完值以后,如果改变了c的值,并不影响a,b的值
# 可变对象与不可变对象
# 不可变对象
a=b=c=99
c=98
print(a)
print(b)
print(c)
# 可变对象
l1=[1,2,3,4,5,6]
l2=l1
l1.append(7)
print(l2)
![](https://i-blog.csdnimg.cn/blog_migrate/4b2efb64478a5c921b0f21e464893bb5.png)