python
解释型语言
面向对象语言
跨平台语言
简单,使程序员集中精力于解决问题而不是语言本身。
对比:
Perl只适合小的程序,大的程序就要用python。
配置环境Windows下:
1 去官网下载合适的python版本 下载
2 傻瓜式安装,并配置系统变量path值为python的目录
使用:
控制行,输入python
或者
开始——》程序——》python——》IDLE(python GUI)
如何运行一个python文件:eg: a.py
进入文件的目录,python a.py
即可。
>>> print('hello\'world')
hello'world
>>> print("hello")
hello
>>> print('hello")
SyntaxError: EOL while scanning string literal
>>> print('he"l')
he"l
>>> print("hello ' world!")
hello ' world!
>>> 1 + 1
2
>>> 1 * 2
2
>>> 2-3
-1
>>> 3/4
0.75
>>> 3//4
0
>>> 3%4
3
>>> 3**3
27
>>> a = 10
>>> while a < 20:
a = a + 1
print(a)
11
12
13
14
15
16
17
18
19
20
>>> print("hello python",end='!')
hello python!
>>> print("hello python",end=',')
hello python,
>>> a = 10
>>> while a < 20:
a = a+2
print(a,end=" ")
12 14 16 18 20
在已有的文件中追加
类
__init__():python中类的构造器,双下划线
元组
列表
列表中的常用方法
字典还可以以更多样的形式出现,例如字典的元素可以是一个List,或者再是一个列表,再或者是一个function。索引需要的项目时,只需要正确指定对应的
解释型语言
面向对象语言
跨平台语言
简单,使程序员集中精力于解决问题而不是语言本身。
对比:
Perl只适合小的程序,大的程序就要用python。
配置环境Windows下:
1 去官网下载合适的python版本 下载
2 傻瓜式安装,并配置系统变量path值为python的目录
使用:
控制行,输入python
或者
开始——》程序——》python——》IDLE(python GUI)
如何运行一个python文件:eg: a.py
进入文件的目录,python a.py
即可。
>>> print('hello\'world')
hello'world
>>> print("hello")
hello
>>> print('hello")
SyntaxError: EOL while scanning string literal
>>> print('he"l')
he"l
>>> print("hello ' world!")
hello ' world!
>>> 1 + 1
2
>>> 1 * 2
2
>>> 2-3
-1
>>> 3/4
0.75
>>> 3//4
0
>>> 3%4
3
>>> 3**3
27
>>> a = 10
>>> while a < 20:
a = a + 1
print(a)
11
12
13
14
15
16
17
18
19
20
>>> print("hello python",end='!')
hello python!
>>> print("hello python",end=',')
hello python,
>>> a = 10
>>> while a < 20:
a = a+2
print(a,end=" ")
12 14 16 18 20
>>>
文件读写
写
my_file=open('my file.txt','w') #用法: open('文件名','形式'), 其中形式有'w':write;'r':read.
my_file.write(text) #该语句会写入先前定义好的 text
my_file.close()
在已有的文件中追加
append_text='\nThis is appended file.' # 为这行文字提前空行 "\n"
my_file=open('my file.txt','a') # 'a'=append 以增加内容的形式打开
my_file.write(append_text)
my_file.close()
""""
This is my first test.
This is the second line.
This the third line.
This is appended file.
""""
#运行后再去打开文件,会发现会增加一行代码中定义的字符串
文件读
file= open('my file.txt','r')
content=file.read()
content=file.readline() # 读取第一行
content=file.readlines() # 读所有行
print(content)
""""
['This is my first test.\n', 'This is the second line.\n', 'This the third line.\n', 'This is appended file.']
"""
# 之后如果使用 for 来迭代输出:
for item in content:
print(item)
"""
This is my first test.
This is the second line.
This the third line.
This is appended file.
"""
类
class Calculator: #首字母要大写,冒号不能缺
name='Good Calculator' #该行为class的属性
price=18
def add(self,x,y):
print(self.name)
result = x + y
print(result)
def minus(self,x,y):
result=x-y
print(result)
def times(self,x,y):
print(x*y)
def divide(self,x,y):
print(x/y)
""""
>>> cal=Calculator() #注意这里运行class的时候要加"()",否则调用下面函数的时候会出现错误,导致无法调用.
>>> cal.name
'Good Calculator'
>>> cal.price
18
>>> cal.add(10,20)
Good Calculator
30
>>> cal.minus(10,20)
-10
>>> cal.times(10,20)
200
>>> cal.divide(10,20)
0.5
>>>
""""
__init__():python中类的构造器,双下划线
class Calculator:
name='good calculator'
price=18
def __init__(self,name,price,hight=10,width=14,weight=16): #为后面三个属性设置默认值,查看运行
self.name=name
self.price=price
self.h=hight
self.wi=width
self.we=weight
""""
>>> c=Calculator('bad calculator',18)
>>> c.h
10
>>> c.wi
14
>>> c.we
16
>>> c.we=17
>>> c.we
17
""""
input获取键盘输入
score=int(input('Please input your score: \n'))
if score>=90:
print('Congradulation, you get an A')
elif score >=80:
print('You get a B')
elif score >=70:
print('You get a C')
elif score >=60:
print('You get a D')
else:
print('Sorry, You are failed ')
""""
Please input your score:
100 #手动输入
Congradulation, you get an A
""""
元组和列表
元组
tuple
,用小括号、或者无括号来表述,是一连串有顺序的数字
a_tuple = (12, 3, 5, 15 , 6)
another_tuple = 12, 3, 5, 15 , 6
列表
list
是以中括号来命名的:
a_list = [12, 3, 67, 7, 82]
for index in range(len(a_list)):
print("index = ", index, ", number in list = ", a_list[index])
"""
index = 0 , number in list = 12
index = 1 , number in list = 3
index = 2 , number in list = 67
index = 3 , number in list = 7
index = 4 , number in list = 82
"""
for index in range(len(a_tuple)):
print("index = ", index, ", number in tuple = ", a_tuple[index])
"""
index = 0 , number in tuple = 12
index = 1 , number in tuple = 3
index = 2 , number in tuple = 5
index = 3 , number in tuple = 15
index = 4 , number in tuple = 6
"""
列表中的常用方法
a.append(0) # 在a的最后面追加一个0
a.insert(1,0) # 在位置1处添加0
a.remove(2) # 删除列表中第一个出现的值为2的项
a = [1,2,3,4,1,1,-1] print(a[0]) # 显示列表a的第0位的值 # 1 print(a[-1]) # 显示列表a的最末位的值 # -1 print(a[0:3]) # 显示列表a的从第0位 到 第2位(第3位之前) 的所有项的值 # [1, 2, 3] print(a[5:]) # 显示列表a的第5位及以后的所有项的值 # [1, -1] print(a[-3:]) # 显示列表a的倒数第3位及以后的所有项的值 # [1, 1, -1]
print(a.index(2)) # 显示列表a中第一次出现的值为2的项的索引
print(a.count(-1))#统计-1出现的次数
a.sort() # 默认从小到大排序
a.sort(reverse=True) #从大到小排序还有多维列表
字典
a_list = [1,2,3,4,5,6,7,8]
d1 = {'apple':1, 'pear':2, 'orange':3}
d2 = {1:'a', 2:'b', 3:'c'}
d3 = {1:'a', 'b':2, 'c':3}
del d1['pear']
print(d1)
# {'orange': 3, 'apple': 1}
d1['b'] = 20
print(d1)
# {'orange': 3, 'b': 20, 'pear': 2, 'apple': 1}#这说明字典是一个无序的容器,列表是。
字典还可以以更多样的形式出现,例如字典的元素可以是一个List,或者再是一个列表,再或者是一个function。索引需要的项目时,只需要正确指定对应的
key
就可以了。
下载的python模块会被存储到外部路径site-packages
,同样,我们自己建的模块也可以放到这个路径,最后不会影响到自建模块的调用。
set 去重
char_list = ['a', 'b', 'b', 'c', 'c', 'd']
print(set(char_list))
a b c d
unique_char = set(char_list) unique_char.add('x')这是在set中添加一个元素。
unique_char.clear() # 使用这种方式将直接清空set unique_char.remove('y') # 使用这种方式,如果set中不含有要remove的元素,则程序会报错 unique_char.discard('y') # 使用这种方式删除元素,即使set中不含有要discard的元素,也不会报错,只会返回原字符串。
除了上面的几种用法,还有一种可以比较两个set的方法:
set1 = unique_char set2 = {'a', 'e', 'i'} print( set.difference(set2) )