Python#

 


# 2. python
- python核心语法:
    数据结构: 列表, 元组, 字典, 集合
    - 项目: 用户管理系统, ATM机, 学生管理, 停车场管理
    面向对象:(2day)
    - 2048游戏(pygame)

- 数据库编程(2-3day)
    - ORM
    - 银行转账的数据操作

- 正则表达式
    - *, ?, [[:upper:]], [[:lower:]].....(通配符)
    -
    - 爬虫()
    - 爬取猫眼电影排行榜前100, 并且存储到数据库中;
    - 爬取银行名及对应的官网链接;
- 多线程, 多进程以及协程
- socket编程(ssh雏形)
- excel, 群发邮件.......

- flask, django, tornado
- Flask: (7day)---微电影管理系统
- Django:(5day)---个人博客管理系统

- 数据分析
    - numpy, pandas, matplotlib

opencv----(人脸识别)
itchat---


#1. 可跨平台移植

Windows   --- Linux(wine)   ------ ios
(QQ)    ------(QQ)    ---- C/C++
---(Java, python)


#2.
编译型:C/C++
解释型语言:shell, python

# 安装平台
- Linux
    - 去官网下载源码安装包(python3.6);
    - 解压安装包到/opt目录;
    - 安装编译过程中需要的依赖包:gcc, zlib, zlib-devel, openssl-devel
    - 进入解压的安装包进行编译;
    ```
    cd /opt/Python3-*/
    # --prefix安装路径, --with-ssl: 添加ssl加密
    ./configure --prefix=/usr/local/python --with-ssl
    ```

    - 安装:make && make install
    - 添加python3的命令到环境变量里面
    ```
    echo $PATH
    # 临时添加
    export PATH="python3命令所在的路径:$PATH"
    # 永久添加
    echo export PATH="python3命令所在的路径:$PATH" >> ~/.bashrc
    # 重新读取配置文件:
    source ~/.bashrc
    ```

***如何检测是否安装成功?
    pyhton3

    

- Windows
- Mac


# python代码编写


- 交互环境:
- 文本环境:

# python-打印

- python2:
    print "打印内容"
    print("content")

- python3:
    print('hello')

- ****python2向python3过渡:
    from __future__ import print_function


# python的编码格式:
    python2: ASCII
    python3: Unicode

ASCII编码:
    1字节=8bit: 一个英文字符占用一个字节,---- 010101010101010(2^8-1)
    96-a
    97-b
Unicode: 一个字符代表两个字节,(2^16-1);
utf-8: 如果是英文, 一个字节存储; 如果是中文, 用三个字节存储;

# 程序: 输入(键盘)------代码(java/python)-------输出(显示屏)


# 输入:

*** python2:
- input:(只接受数值类型)
```
>>> help(input)

>>> input()
1
1
>>> num = input()
1
>>> num
1
>>> num = input("请输入密码:")
请输入密码:1234567
>>> import getpass
>>> num = getpass.getpass("请输入密码:")
请输入密码:
>>> print(num)
12345678
>>> num = input("请输入密码:")
请输入密码:westos123
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'westos123' is not defined

```


- raw_input(接收字符串类型)

```
>>> name = raw_input("请输入用户名:")
请输入用户名:westos

# 如果接收的值要进行数值比较时, 一定要转化为同种类型比较;
>>> age = raw_input("请输入年龄:")
请输入年龄:19
>>> type(age)
<type 'str'>
>>> age >19
True
>>> int(age) >19
False

```

*** python3
- input: 接收的为字符串数据类型, 没有raw_input
```
>>> num = input()
12
>>> name = input()
westos
>>> type(num)
<class 'str'>
>>> type(name )    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'nam' is not defined
>>> type(name)
<class 'str'>
```


# 输出:

```
# %s:代表字符串, %d: 整形, %f: 浮点型
>>> print("%s的年龄为%s" %(name, age))
westos的年龄为19


# .2f: 保留小数点后两位
>>> money = 7800.7812345660
>>> print("%s本月的公资为%f" %(name, money))
westos本月的公资为7800.781235
>>> print("%s本月的工资为%.2f" %(name, money))
westos本月的工资为7800.78

#.3d: 整形总占位数, 不够的前面补0
>>> sid = 1
>>> print("%s的学号为130%d" %(name, sid))
westos的学号为1301
>>> print("%s的学号为130%.3d" %(name, sid))
westos的学号为130001
>>> sid = 10
>>> print("%s的学号为130%.3d" %(name, sid))
westos的学号为130010

```

# 数值类型

1(整形)
```
>>> aInt = 13
>>> print(aInt)
13
>>> print(type(aInt))
<type 'int'>

```

17438759847509836949587787(长整形)

```

** python2: 有长整形
>>> aLong = 125653274468735986958609585
>>> print(type(aLong))
<type 'long'>
>>> bLong = 1L
>>> print(type(bLong))
<type 'long'>


**python3中: 没有长整形


>>> aInt=1
>>> type(aInt)
<class 'int'>
>>> aLong = 8727398274987398555567985798567777777777985769843
>>> type(aLong)
<class 'int'>
>>> bLong = 1L    
  File "<stdin>", line 1
    bLong = 1L
             ^
SyntaxError: invalid syntax

```


1.23455, 12000000 , 12e6, 1.2e+7, 0.12e+8, 0.012, 1.2e-2(浮点型)

