1.输入输出
输出:
print(''),print('xixi','haha')
会依次打印每个字符串,遇到逗号“,”会输出一个空格
print(100+200)
输入:input()函数
举例:name=input()
print(name)
>>>name
'xxx'
print("请输入您的名字!") name=input() print('hello',name)
2.数据类型
整型,浮点型,
字符串:
含有 ' 用 " "包括字符串
print("I'm OK")
含有"的字符串,转义字符
print('I\'m \"OK\"')
转义字符\
可以转义很多字符,比如\n
表示换行,\t
表示制表符,字符\
本身也要转义,所以\\
表示的字符就是\
print('hello boys\n girls')
hello boys
girls
print('hello\they')
hello hey
print('\\hey\\')
\hey\
>>> print('\\\t\\') \ \ >>> print(r'\\\t\\') \\\t\\
如果字符串内部有很多换行,用\n
写在一行里不好阅读,为了简化,Python允许用'''...'''
的格式表示多行内容
>>> print('''line1 ... line2 ... line3''') line1 line2 line3
>>> print(r'''hello,\n ... world''') hello,\n world >>> print(r'''hello,\n world''') hello,\n world
布尔值(true,false),and or not
>>> 3>2 True >>> 3<1 False
空值
none,不能用0表示,0有意义
变量
动态语言,变量本身类型不固定
常量
两种除法 //取整
>>> print(10/3) 3.3333333333333335 >>> print(10//3) 3
%取余数
2.字符编码???
由于Python的字符串类型是str
Python对bytes
类型的数据用带b
前缀的单引号或双引号表示:
要注意区分'ABC'
和b'ABC'
,前者是str
,后者虽然内容显示得和前者一样,但bytes
的每个字符都只占用一个字节。
以Unicode表示的str
通过encode()
方法可以编码为指定的bytes
对于单个字符的编码,Python提供了ord()
函数获取字符的整数表示,chr()
函数把编码转换为对应的字符:
要把bytes
变为str
,就需要用decode()
方法
如果bytes
中只有一小部分无效的字节,可以传入errors='ignore'
忽略错误的字节:
>>> b'ABC'.decode('ascii') 'ABC' >>> 'ABC'.encode('ascii') b'ABC' >>> b'\xe4\xb8\xad\xff'.decode('utf-8', errors='ignore') '中'
要计算str
包含多少个字符,可以用len()
函数:
>>> len('ABC') 3
1个中文字符经过UTF-8编码后通常会占用3个字节,而1个英文字符只占用1个字节
避免乱码
#!/usr/bin/env python3 # -*- coding: utf-8 -*-
格式化
内容都是根据变量变化的,所以,需要一种简便的格式化字符串的方式
在Python中,采用的格式化方式和C语言是一致的,用%
实现
>>> 'hello,%s' % 'world' 'hello,world' >>> 'hi %s,you have$%d' % ('micheal',100000) 'hi micheal,you have$100000'
print('hi %s,you have $%d' % ('tom',1000)) print('Hello, %s' % 'world')
其中,格式化整数和浮点数还可以指定是否补0和整数与小数的位数
print('%2d-%02d' %(3,1)) print('%.2f' % 3.1415926)
3-01
3.14
如果你不太确定应该用什么,%s
永远起作用,它会把任何数据类型转换为字符串
字符串里面的%
是一个普通字符怎么办?这个时候就需要转义,用%%
来表示一个%
:
另一种格式化字符串的方法是使用字符串的format()
方法,它会用传入的参数依次替换字符串内的占位符{0}
、{1}
……,不过这种方式写起来比%要麻烦得多:
print('hello,{0},成绩提升了{1:.1f}%'.format('小明',17.125))
list类型【】
classmate=['dog','cat','sheep']
print(classmate)
print(len(classmate))
print(classmate[0])
print(classmate[-1])
如果要取最后一个元素,除了计算索引位置外,还可以用-1
做索引,直接获取最后一个元素:
以此类推,可以获取倒数第2个、倒数第3个
print(classmate[-2])
list是一个可变的有序表。可以追加元素
classmate.append('pig')
可以把元素插入到指定位置
classmate.insert(1,'jackson') print(classmate)
['dog', 'jackson', 'cat', 'sheep']
要删除list末尾的元素,用pop()
方法
classmate.pop() print(classmate) ['dog', 'jackson', 'cat', 'sheep'] ['dog', 'jackson', 'cat']
要删除指定位置的元素,用pop(i)
方法,其中i
是索引位置
classmate.pop(0)
要把某个元素替换成别的元素,可以直接赋值给对应的索引位置
classmate[1]='duck'
list里面的元素的数据类型也可以不同,list元素也可以是另一个list
p=['asp','php'] s=['python','java',p] print(s[2][1]) php
要拿到'php'
可以写p[1]
或者s[2][1]
,因此s
可以看成是一个二维数组
如果一个list中一个元素也没有,就是一个空的list,它的长度为0
可以通过len()取数组长度
2.tuple()
另一种有序列表叫元组:tuple。tuple和list非常类似,但是tuple一旦初始化就不能修改
classmate=('haha','xixi') print(classmate) t=() print(t) p=(1,) print(p)
单个元素第一个后面加,避免歧义
实例属性和类属性
实例属性属于各个实例所有,互不干扰;
类属性属于类所有,所有实例共享一个属性;
class Student(object): count = 0 def __init__(self, name): self.name = name Student.count += 1
pillow图像处理
import numpy as np from PIL import Image,ImageFilter im=Image.open('timg.jpg') im2=im.filter(ImageFilter.BLUR) im2.save('image.jpg','jpeg')
requests
import requests r = requests.get('https://www.douban.com/') # 豆瓣首页 print(r.status_code) '''print(r.text)'''
GUI
from tkinter import * class Application(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.pack() self.createWidgets() def createWidgets(self): self.helloLabel = Label(self, text='Hello, world!') self.helloLabel.pack() self.quitButton = Button(self, text='Quit', command=self.quit) self.quitButton.pack() app = Application() # 设置窗口标题: app.master.title('Hello World') # 主消息循环: app.mainloop()