zoukankan      html  css  js  c++  java
  • 1、python基本语法

    1、python基本语法

    1.1、 pycharm中字符集,单行,多行注释

    #-*- codeing = utf-8 -*-
    #@Time : 2020/6/6 15:17
    #@Author : zhangfudong
    #@FILE :demo1.py
    #@Software : PyCharm
    
    ## 多行注释
    '''
    this is the first zhushi
    this is the second zhushi
    this is the third zhushi
    '''
    

    1.2、格式化输出控制

    print("python")
    print("标准化输出")
    print("hello world")
    
    ## 变量赋值输出
    a = 2
    print("this is a var: ",a)
    
    版本库已定义的变量,需要在python环境下才能执行
    import keyword
    keyword.kwlist
    
    
    ## 格式化输出
    age=18
    print("我今年 %d 岁" %age)
    print("我的名字是%s,我的国籍是%s"%("小张","中国"))
    
    ## 分隔符,默认以空格为分隔符,sep指定分隔符
    print("aaa","bbb","ccc")
    print("www","baidu","com",sep=".")
    
    ## end 换行控制
    print("hello",end="")    ##换行符为空,表示不换行
    print("world",end="	")  ##换行符为	,表示退格,tab
    print("python",end="
    ") ##换行符为
    ,表示换行
    print("default")         ##省略end,表示换行
    print("end")             ##省略,表示默认换行
    
    
    ## 将输入的信息打印显示出来
    password = input("请输入密码:")
    print("您输入的密码是:",password)
    
    
    ##输出字符类型
    a=10;b="zhangafudong";c=0.123
    print(type(a),type(b),type(c))      ## 显示int类型,str类型,fload类型
    
    a=input("输入:")  ## 默认输入的都是str类型,如123
    print("请输入一个数字:%s"%a)
    c=int(input("输入c:"))
    print("请输入一个数字:%d"%c)
    print("输出:"%c)   ## 报错是因为默认输出是str类型
    

    1.3、if 条件句及库的引入

    ## if条件句,默认字符True,False;注意条件之后有冒号“:”,注意格式有缩进
    ## 一般缩进前都会有冒号":",表示代码段的开始
    if 0:
        print("true")
    elif 2:
        print(False)
    else:
        print("else")
    print("end")
    
    ## if条件句实践
    score = int(input("请输入您的score,查看等级:"))
    if score >= 90 and score <= 100:
        print("您的分数:%s,属于 A 等" %score)
    elif score >= 80 and score < 90:
        print("您的分数:%s,属于 B 等" %score)
    elif score >= 70 and score < 80:
        print("您的分数:%s,属于 C 等" %score)
    elif score >= 60 and score < 70:
        print("您的分数:%s,属于 D 等" %score)
    else :
        print("您的分数:%s,属于 E 等" %score)
        
        ### python 引入库
    import 或者 from...import来导入响应的模块
    将整个模块导入:import somemodule
    从某个模块中导入某个函数:from somemodule import somefunc
    从某个模块中导入多个函数:from somemodule import firstfunc,secondfun,...
    将某个模块中的全部函数导入:from somemodule import *
    
    
    ## 生成随机数
    import random       ## 引入随机数库
    x = random.randint(0,2) ## 引入随机整数
    print(x)        ## 输入0,1,2中的随机一个
    
    ## if语句实现剪刀石头布的小游戏
    x=int(input("请输入:剪刀(0)、石头(1)、布(2):"))
    print("你的输入为:",x)
    y=random.randint(0,2)
    print("随机生成数字为:",y)
    if x == y :
        print("平局,再来一局")
    elif (x==0 and y==1) or (x == 1 and y == 2) or (x == 2 and y == 0):
        print("哈哈,你输了,再来一局")
    else :
        print("恭喜,你赢了")
    

    1.4、for 循环介绍

    # for 循环介绍,注意循环语句前的冒号":"
    ## range()函数:range(5)表示0-4;range(1,10,3)表示1-9,步长为3,如1,4,7;不包含最后一个值
    for i in range(1,10,3):
        print("this is num:",i)
    
    ## 字符串输出
    name = "shanghai,guangzhou"
    print(name)
    for  i in name:
        print(i,end="	")  ## 注意:循环输出字符串时,会按单个字符输出,区别于print(name)
    
    
    ## list列表输出,list相当于数组,按照下标访问;len()函数取list列表的长度
    name=["aa","bb","cc","dd"]
    for i in range(len(name)):
        print(i,name[i])
    

    1.5、while循环介绍及乘法表打印

    # while 循环
    i=0
    while i<5:
        print("这是第 %d 次循环:"%(i+1),end="	")
        print("i =",i,";")
        i+=1
    
    ## while中特有的用法:while+else
    count=10
    while count<5: ## 条件满足时执行
        print(count,"小于5")
        count+=1
    else: ## 当while条件不满足时继续执行else部分
        print(count,"大于或等于5")
    
    
    ## 1+2+3+...+100求和
    ### for循环实现
    sum=0;
    for i in range(1,101):
        sum+=i
    print("1+2+3+...+100 =",sum)
    
    ### while循环实现
    sum=0;i=0;n=100
    while i<=n:
        sum+=i
        i+=1
    print("1+2+3+...+%d = %d" %(n,sum))
    
    ## 打印九九乘法表
    ### for 循环实现
    for i in range(1,10):
        for j in range(1,i+1):
            print("%d*%d=%d"%(i,j,i*j),end="	")
            j+=1
        i+=1
        print("")
    
    ### while循环实现
    i=1
    while i<=9:
        j=1
        while j<=i:
            print("%d*%d=%d"%(i,j,i*j),end="	")
            j+=1
        print("")
        i += 1
    
    

    1.6、循环中的continue、break、pass的应用

    # python中的continue、break、pass
    break 语句跳出for和while语句
    continue 跳过当前循环,直接进入下一轮循环继续执行
    pass pass是空语句,python中特有的,一般用作占位语句,不做任何事情
    
    ### while中pass的使用
    for letter in 'ROOM':
        if letter == 'O':
            pass
            print('pass')
        print(letter)
    
    ### while中continue和break的使用
    i=0
    while i<10:
        i=i+1
        print("-"*30)
        if i==5:
            #break     ## 结束整个while循环
            continue   ##结束本次循环,继续下次while循环
        print(i)
    
  • 相关阅读:
    hao947 : Mybatis resultMap配置插入和主键自增返回 : 好947
    VelocityTracker简单介绍
    Java中StringBuilder的清空方法比較
    jquery中的动画
    数据库索引的作用和长处缺点
    很具体GC学习笔记
    深入理解 JBoss 7/WildFly Standalone 模式启动过程
    curl命令具体解释
    【免费】iPhone上最好用的短信群发软件: 高速短信4.1
    Project interpreter not specified(eclipse+pydev)
  • 原文地址:https://www.cnblogs.com/moox/p/13199430.html
Copyright © 2011-2022 走看看