python程序组成
程序:由注释和模块组成
模块:由语句、函数、类组成
函数:由注释和语句组成
语句:包含表达式
表达式:建立并处理数据对象
单行注释: 以
多行注释: 以三引号开头和三引号结尾;三单和三双都行,就是颜色不一样
vim写脚本格式
kylin@kylin-PC:~$ vim hello.py
print ("hello word")
kylin@kylin-PC:~$ chmod +x hello.py
kylin@kylin-PC:~$ ./hello.py
hello word
print的几种用法
1.输出数字
>>> print (12)
12
2.输出字母
>>> print("a")
a
3.输出汉字
>>> print("毕润林")
毕润林
4.输出变量
>>> x = 12
>>> print(x)
12
5.输出多个值
>>> x = 12
>>> print (x,"x")
12 x
6.修改输出的间隔符 sep
>>> print("tom","jerry","hh")
tom jerry hh
>>> print("tom","jerry","hh",sep=" ")
tom jerry hh
>>> print("tom","jerry","hh",sep=":")
7.修改输出换行符 end
>>> print("tom","jerry"),print("hello")
tom jerry
hello
>>> print("tom","jerry",end="*"),print("hello")
tom jerry*hello
8.改变输出样式 \t和\n
1.\t 默认等于四个空格
>>> print("a\tb\tc\td")
a b c d
2.\n 换行
>>> print("a\nb\nc\nd")
a
b
c
d
input的用法
让程序停下来,等待用户输入信息,返回用户输入的信息,默认把用户输入的信息规划为字符类型数据
1.基本输入
>>> input()
毕润林
'毕润林'
2.添加提示
>>> input("请输入你的名字:")
请输入你的名字:毕润林
'毕润林'
3.把用户输入的信息保存
>>> name=input("请输入你的名字:")
请输入你的名字:毕润林
>>> print(name)
毕润林
4.把输入类型转换成整型
>>> num=int(input("请输入数字:"))
请输入数字: 22
>>> print(num)
22
>>> type(num)
<class 'int'>
type查看数据类型
注:想输出字符时,加上引号,单引号、双引号、三引号都可以
>>> print (type(x))
<class 'int'>
>>> print (type("x"))
<class 'str'>
>>> print (type("1"))
<class 'str'>
引号的区别
- 双引号字符串内部的单引号不算是结束符
- 单引号字符串内部的双引号不算是结束符
#只是为了能输出想要的引号
1.双引套单引
>>> print ("I'm a boy")
I'm a boy
2.单引套双引
>>> print ('"种花家"')
"种花家"
3.三引号
>>> print ('"种花家"')
"种花家"
>>> print('''此生无悔\t入华夏\n来生还是\t种花家''')
此生无悔 入华夏
来生还是 种花家
变量的定义
变量起名规定:
第一个字母只能是大小写字母或下划线
后续字符只能是大小写字母、数字或下划线
Python是动态类型语言,不需要预先声明变量类型
变量在使用前,必须赋初值
Python的关键字

