Python全栈(第一部分)day1

时间:2023-03-08 21:31:47

计算机基础

  1. cpu:相当于人的大脑,用于计算。

  2. 内存:储存数据,4G,8G,16G,32G,成本高,断电即消失。

  3. 硬盘:1T,固态硬盘,机械硬盘,储存数据,应该长久保持数据,重要文件,小电影等等。

  4. 操作系统:

  5. 应用程序。

python历史

宏观上:python2 与 python3 区别:

python2 源码不标准,混乱,重复代码太多,

python3 统一 标准,去除重复代码。

python的环境

  1. 编译型:一次性将所有程序编译成二进制文件。

    缺点:开发效率低,不能跨平台。

    优点:运行速度快。

    C,C++等等。

  2. 解释型:当程序执行时,一行一行的解释。

    优点:开发效率高,可以跨平台。

    缺点:运行速度慢。

    python,php,等等。

python的发展

python种类

  1. cpython: c语言识别的字节码

  2. jypython: java能识别的字节码

  3. ironpython

  4. 其他语言python

  5. pypy: 一次性全部编译成字节码,运行速度快,开发效率相对慢

运行第一个py文件:

python3x : python 文件路径 回车

python2x : python2 文件路径 回车

python2与python3 区别:

  • python2默认编码方式是ascii码

    解决方式:在文件的首行添加:#-*- encoding:utf-8 -*-
  • python3 默认编码方式utf-8

变量

变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用。

  1. 必须由数字,字母,下划线任意组合,且不能数字开头。

  2. 不能是python中的关键字。

    ['and', 'as', 'assert', 'break', 'class', 'continue',

    'def', 'del', 'elif', 'else', 'except', 'exec',

    'finally', 'for', 'from', 'global', 'if', 'import',

    'in', 'is', 'lambda', 'not', 'or', 'pass', 'print',

    'raise', 'return', 'try', 'while', 'with', 'yield']

  3. 变量具有可描述性。

  4. 不能是中文。

常量

一直不变的量。 \(\pi\)

BIR_OF_CHINA = 1949

注释

方便自己方便他人理解代码。

  • 单行注释:#
  • 多行注释:'''被注释内容''' """被注释内容"""

用户交互,input

  1. 等待输入

  2. 将你输入的内容赋值给了前面变量。

  3. input出来的数据类型全部是str

基础数据类型初始

  1. 数字:int 12,3,45

    + - * /

    ** 幂运算

    % 取余数

    ps: type()判断数据类型

    字符串转化成数字:int(str) 条件:str必须是数字组成的。

    数字转化成字符串:str(int)

  2. 字符串:str,python当中凡是用引号引起来的都是字符串,字符串换行需用''' '''

    可相加: 字符串的拼接。

    可与数字相乘: str * int

  3. bool: 布尔值。 True False。

if语句

  1. 单选

     if 条件:
    结果
  2. 对比

     '''
    if 条件:
    结果
    else:
    结果
    ''' if 条件:
    结果
    结果
  3. 多选

     if 条件:
    结果
    elif 条件:
    结果
    else:
    结果
  4. 嵌套

     name = input('请输入你的名字:')
    age = input('请输入你的年龄:')
    if name == 'xkzhai' :
    if age == '18':
    print(666)
    else:
    print(333)
    else:
    print('错了')

while语句

    while 条件:
循环体
#打印1~100
count = 1
while count<=100:
print(count)
count = count+1 #1~100求和
count = 1
sum = 0
while count<=100:
sum = sum + count
count = count + 1
print('1+2+...+100='+str(sum)) #break
print('111')
while True:
print(222)
print(333)
break
print(444)#不打印
print('abc') #break打印1~100
count = 1
while True:
print(count)
count = count + 1
if(count>100):
break #continue
print(111)
count = 1
while count<20:
print(count)
continue #无限输出1
count = count + 1 count = 0
while count <= 100 :
count += 1
if count > 5 and count < 95:
continue #6~94不输出
print("loop ", count)
print("-----out of while loop ------")

条件成立则无限循环。

终止循环的三种方式:

  • 改变条件,使其不成立。
  • break
  • continue

作业

  1. while循环输入1,2,3,4,5,6,8,9,10

  2. 输出1-100内所有奇数

  3. 输出1-100内所有偶数

  4. 求1-2+3-4+...+99的值

  5. 用户登陆(三次机会尝试)