zoukankan      html  css  js  c++  java
  • python笔记---day1

    变量

    变量名只能是字母,数字或下横线的任意组合。

    变量名的第一个字符不能是数字。

    定义方式:官方推荐使用下横线体。例如:age_of_oldboy=56

    常量

    常量就是指不变的量,或者程序的运行不会改变的量。在python中没有一个专门的语法代表常量。

    通常程序员都约定常量变量名全部都是大写。

    程序的交互

    读取用户的输入:使用input函数接受用户的输入。

    例子:

    name=input("你叫什么名字? ")
    age=input("你今年多少岁啦? ")
    hometown=input("你是哪里的人?  ")
    print( "你好",name,",""你的年龄是:",age,"岁,你的故乡是:",hometown )

    备注:print的时候要用逗号分开每个字符。

    代码的注释

    使用#号单行注释,多行注释用一堆的3个单引号或者是双引号

    例子:

    """
    这里的内容都是会注释掉的
    """
    
    '''
    这里的内容也是会注释掉的
    '''

    字符串 

    python中最常用的就是字符串,我们可以用单引号或者双引号(‘和“”)创建

    例:

    >>> s1='zhang'
    >>> s2='yong'
    >>> s1+s2
    'zhangyong'

    访问字符串中的值

    >>> print(s1[0:3])
    zha

    字符串还支持以下运算符

    +  字符串拼接    * 字符串重复输出   

     [ : ] 截取字符串中的一部分

    in   成员运算符 - 如果字符串中包含给定的字符返回 True

    not in 成员运算符 - 如果字符串中不包含给定的字符返回 True

     格式化输出
     先把格式准备好,但是里面的一些信息是要用户输入的,你没办法知道。因此你可以先放一个位置符,在把字符串里的占位符与外部的变量做一个映射关系就可以了。

    方法一:

     1 name=input("请输入你的名字:")
     2 age=input("请输入你的年龄:")
     3 job=input("请输入你的工作:")
     4 
     5 info="""
     6     - - - - - info of %s - - - - - - 
     7       姓名: %s
     8       年龄: %s
     9       工作: %s    
    10     - - - - - -  END - - - - - - - - 
    11 """%(name,name,age,job)
    12 
    13 print(info)

    #如果只有一个变量的话,可以不用括号,直接 %name 就可以了。

    %s  代表是字符串
    %d 代表整数
    %f  代表浮点数

    方法二:

     1 name=input("你叫什么名字: ")
     2 age=input("你今年多少岁: ")
     3 job=input("你的工作是什么:")
     4 hometown=input("你是哪里的人:")
     5 
     6 info="""
     7  -------info of {_NAME} -------
     8 姓名: {_NAME}
     9 年龄:{_AGE}
    10 工作: {_JOB}
    11 籍贯: {_HOME}
    12 
    13  ---------- END ---------------
    14 """.format(
    15         _NAME=name,
    16         _AGE=age,
    17         _JOB=job,
    18         _HOME=hometown
    19 )
    20 print(info)

     流程控制语句

    单分支

    if 条件:

      满足条件后要执行的代码

    双分支

     if 条件:

      满足条件后要执行的代码

    else:

      if条件不满足,走这段代码

    例子:

    _username= 'zhangyong'
    _password = '123456'
    
    username = input("username: ")
    password = input("password: ")
    
    if username == _username and password == _password:
         print("Welcome user %s login..." %username)
    else:
         print("Error username or passsword")

    多分支

    if 条件:

      满足条件要执行的代码

    elif 条件:

      满足条件后要执行的代码

    elif 条件:

      满足条件要执行的代码

    elif 条件:

      上面的条件不满足走这段

    例子:

    import random
    NUM=random.randint(50,100)
    
    tmp=input("请你输入一个数字:")
    guess=int(tmp)
    
    if guess == NUM:
        print('Good! you guessed it')
    elif guess > 100:
        print('Is to big...Guess the number 50-100')
    elif guess < 50:
        print('Is to small...Guess the number 50-100')
    else:
        print('Guess wrong! the number is %s....Good bey!' %NUM)

    循环

    while 循环

    语法:

        while 条件:

                  执行代码:

       else:

                上面的条件不成立,执行这段

    例子:

    循环打印0-50

    count = 0
    while count <= 50:
            print("loop",count)
            count = count + 1
    else:
            print("loop is stop")
    #打印偶数
    count = 0
    while count <= 100:
       if count %2 == 0:
          print (count)
       count = count + 1

    死循环

    可以用True来表示。首个字母要大写。

    count = 0
    while True:
     count = count + 1
     print("loop",count)

    循环的终止语句

    如果在循环的过程中,因为某些原因,你不想继续循环了,怎么把它终止呢?这里用到了 break 和 continue语句。

    break:跳出整个循环

    continue:跳出本次循环,继续下次的循环。

    count = 0
    while True:
       count = count + 1
       print("loop",count)
       if count > 20:
           break

    FOR循环

    oldboy_age = 56
    count = 3
    for i in range(3):
        count = count - 1
        temp = input("Inter a number of oldboy techer age: ")
        guess_age = int(temp)
        if guess_age == oldboy_age :
            print("Yes! your inter is right")
            break
        elif  guess_age > oldboy_age:
            print("No! the number is too big...")
        else:
            print("No! the number too small....")
    
    
        if count > 0 :
            print("你还有%s 次机会!" %count)
        else:
            print("机会用完了 再见!!!!")
  • 相关阅读:
    JS监听组合按键
    XSS初体验
    debuggap,移动端调试新方式
    简析分页逻辑
    【译】Javascript中的数据类型
    【译】typeof null的前世今生
    一个跨域请求的XSS漏洞再续
    H5页面音频自动播放问题
    一个跨域请求的XSS续
    成功自我管理之压力管理
  • 原文地址:https://www.cnblogs.com/zyos/p/10018805.html
Copyright © 2011-2022 走看看