变量赋值格式
1.给字符赋值
>>> name="a"
>>> print(name)
a
2.计算结果赋值
>>> num=3+4
>>> print(num)
7
>>> num=3<4
>>> print(num)
True
>>> num=3>4
>>> print(num)
False
3.多个变量同时赋值
>>> x=y=z=11
>>> print(x,y,z)
11 11 11
运算符
标准算数运算符
+
>>> 10+1
11
>>> "a"+"b"
'ab'
-
>>> 10-1
9
*
>>> 10*2
20
/
>>> 10/3
3.3333333333333335
//
>>> 10//3
3
%
>>> 10%3
1
**
>>> 2**3
8
+=
>>> x=1
>>> x+=1
>>> print(x)
2
-=
>>> x=2
>>> x-=1
>>> print(x)
1
*=
>>> x=2
>>> x*=2
>>> print(x)
4
/=
>>> x=10
>>> x/=3
>>> print(x)
3.3333333333333335
//=
>>> x=10
>>> x//=3
>>> print(x)
3
%=
>>> x=10
>>> x%=3
>>> print(x)
1
**=
>>> x=2
>>> x**=3
>>> print(x)
8
比较运算符
<
<=
>
>=
==
!=
逻辑运算符
and
>>> 3<4 or 5<6
True
not
>>> "w" not in 'abc'
True
or
>>> 3<4 or 5>6
True
>>> 1 == 1 or 2 < 3 and 5 > 6
True
数据类型
数字类型
int 有符号整数
float 浮点数
bool 布尔值
-True 1
-False 0
- 默认以十进制显示
- 数字以0o或00开头表示八进制
- 数字以0x或0X开头表示十六进制
- 数字以0b或0B开头表示二进制
字符串str
python中字符串被定义为引号中的字符集合
Python中支持使用成对的单引号或双引号
单引号和双引号表示的意义相同
支持三引导,用来包含特殊字符
不区分字符和字符串
字符串切片
>>> print(string)
abcdefg
>>> string[0]
'a'
>>> string[-1]
'g'
>>> string[-2]
'f'
>>> string[0:4]
'abcd'
>>> string[2:]
'cdefg'
>>> string[-4:-1]
'def'
字符串连接
- 使用+号可以将多个字符串拼接在一起
* 可以将一个字符串重复多次
>>> a="abc"
>>> b="ABC"
>>> c=a+b
>>> print(c)
abcABC
>>> print("毕润林真帅\n" * 5)
毕润林真帅
毕润林真帅
毕润林真帅
毕润林真帅
毕润林真帅
元组
>>> s=('abc','ABC')
>>> type(s)
<class 'tuple'>
>>> print(s[0])
abc
>>> s[0]="aaa"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
列表(数组)
1.基本使用
>>> s2=['abc','ABC']
>>> type(s2)
<class 'list'>
>>> print(s2[0])
abc
>>> s2[0]="aaa"
>>> print(s2[0])
aaa
>>> print(s2)
['aaa', 'ABC']
2.空列表的使用
>>> L = []
>>> L2=list()
<class 'list'>
>>> type(L)
<class 'list'>
>>> type(L2)
<class 'list'>
>>> L.append('brl')
>>> print(L)
['brl']
>>> print(L[0])
brl
3.一个列表里面添加多种不同的数据
>>> l3=[1.1,'abc',True,['brl','xm']]
>>> print(l3[0],l3[1],l3[2],l3[3],l3[3][0],l3[3][1])
1.1 abc True ['brl', 'xm'] brl xm
4.列表切片
1. : 列表切片
>>> print(l3)
[1.1, 'abc', True, ['brl', 'xm']]
>>> print(l3[0:2])
[1.1, 'abc']
2.使用len函数切片
>>> len(test)
4
>>> test[:len(test)]
['a', 'b', 'c', [1, 2]]
5.列表追加数据
>>> l3.append('byj')
>>> print(l3)
[1.1, 'abc', True, ['brl', 'xm'], 'byj']
>>> l3[3].append('haha')
>>> print(l3)
[0, 'abc', True, ['brl', 'xm', 'haha'], 'byj']
>>> l3[0]=0
>>> print(l3)
[0, 'abc', True, ['brl', 'xm'], 'byj']
6.列表移除数据
>>> print(l3)
[0, 'abc', True, ['brl', 'xm', 'haha'], 'byj']
>>> l3.remove(True)
>>> print(l3)
[0, 'abc', ['brl', 'xm', 'haha'], 'byj']
>>> l3[2].remove('haha')
>>> print(l3)
[0, 'abc', ['brl', 'xm'], 'byj']
字典
字典是由键-值对构成的数据类型
通过键取值
1.创建一个字典
>>> t={"name":"brl","age":22,"gender":"boy"}
>>> print(t)
{'name': 'brl', 'age': 22, 'gender': 'boy'}
>>> type(t)
<class 'dict'>
2.字典取值
>>> t["name"]
'brl'
>>> t["age"]
22
3.修改字典中的值
>>> t["age"]=18
>>> t["age"]
18
4.创建一个空字典
>>> a={}
>>> type(a)
<class 'dict'>
>>> b=dict()
>>> type(b)
<class 'dict'>
5.in && not in的使用
>>> print(t)
{'name': 'brl', 'age': 18, 'gender': 'boy'}
>>> "brl" in t
False
>>> "name" in t
True
总结(数据类型比较)
- 按存储模型分类
- 标量类型:数值、字符串
- 容器类型:列表、元组、字典
- 按更新模型分类
- 可变类型:列表、字典
- 不可变类型:数字、字符串、元组
- 按访问模型分类
- 直接访问:数字
- 顺序访问:字符串、列表、元组
- 映射访问:字典