什么是python以及特点?
1、python是一门编程语言。
2、简洁、优雅、灵活、程序编写容易阅读。
3、跨平台的,可以运行在不同平台。windows\mac\ubuntu
4、易于学习。
5、极为强大的的标准库及第三方库。
6、python是面向对象语言。
python可以实现什么
1、爬虫
2、大数据与数据分析
3、自动化运维与自动化测试
4、WEB编程开发:FLASK、Django
5、机器学产:Tensor Flow
6、胶水语言。
代码是什么?
代码是实现世界事物在计算机中的映射。
python的基本数据类型
整数:int
浮点数:float
其他语言:单精度(float),双精度(double)
其他:short in
二进制 8进制16进制
二进制(0b)0b10=2 0b11=3
8进制 0o10 =8 0o11=9
16进制 0x1F=31 0x0F=15
进制之间的相互转换
bin(10) 其他转为二制
int(0x1f) 其他传为十制
hex(0b1111)其他转为十六进制
oct(12)其他转为八进制
Number:数字
bool 布尔类型 :表为真与假 (True 和False) bool(1) True ; bool(0)=False,bool(2.1)=True bool([])=False 所有空为假,非空为真
complex 复数
字符串
str 字符串
单引号、双引号、三引号
\n代表回车
转义字符
特殊的字符
无法“看见”的字符 与语言本身语法有冲突的字符
\n 换行
\' 单引号
\t 横向制表符
\r 回车
print("hello \\n world") 或者 print(r"hello \n world") -r不是一个普通字符串,而是一个原始字符串
字符串的运算处理
'hello' [0] h 从0开始 从左开始
'hell0'[-3] l 从右开始
'hello world'[6:11] #word [6:20] [6:] [-5:]
基本数据类型
["hellyou","world",[1,3],[4,3]]
type(["hellyou","world",[1,3],[4,3]])
<class 'list'>
["中国","俄罗斯","韩国","日本"]+["新加坡"] #可以+或* 不能-
["中国","俄罗斯","韩国","日本"]*2
["中国","俄罗斯","韩国","日本"][1:]
['俄罗斯', '韩国', '日本']
元组
(1,2,3,4)[0]
(1,2,3,4)[1:3]
(2, 3)
(1,2,3,4)[1:4]
(2, 3, 4)
type((1,2,3,4))
<class 'tuple'>
type((1)) 是int ()代表运算符,
type((1,))是代表一个字符元组,空元组用type(())表示
3 in [1,2,3]
True
3 not in [1,2,3]
False
len ([1,2,3,4,5])
5
max ([1,2,3,4,5])
5
min ([1,2,3,4,5])
1
ord('w')
119
集合set (无序、不重复){}
不支持切片{1,2,3,3,3,2,2,1}[2:]
{1,2,3,3,3,2,2,1}
{1, 2, 3}
len({1,2,3})
1 in {1,2,3}
True
{1,2,3,4,5,6} -{3,4}
{1, 2, 5, 6}
{1,2,3,4,5,6} -{3,4} 差集
{1,2,3,4,5,6}&{3,4,}交集
{1,2,3,4,5,6} |{3,4,7}合集
{1, 2, 3, 4, 5, 6, 7}
set() 定义一个空的集合
type (set())
<class 'set'>
字典
key value (key)不可变的类型
type({1:1,2:2,3:3})
<class 'dict'>
{1:"中国","1":"俄罗斯","3":"韩国","4":"日本"}
{1: '中国', '1': '俄罗斯', '3': '韩国', '4': '日本'}
{1:"中国","1":"俄罗斯","3":"韩国","4":"日本"} [1]
'中国'
{1:"中国","1":"俄罗斯","3":"韩国","4":"日本"} ["1"]
'俄罗斯'
变量
1、变量命名规范
(1)、包含字母、数字、下划线
(2)、变量名尽量有意义。
2、变量不能以数字开头。
int str tuple(不可改变类型) 值类型 list set dict (可变)引用类型。
int float 0 表示为False 非0为True
not 0
True
not 1
False
字符串 空字符串为False,其他为True
not ' d'
False
not ' 2'
False
not 0
True
not '0'
False
[]空的列表为False , 其他为True
b in {'c':1}
True
身份运算符
a=1
b=2
a is b
False
例2
a=1
b=1.0
a==b
True
a is b
False
id (a)
1740996720
id(b)
2059589919200
is 是对比的内存地址,
例:
a={1,2,3}
b={2,1,3}
a==b
True
a is b
False
a=(1,2,3)
b=(2,1,3)
a==b
False
a is b
False
对象的三个特征:id (身份),value(值),type(类型)
is == isinstance
a="hell0"
isinstance(a,str)
True
isinstance(a,(int,str))
True
按位运算
a=2
b=3
a&b
结果2
a|b
3
类、对象
类
class Student():
name = ''
age = 0
def print_file(self):
print('name:' + self.name)
print('age:' + str(self.age))
#student = Student()
#student.print_file()
方法:设计层面
函数:程序运行、过程式的一种称谓
类与对象
类是实现世界或思维世界中的实体在计算机中的反映,它将数据以及这些数据上的操作封装在一起。
类是一个模板,可以产生许多对像。
行为和特征
class Student():
name = ''
age = 0
def __init__(self, name, age): #构造函数
#print("student")
name = name
age = age
def homework(self):
print("homework")
#print('name:' + self.name)
#print('age:' + str(self.age))
#student = Student()
#student.print_file()
student1 = Student("kk", 11)
a = student1.__init__("dc", 12)
print(student1.name)
类变量和实例变量
def __init__(self, name, age): #构造函数
#print("student")
self.name = name
self.age = age