关注我的个人博客:www.yaoxinlei.com

姚鑫磊的博客园

翻过一座山,山后一片海。

python基础一

Python基础一

day-1

1.变量,常量,基础数据类型初始,流程控制语句等;

day-1Python的分类:

编译型:将代码一次性全部编译成字节码,再执行

代表语言:C语言

优点:执行速度快;

缺点:不能跨平台,开发速度慢,不好调试

解释型:代码从上至下逐行解释并执行

代表语言:Python

优点:便于调试,可以跨平台,开发效率高

缺点:执行速度相对慢

x = (1*2*3*4*5)
y = 5 * 20 / 8
print(x,y)
x与y 为变量;
day-1-1变量:
  • 变量只能由数字,字母开头,下划线任意组合;

  • 不能以数字开头;

  • 不能是python中的关键字;

  • 变量要有描述性;

name = 'alex'
age = 'yaoxinlei'
  • 变量不能使用中文;

  • 变量不能使用拼音;

  • 变量不能过长;

Python:从上至下的顺序执行

age1 = 12
age2 = age1
age3 = age2
age2 =26
print(age1,age2,age3)
day-1-2常量:
  • 常量一直不变的量. Python中本来没有的常量;

    name = 'alex'
    NAME = 'zhangsan'
  • python中的常量:将变量全部大写;

  • 往往常量会放在文件最上面

  • 注释 :对代码解释说明,帮助自己或者别人理解;

单行注释: #
多行注释: '''被注释的内容'''或者"""被注释的内容"""
day-1-3数据类型的初始:
  • 什么是数字类型?

    人类给机器划分的一些数据:数字类型,字符串类型,bool类型(Ture与False)

数字类型:

float:浮点型 3.14;

int :整数型 i = 10 i2= 100;

str :字符串类型 Python中凡是用引号引起来的就是字符串;

s1 = 'alex'

s2 = 'alex小白'

单双引号配合使用:

s3 = "I'm 小白"

print(s3)

msg = """窗前明月光,疑是地上霜"""

print(msg)

字符串 + 字符串 可以与字符串相加 字符串的拼接

s1 = 'alex'

s2 = '你好'

字符串 可以与数字相乘

s1 = '你好'

print(s1 * 5)

bool 类型:Ture 与False

print(Ture)

print(False)

day-1-4用户输入:
  • input:输入

    name = input('请输入用户名:')
    age = input('请输入年龄:')
    print(name,age)
  • type:判断数据类型

    name = input('请输入用户名:')
    age = input('请输入年龄:')
    print(type(name))
    print(type(age))
  • 字符串拼接

    name = input('请输入用户名:')
    age = input('请输入年龄:')
    print('此用户的姓名是'+name,'此用户的年龄是'+age)
    day-1-4if语句:
  • if 条件语句

    初识结构:
    if 条件
      执行结果(代码块)
    第一种结构:单 if
    print(111)
    if 3 > 2 :
      print(222)
    第二种结构:if else
    chione = int(input('请输入你猜的大小:'))
    if 0 < chione < 4 :
      print('你猜小了')
    else :
      print('你猜大了')
    第三种结构: if elif elif(多选结构)
    chione = int(input('请输入你猜的数字:'))
    if chione == 3 :
      print('你可真棒')
    elif chione == 4 :
      print('一点也不棒')
    elif chione == 6 :
      print('太棒了666')
    第四种结构:if elif elif ...... else
    chione = int(input('请输入你猜的数字:'))
    if chione == 3 :
      print('你可真棒')
    elif chione == 4 :
      print('一点也不棒')
    elif chione == 6 :
      print('太棒了666')
    else :
      print('没有猜对哦')
    第五种结构:if 嵌套
    username = input('请输入用户名:')
    password = input('请输入用户密码:')
    if username == 'alex':
      if password == '123':
          print('用户登陆成功')
      else:
          print('密码输入有误')
    else:
      print('用户名输入有误')

day-2

day-2whlie循环:
  • while 循环

    while 无限循环
    while 循环的结构:
    while 条件:
    循环体
    举例:
    flag = True
    while flag:
      print('两只老虎')
      print('你说')
      flag = False
      print('你走')
      print('拜拜')
    练习:
    方法一:
    msg = True
    conut = 1
    while msg:
      print(conut)
      conut = conut + 1
      conut <= 101;
      if conut == 101:
          msg = False
      方法二:
    count = 1
    while count < 101:
          print(count)
          count = count + 1
  • 如何终止while循环

    break:循环中只要遇到break,立马结束循环;
    举例:
    while True:
      print(111)
      print(222)
      print(333)
      break
      print(444)
      print(555)
    print(123)
    利用break,while 计算1 +2 +3 +4...+100的结果
    count = 1
    sum = 0
    while True:
          sum = sum + count
          count = count + 1
          if count == 101:
              break
    print(sum)

    continue:结束本次循环,继续下一次循环
    while True:
          print(111)
          print(222)
          continue
          print(333)

    while else结构:如果while 循环被break打断,则不执行else代码;
    count = 1
    while count < 5:
          print(count)
          count = count +1
          if count == 3 :break
    else:
          print(666)
    print(222)
    while应用场景:
    验证用户名与密码,重新输入这个功能需要while循环
    无限次的显示页面,无限次的输入
day-2-1格式化输出:
第一种方式:
name = input('请输入你的姓名:')
age = int(input('请输入你的年龄:'))
sex = input('请输入你的性别:')
# % 占位符 s 数据类型为字符串
msg = '你的名字%s,你的年龄%d,你的性别%s' % (name,age,sex)
print(msg)
第二种方式:字典
name = input('请输入你的姓名:')
age = int(input('请输入你的年龄:'))
sex = input('请输入你的性别:')
# % 占位符 s 数据类型为字符串
msg = '你的名字%(name)s,你的年龄%(age)d,你的性别%(sex)s' % {'name':name,'age':age,'sex':sex}
print(msg)
  • bug点

    msg = '我叫%s,今年%s,我的学习进度1%%' % ('姚鑫磊',24,)
    print(msg)
day-2-2运算符:
  • 计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算,今天我们暂只学习算数运算、比较运算、逻辑运算、赋值运算、成员运算

  • 算数运算

    以下假设变量:a=10,b=20

img

  • 比较运算

    以下假设变量:a=10,b=20

img

  • 赋值运算

    以下假设变量:a=10,b=20

img

  • 逻辑运算

    img

  • 优先级:()--->not--->and---or

  • 针对逻辑运算的进一步研究:

      1,在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为( )>not>and>or,同一优先级从左往右计算。

    例题:

    判断下列逻辑语句的True,False。

1,3>4 or 4<3 and 1==1
2,1 < 2 and 3 < 4 or 1>2
3,2 > 1 and 3 < 4 or 4 > 5 and 2 < 1
4,1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8
5,1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
6,not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

2 , x or y , x为真,值就是x,x为假,值是y;

x and y, x为真,值是y,x为假,值是x。

img

posted @ 2021-08-05 23:04  姚鑫磊  阅读(55)  评论(0)    收藏  举报
区顶部