```
>>> aFloat = 1.2
>>> type(aFloat)
<type 'float'>
>>> aFloat = 12e10
>>> aFloat
120000000000.0
>>> type(aFloat)
<type 'float'>
>>> aFloat=12e-10
>>> aFloat
1.2e-09
>>> type(aFloat)
<type 'float'>

```

2i+1(复数类型)====x**2>0, x**2=-1

```
>>> aComplex = 2j+3
>>> type(aComplex)
<type 'complex'>


***查看帮助: 可以使用什么方法, 实现什么功能?
>>> help(aComplex)

>>> dir(aComplex)
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 'conjugate', 'imag', 'real']


***案例:
>>> aComplex.conjugate()
(3-2j)
>>> aComplex.imag
2.0
>>> aComplex.real
3.0
```

# 字符串数据类型

```
>>> aString = "hello"
>>> type(aString)
<type 'str'>

>>> dir(aString)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> help(aString.center)

>>> aString.center(40)
'                 hello                  '
>>> aString.center(40, '*')
'*****************hello******************'
>>> print("学生管理系统".center(50, '-'))
----------------学生管理系统----------------
>>> print("学生管理系统".center(50, '*'))
****************学生管理系统****************

```

# 数据类型的转换:
- 在python中, 所有的数据类型都可以作为内置函数,用来转换数据类型;
```
>>> str(1)
'1'
>>> int(2e-10)
0
>>> complex(2)
(2+0j)
```


# 如何删除内存中的变量?

```
>>> aFloat
1.2e-09
>>> del aFloat
>>> aFloat
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'aFloat' is not defined
```

# 布尔数据类型
bool: 只有两个值(True, False)

```
>>> bool(a)
True
>>> bool(0)
False
>>> bool(67)
True
>>> bool(67.768)
True


>>> name = "westos"
>>> bool(name)
True
>>> name = ""
>>> bool(name)
False

```

# 求平均成绩(python3解释器)
#
#- 输入学生姓名;
#- 依次输入学生的三门科目成绩;
#- 计算该学生的平均成绩, 并打印;
#- 平均成绩保留一位小数点;
#- 计算该学生语文成绩占总成绩的百分之多少?并打印。eg: 78%;
#

name = input("学生姓名:")
chinese = float(input("语文成绩:"))
math = float(input("数学成绩:"))
english = float(input("英语成绩:"))

# 总成绩
sumScore = chinese+math+english
# 求平均成绩
avgScore = sumScore/3
# 求百分比, 0.33*100 = 33.33    ----- 33.33%
chinesePercent = (chinese / sumScore)*100

print("%s 的平均成绩为%.2f" %(name, avgScore))
print("语文成绩占总成绩的%.2f%%" %(chinesePercent))

- 算术运算符:+,-,*,**, /, %, //

```

**** /:
# python2:

>>> 5/2
2
>>> 100/300
0
>>> 5/2.0
2.5
>>> 100/300.0
0.3333333333333333
>>> from __future__ import division
>>> 5/2
2.5
>>> 100/300
0.3333333333333333

# python3:
>>> 5/2
2.5
>>> 100/300
0.3333333333333333


```


# 赋值运算符:=, +=, -=, /=, *=, %=

"""
- if:


if 条件表达式(返回值只能是bool类型):
    满足条件表达式执行的语句


if 条件表达式:
    满足条件表达式执行的语句
else:
    不满足条件表达式执行的语句

if 条件表达式:
    满足条件表达式执行的语句
elif 条件表达式:
    满足条件表达式执行的语句
elif 条件表达式:
    满足条件表达式执行的语句
else:
    不满足条件表达式执行的语句

if间接实现三元运算符:   value1 if 条件 else value2


```
>>> a=14
>>> b=2
>>> max = a>b?a:b
  File "<stdin>", line 1
    max = a>b?a:b
             ^
SyntaxError: invalid syntax
>>> a if a>b else b
14
>>> max = a if a>b else b
```

"""


#1.  判断用户输入是否为空?


#value = input("Value:")
##if value == '':
##    print("请输入合法的值")
#
#
## "hello"
#if  not value:
#    print("请输入合法的值")

# 2. 判断学生等级。
score = 100
if 90<score <=100:
    grade = "A"
elif 80<score<=90:
    grade = "B"
else:
    grade = "C"


print(grade)

# 输入三个数,求一元二次方程ax**2 + bx +c = 0的解;
import math

a = int(input('a:'))
b = int(input('b:'))
c = int(input('c:'))

# 1. 判断是否为一元二次方程, 如果不是, 则退出?
if a==0:
    print("a不能为0;")
    exit()

# 2. 判断delta的值;
delta  =  b**2-4*a*c
if delta < 0:
    print("无解")
elif delta == 0:
    print("一个解")
    x = (-b+math.sqrt(delta))/(2*a)
    print(x)
else:
    print("两个解")

# 判断闰年?
# 用户输入年份year, 判断是否为闰年?
#     - year能被4整除但是不能被100整除 或者 year能被400整除, 那么就是闰年;

# eg: 8: 8%4==0


#name = "westos"
#if name == "root":
#    print("是超级用户")
#else:
#    print("不是超级用户")
#    
    

year = int(input("Year:"))
if (year % 4 == 0 and year % 100 != 0) or ( year % 400 == 0):
    print("%s是闰年" %(year))    
else:
    print("%s不是闰年" %(year))   

猜你喜欢

转载自blog.csdn.net/houzeyu666/article/details/81511379