python入门_老男孩_day1

视频链接:https://www.bilibili.com/video/av28244491

关键词:

  • 计算机基础
  • 什么是python
  • 历史
  • 种类
  • python2 和 python3 的区别
  • 变量
  • 常量
  • 注释
  • 用户交互
  • 基本数据类型初始 int str bool
  • if
  • while
  • 作业

计算机基础

  点击一个app到响应,中间发生了什么?

  粗略来说app.exe存在硬盘上,点击,将应用程序加载到内存,cpu通过操作系统将内存中的app.exe执行。

  python用来写应用程序

什么是python

  python是动态解释的强类型定义语言。

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

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

    优点: 执行速度快。

    例子:C, C++

  解释型: 程序运行时,一行一行地解释。

    缺点:执行速度满。

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

    例子: python, Java

python历史

   1989年圣诞节期间,仁慈的独裁者Guido van Rossum为消磨时间,开始写追求简单、优美、清晰的python。

   1991年,第一个python编译器诞生。

   2004年Django的出现,对于python具有跨时代的意义。犹如xp于windows。同年发行python2.4。

   因各路大神贡献源码,造成python程序冗杂,权衡之后,2008年龟叔推出python3,并决定python2在2020年之后便不再发行,终止版本为python2.7。

python种类

  cpython, jpython, pypy, ironpython, 其他语言python

  语言python,即将python程序编译成其他语言字节码,再编译成二进制码。方便python与其他语言交互。

  pypy,将python程序一次性编译好,然后再执行。类似于编译性语言。

python2 和 python3 区别

  宏观:

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

    python3  源码统一, 标准

  微观:

    python2  默认编码为ascii码,会出现中文乱码现象

         解决: #-*-  encoding:utf-8 -*-

    python3  默认编码为utf-8 

变量

  就是将一些中间运算暂存在内存中,以便后续代码调用

  •   必须由数字,字母,下划线,且不能数字开头
  •   不能是python中的关键字
  •   变量具有可描述性
  •   不能是中文
1 t-t = 2
2 3t_t =23
3 *r = 4
4 _ = 'fdsa'
5 __ = 'hello,world!'
6 %_ = 4
7 qwe-r = 'wor'

常量

  一直不变的量

  BIRTH_OF_CHIINA = 1949

注释

  方便后来的自己和其他人理解代码

  单行注释:  #

  多行注释:  ’‘’ ‘’‘’   “”“ ”“”

用户交互

  input

  •   等待输入
  •   将输入的东西存到变量中
  •   得到变量的类型为str

  查看变量类型  type(变量)

三种最基本的数据类型

  整型,字符串, 布尔值

  整型:

    int  12, 3

      + - * / 

      幂 **  取余 %

  字符串:

    stu  引号中引起来的

      可相加  +

      可相乘  str*int

  布尔值:

    True  False

if

1 if 判别式:
2     操作
3 elif 判别式:
4     操作
5 else:
6     操作

while

 1 # 输出1-100
 2 # 方法1    不满足条件跳出
 3 count = 1
 4 while count <= 100:
 5     print(count)
 6     count = count + 1
 7 
 8 # 方法2    break跳出
 9 count = 1
10 while True:
11     print(count)
12     count = count + 1
13     if count > 100:
14         break
 1 # continue
 2 # 试想程序运行结果,比较与break的不同
 3 
 4 count = 1
 5 
 6 while True:
 7     print(count)
 8     break
 9     count = count + 1
10 
11 print('BELOW IS THE RESULT OF CONTINUE...')    
12 
13 while True:
14     print(count)
15     continue
16     count = count + 1

习题

1 # 使用while循环输入 1 2 3 4 5 6    8 9 10
2 count = 1
3 
4 while count <= 10:
5     if count != 7:
6         print(count)
7     else:
8          print(' ')
9     count = count + 1
 1 # 求和1+2+...+100
 2 sum = 0
 3 
 4 # 方法一:for循环
 5 for i in range(101):
 6     sum = sum + i
 7 print(sum)
 8 
 9 # 方法二:while循环
10 count = 1
11 he = 0
12 
13 while count <= 100:
14     he = he + count
15     count = count + 1
16 print(he)
# 输出1-100的所有奇数

for i in range(1, 101):
    if i%2 == 1:
        print(i)
# 输出1-100的所有偶数
count = 1

while count <= 100:
    if count%2 == 0:
        print(count)
    count = count + 1
# 求1-2+3-4+5...99的和   50

for i in range(1, 4):
    if i%2 == 1:
        sum = sum + i
    else:
        sum = sum - i

print(sum)
# 用户登陆,三次机会重试

i = 0
while i < 3:
    usrname = input('请输入账号: ')
    password = input('请输入密码: ')

    if usrname == 'larry' and password == 827:
        print('登陆成功’)
    else:
        print('登陆失败,请重新登陆')
    i +=1
View Code

猜你喜欢

转载自www.cnblogs.com/dignity/p/9721327.html