zoukankan      html  css  js  c++  java
  • Python--语法

    突破从改变开始,一行行字符,熟悉的感觉,还是那个味儿...呀哈哈哈

    一、变量

    变量是计算机语言中能存储计算结果或能表示值的抽象概念,变量可以通过变量名访问、调用及修改。变量通常表示可变状态,即具有存储空间的抽象。变量是一种为方便使用的占位符,用于引用计算机内存地址。使用变量不需要了解变量在计算机内存中的地址,只需要通过变量名引用就可以查看和更改变量的值。

    命名规则

    1、由字母、数字、下划线组成,且不能以数字开头,不能包含特殊字符,字母区分大小写。

    2、不能保留字命名。

    3、定义的变量名应具有一定意义,一般为简短、易于记忆的名称。

    4、变量名在有效范围内必须是唯一的,就是引用的变量可以被程序正确识别。

    声明变量

    1 num1 = 1
    2 name = "Oliver Lee"

    二、注释的用法

    Python中的注释包括单行注释、多行注释、批量注释、中文注释。注释可以起到一个备注的作用,个人编写的代码可以让别人很容易理解代码的用途,对于开发者来说,将注释作为一种开发习惯很有必要。

    1、井号(#)被用作单行注释符号,在代码中使用#时,它右边的任何数据都会被忽略,当做注释信息。

    例如:

    1     if num == 3:
    2         continue    #Jump out of this cycle.

    #号右边的内容在执行时不会被输出。
    2、多行注释使用三引号"""需注释的内容""",可以是三对单引号,也可以是三对双引号。

    例如:

    1 '''
    2 num = 1
    3 while num <= 10:
    4     num+=1
    5     if num == 3:
    6         continue    
    7     else:
    8         print (num)
    9 '''

    3、中文注释

    在Python编写代码的时候,避免不了用到中文,这时候需要在文件开头加上中文注释。如果开头不声明保存编码的格式是什么,将会默认使用ASCII编码保存文件,在执行时就会出错喽,即使这部分中文包含在#注释或三引号注释中也会出错。因此,需要用到中文注释。

    例如:

    1 #_*_coding:utf-8_*_

    三、字符串拼接

    招式1:

    直接用“+”来连接两个字符串:

    1 print("Oliver"+"Lee")

    输出结果:OliverLee

    招式2:

    字符串用逗号隔开,字符串将会被连接,但是,字符串之间会多出一个空格。

    1 print("Oliver","Lee")

    输出结果:Oliver Lee

    招式3:

    将两个字符串直接放在一起,两个字符串自动连接为一个字符串。

    1 print("Oliver""Lee")

    输出结果:OliverLee

    四、运算符

    1、算术运算符

    操作符 描述 举例
    + 将操作符两侧的值相加 a + b = 9
    - 左侧操作数减去右侧操作数 a - b = 2
    * 运算符两侧的值相乘 a * b = 35
    / 两侧操作数相除 a / b = 2.5
    % 模,左侧操作数除以右侧操作数,取余得到结果 a / b = 3
    ** 指数,指数(幂)计算 a ** b = 1024
    // 地板除 9 // 5 = 1 , 9.0 //5.0 = 1.0

    2、比较运算符

    ==  检查两个操作数的值是否相等,是则条件为真。

    !=  检查两个操作数的值是否相等,不相等则条件为真。

    <>  检查两个操作数的值是否相等,不相等则条件为真。类似于!=运算符。

    >  检查左侧操作数是否大于右侧操作数,是则条件成立。

    <  检查左侧操作数是否小于右侧操作数,是则条件成立。

    >=  检查左侧操作数是否大于等于右侧操作数,是则条件成立。

    <=  检查左侧操作数是否小于右侧操作数,是则条件成立。

    3、逻辑运算符

    判断优先级用从左到右短路原则,具体判断方式如下:

    and  与运算符,左右两侧都为真,则条件成立。如果左侧为假,右侧无需判断。

    or  或运算符,左侧为真,则条件成立,否则判断右侧。

    not  非运算符,反转逻辑状态。

    五、While循环

    例1:比较3个数的大小,输出最大的数。

    #####################################################
    # Task name: Comparetive size of three numbers      #
    # Description: Enter the three number,print the     #
    #             maximum number                        #
    #---------------------------------------------------#
    # Author: Oliver Lee                                #
    #####################################################
    
    num1 = input("Please the first number:")
    num2 = input("Please the second number:")
    num3 = input("Please the third number:")
    max_num = 0
    if num1 > num2:
        max_num = num1
    else :
        max_num = num2
    if max_num > num3:
        print("Maximum number is:",max_num)
    else :
        max_num = num3
        print("Maximum number is:",max_num)

    例2:猜年龄

     1 #####################################################
     2 # Task name: Guess age                  #
     3 # Description:                       #
     4 #---------------------------------------------------#
     5 # Author: Oliver Lee                                #
     6 #####################################################
     8 age = 50
     9 flag = True
    10 while flag:
    11     user_age = int(input("Age is:"))
    12     if user_age == age:
    13         print ("Bingo")
    14         flag = False    #break        
    15     elif user_age > age:
    16         print ("Is bigger")
    17     else :
    18         print ("Is Smaller")

    例3:continue的用法,跳出当次循环。

     1 #####################################################
     2 # Task name:                        #
     3 # Description:                       #
     4 #---------------------------------------------------#
     5 # Author: Oliver Lee                                #
     6 #####################################################
     7 num = 1
     8 while num <= 10:
     9     num+=1
    10     if num == 3:
    11         continue    #Jump out of this cycle.
    12     else:
    13         print (num)

    例4:根据用户输入的高和宽,打印出符合条件的矩形

     1 #####################################################
     2 # Task name: Draw a rectangle                       #
     3 # Description: User input height and width,Draw a   #
     4 #             rectangle that meets the requirements.#
     5 #---------------------------------------------------#
     6 # Author: Oliver Lee                                #
     7 #####################################################
     8 height = int(input("height is:"))
     9 width = int(input("width is:"))
    10 num1 = height
    11 while num1 > 0:
    12     num2 = width
    13     while num2 > 0:
    14         print("#",end="")
    15         num2-=1
    16     print()
    17     num1-=1

    例5:

     1 #####################################################
     2 # Task name: Print Triangle                         #
     3 # Description:   ****                               #
     4 #                ***                                #
     5 #                **                                 #
     6 #                *                                  #
     7 #---------------------------------------------------#
     8 # Author: Oliver Lee                                #
     9 #####################################################
    10 num1 = 4
    11 num2 = 4
    12 while num1 > 0:                
    13     while num2 > 0:            
    14         print("*",end="")    
    15         num2-=1                
    16     print()
    17     num1 -=1
    18     num2 =num1

    例6:

     1 #####################################################
     2 # Task name: Print Triangle                         #
     3 # Description:   *                                  #
     4 #                **                                 #    
     5 #                ***                                #
     6 #                ****                               #        
     7 #---------------------------------------------------#
     8 # Author: Oliver Lee                                #
     9 #####################################################
    10 num1 = 1
    11 while num1 <= 4:
    12     num2 = 1
    13     while num2 <= num1:
    14         print("*",end="")
    15         num2+=1
    16     num1+=1
    17     print()

    例7:

     1 #####################################################
     2 # Task name: 9 * 9                                  #
     3 # Description: 1*1=1                                #
     4 #              1*2=2    2*2=2                       #
     5 #              1*3=3    2*3=6    3*3=9              #
     6 #               ......                              #
     7 #---------------------------------------------------#
     8 # Author: Oliver Lee                                #
     9 #####################################################
    10 first = 1
    11 while first <=9:
    12     second = 1
    13     while second <= first:
    14         print(str(second)+"*"+str(first)+"="+str(second*first),end="	")    # column * row
    15         second+=1
    16     print()
    17     first+=1

    ------------------前方高能,注意拦截--------------------------

  • 相关阅读:
    PAT (Advanced Level) 1060. Are They Equal (25)
    PAT (Advanced Level) 1059. Prime Factors (25)
    PAT (Advanced Level) 1058. A+B in Hogwarts (20)
    PAT (Advanced Level) 1057. Stack (30)
    PAT (Advanced Level) 1056. Mice and Rice (25)
    PAT (Advanced Level) 1055. The World's Richest (25)
    PAT (Advanced Level) 1054. The Dominant Color (20)
    PAT (Advanced Level) 1053. Path of Equal Weight (30)
    PAT (Advanced Level) 1052. Linked List Sorting (25)
    PAT (Advanced Level) 1051. Pop Sequence (25)
  • 原文地址:https://www.cnblogs.com/pyramid1001/p/5793716.html
Copyright © 2011-2022 走看看