Python学习笔记
2021/12/6
注释
注释以#开头,python将忽略这一行
单行注释:
# 单行注释
多行注释:
# 多行注释1
# 多行注释2
# 多行注释3
变量
python只需要在给对象赋值的时候创建对象,并不需要给变量指定任何特定类型的声明,甚至可以在设置后过呢更改类型。
eg:
x = 5 # x is of type int
x = "Steve" # x is now of type str
print(x)
··················································
字符串变量可以用单引号/双引号进行声明
x = "Bill"
# is the same as
x = 'Bill'
··················································
可以一行中为多个变量赋值
x, y, z = "Orange", "Banana", "Cherry"
··················································
函数的定义:
def 函数名:
(空格)函数体
注意:在python中空格是控制代码块的
··················································
局部变量:
当函数内外同时存在一个相同名称的变量的时候,函数中调用的是函数内部的变量。
eg:
x = "awesome" #全局变量
def myfunc():#函数的定义
x = "asd"
print("Python is " + x)
myfunc() #调用myfunc函数,输出Python is asd #
print("Python is " + x) #Python is awesome
··················································
全局变量
在函数外部创建的变量称之为全局变量,全局变量可以在任何地方使用
在函数内部创建的变量,在使用global关键字声明后,该变量属于全局范围
eg:
def myfunc():
global x
x = "fantastic"
print("Python is " + x) #Python is fantastic
myfunc()
print("Python is " + x) #Python is fantastic
另外,如果要在函数内部改变全局变量的值,使用global关键字引用该变量
eg:
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x) #Python is fantastic
数据类型
设置数据类型
设定特定的数据类型
如果希望指定数据类型,泽可以使用一下的构造函数:
数字
三种数字类型
int(整数) 整数或负数,长度不限 x = 10
float(小数) 包含小数的正数或负数 y = 2.1
complex(复数) 用"j"作为虚部编写 z= 2i
验证python中任何对象的类型,使用type()函数
eg:
x = 10
print(type(x))
··················································
类型转换
eg:
x = 10 #整型
y = 1.2#小数
z = 1j#复数
a = float(x)#将整数转换成小数
b = int(y)#将小数转换成整数
c = complex(x)#将整数转换成复数
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
2021/12/7
布尔
当对两个数据进行比较的时候,返回的值就是true或false
··················································
字符串:除空字符串外,均为true
数字:除0外,均为true
除空列表外,任何列表、元组、集合和字典均为true
··················································
··················································
注意:当一个值或者对象的计算结果为false,即如果对象由带有_len_函数的类生成的,且该函数返回0或者false
eg:
class myclass():
def __len__(self):
return 0
myobj = myclass()
print(bool(myobj))
··················································
instance()函数:可以用于阙清对象是否具有某种数据类型
eg:
x = 200
print(instace(x,int))#判断x是不是int类型的数据
运算符
算术运算符
赋值运算符
比较运算符
逻辑运算符
身份运算符
成员运算符
位运算符
算术运算符
+ 加
- 减
* 乘
/ 除
整除会显示
x.0
若有小数会显示
x.(小数点后16位)
% 取余
** 幂
// 整除
赋值运算符(感觉和java很相似)
下图是对&=、|=、^=、>>=、<<=的补充说明
比较运算符
逻辑运算符
身份运算符
成员运算符
位运算符
集合
概述
列表list 有序,可更改,允许重复
元素用[]进行包裹,每个元素用"",包裹,之间用,隔开
eg:
x = ["apple","banana"]
元组tuple 有序,不可更改,允许重复
元素用()包裹
eg:
x = ("apple","banana")
集合set 无序,无索引,没有重复
元素用{}包裹
eg:
x = {"apple","banana"}
字典dictionary 无序,可更改,有索引,没有重复的元素
元素用{}包裹,key:value的形式,个人感觉像Java的map
eg:
x = {"name":"Bill","age":18}
list列表
遍历列表:(for+in关键字)
eg:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
··················································
检查项目是否存在(if+in关键字)
eg:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
··················································
列表长度
eg:
thislist = ["apple","banana"]
print(len(thislist)) #2
··················································
添加:
append()方法(在最后面进行追加)、insert()方法(在指定索引位置添加,列表名.insert(索引, 要添加的数据) )