初探python

一、pip使用

安装pip nose模块:py -3 -m pip install nose

升级pip工具的命令:py -3 -m pip install --upgrade pip
升级pip本身:py -3 -m pip install --upgrade pip
卸载pipnose模块:py -3 -m pip uninstall nose
显示安装的包名和版本号:python -m pip  list

二、使用工具

建议使用前两种:

1、命令行

2、记事本

 

3、Ide

 

三、ASCLL码

小写字母a-z   97-122
大写字母A-Z   65-90
数字0-9     48-59

 

chr 和 ord使用

Chr
>>> chr(97)
'a'
>>> chr(122)
'z'
>>> chr(65)
'A'
>>> chr(90)
'Z'
>>> chr(48)
'0'
>>> chr(57)
'9' 
ord
>>> ord('a')
97
>>> ord('a')
97
>>> ord('A')
65
>>> ord('0')
48

 

四、range的使用

>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(1,10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1,10,2))
[1, 3, 5, 7, 9]
>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(1,9,2))
[1, 3, 5, 7]
>>> list(range(1,10,2))
[1, 3, 5, 7, 9]
>>> type(range(10))
<class 'range'>

五、字符之间的转换bytes 和 str之间的转换

python3中字符串类型:

1 str类型 ----Unicode编码

2 bytes ---gbk,utf8    Unicode编码转成其他编码类型

 

str --->bytes  通过encode转化

bytes ---->str  通过decode转化

 

文本文件:

保存动作:白内存中的文件保存到硬盘 这个过程就是 Unicode 到 bytes

打开文件:硬盘中的文件调用内存;这个过程就是 bytes 到 Unicode

 

>>> "hello wrold".encode("utf-8")		-----str转换bytes
b'hello wrold'
>>> "hello wrold".encode("utf-8").decode("utf-8")	----bytes转换str
'hello wrold'

六for循环

>>> for i in range(10):
...     print(i)
...
0
1
2
3
4
5
6
7
8
9
 
>>> for i in range(11):
...     if i%2==0:
...         print(i)
...
0
2
4
6
8
10

 

习题1:

输出所有的大写字母,小写字母,大小写字母和数字;

使用for range() chr()

  1. 算法:

1)循环遍历所有的大写字母:65-90

依次把循环遍历到的字母拼接到结果字符串中;

打印输入结果字符串

  1. 代码

算法-->代码

1 输出全部大写字母

uppers = ""
for i in range(65,91):
    uppers = uppers  + chr(i)

print(uppers)

2 输出全部小写字母

lowers = ""
for i in range(97,123):
    lowers = lowers + chr(i)#lowers += chr(i)

print(lowers)


3 输出0-9数组

digits = ""
for i in range(48,58):
   digits = digits + chr(i)

print(uppers+lowers+digits)

 

4 通过一个循环输出大小写字母

biga=65
minb=97
result=""
result1=""
for i in range(26):
    result=result+chr(biga)
    result1=result1+chr(minb)
    biga+=1
    minb+=1
 
print(result,result1)

 

 

七、变量

变量:字母

 

变量不能使用python自带的函数去命名

变量是指:b指向5存储的位置

 

>>> a = 3
>>> b = 3
>>> id(a)
140706776405088
>>> id(b)
140706776405088

 

Python开辟了一个空间-5~256不同变量指向数组的ID相同,超出这个部分不一样

>>> a = 300
>>> b = 300
>>> id(a)
1223505065456
>>> id(b)
1223504644112

Python中的变量不需要提前声明:

比如

java : int a
Python: a=10

 

特殊写法

1 同时用a,b进行变量

>>> a = 20
>>> b = 30
>>> a,b = 20,30
>>> a
20
>>> b
30

 

2 在同一行变量a,b

>>> a=10;b=20
>>> a
10
>>> b
20

 

习题2:

交换 a =10,b=50两个变量的值

 

>>> a,b = 10,11
>>> a
10
>>> a,b=b,a
>>> a
11

借助中间变量

>>> a,b = 10,11
>>> tamp = a
>>> a = b
>>> b=tamp
>>> a
11
>>> b
10

八、类型:

number数字:int float complex

字符串:str bytes

列表 list

元组 tuple

字典 dict

集合 set

布尔类型:true=1  false=0

非空类型都是True

所有空值,0都是False

>>> if []:
...     print(1)
...
>>>