python基础知识1

python基础一

  1. 注释

    当行注释:#被注释内容

    多行注释:'''被注释内容''',或者"""被注释内容"""

    print('hello,world')#打印
    '''
    打印的内容是hello,world
    '''
    
  2. 变量

    变量是把程序运行中间结果临时的存在内存里,方便后续代码调用

    x=2;
    y=x;
    x=3;
    print(x,y)
    
  • 变量定义规则

    • 变量名只能是字母,数字或下划线任意组合
    • 变量名首个字符不能是数字
    • 变量的定义要具有可描述性
    • 变量名不能以python语句中关键命令命名。例如,break,and,class,等等
  • 推荐定义方式

    #下划线
    age_of_mao=24
    #驼峰体
    AgeOfMao=24
    
  1. 常量
    常量一般指不变的量,但是python中没有不变得量,便约定俗成,全部大写默认其为常量

  2. 基础数据类型

    数据类型就是说明,什么是字符,什么是数字

    #整型,字符型 
    x=3.0#这是浮点型
    print(int(x))#通过int将浮点型变成整型
    name='Kim'#字符型通过引号来赋值给变量
    #多引号
    mag='''
    今天我想你了,
    明天我还想你。
    '''
    print('mag')
    #通过多引号来加载多行字符串
    
    • 字符串拼接
    #利用加号,以及*
    name='alex';
    age=15;
    msg=name+',年龄'+str(age);
    print(msg)
    
    • boole值

    用于逻辑判断的,非真即假。真为True,假为False

    a=1
    b=2
    a==2#不成立就是假输出False
    
  3. 程序交互

    使用的是input

    #类似登记个人信息
    name=input('请输入你的姓名')
    age=input('请输入你的年龄')
    print('我的姓名是:'+name+'\n我的年龄是:'+age)
    
    • 进阶版
    #格式化输出
    name=input('give me your name')
    age=input('give me your age')
    job=input('give me your job')
    info='''
    --------------info of %s----------------
    Name	:%s
    Age		:%s
    Job		:%s
    --------------end-----------------------
    '''%(name,name,age,job)
    print(info)
    

    %s就是代表字符串占位符,除此之外,还可以用%d是数字占位符

流程控制

  1. if

    if语句是判断语句,满足条件就往下走 ,否则不走。就如同走二叉路一样,符合条件走左边,否则走右边,是类似的道理

    • 单与单分支if...else语句

      '''
      if 条件:
      	执行内容
      else:
      	执行内容
      '''
      #上述格式是走的二叉路类型的,非A即B
      #单分支
      age=input('give me your age:')
      if int(age)>17:
          print('you are man')
      #双分支
      if int(age)>=18:
      	print('you are adult')
      else:
      	print('you are children')
      

      其中需要注意缩进,if的下一行一般都是缩进4的空格地位等同于c语言里面的{}

      var age = 56
      if ( age < 50){
        console.log("还能折腾")
          console.log('可以执行多行代码')
      }else{
         console.log('太老了')
      }
      
    • 多分支

      '''
      if 条件:
      	执行任务
      elif 条件:
      	执行任务
      elif 条件:
      	执行任务
      else:
      	执行任务
      '''
      #生活中的门票
      a=input('tell me your age')
      age=int(a)
      if age<=5:
      	print('free')
      elif age<=15:
      	print('half price')
      else :
      	print('full price')
      
  2. while

    循环语句就是常见的,每天吃饭,睡觉之类的,一直重复

    • 基本循环

      '''
      while 条件:
      	循环语句
      '''
      while True:
          a=input('if you want to break:please input 1')
          if int(a)==1:
              break
          else:
              print('go')
      
    • 如何终止循环

      1. 改变条件
      2. 关键字break
      3. 调用系统命令quit(),exit(),后面会介绍
      4. 关键字continue 终止本次循环
      #改变条件
      count=1
      flag=True
      while flag:
      	print(count)
      	count+=1
      	if count>=10:
      		flag=False
      #break
      count=1
      flag=True
      while flag:
      	print(count)
      	count+=1
      	if count>=10:
      		break
      #continue
      count=0
      while count<=101:
      	count+=1
      	if count%2==0:
      		print(count)
      	else:
      		continue;
      
    • while... else...

      如果while没有被中间被打断,那么会执行else

      count=0
      while count<=5:
          count+=1;
          print(count)
      else:
          print('all count are input')
      #被continue打断,仍会输出else的内容
      count=0
      while count<=5:
          count+=1;
          if count==2:
              continue;
          print(count)
      else:
          print('all')
      #被break打断
      count=0
      while count<=5:
          count+=1;
          if count==2:
              break;
          print(count)
      else:
          print('all')
      
posted @ 2021-06-18 15:13  墨墨无闻  阅读(38)  评论(0)    收藏  举报