zoukankan      html  css  js  c++  java
  • python基础一

    python历史

      python2,python3区别(大环境下)

    python2:

      1、源代码都含有php,java,c等语言的规范陋习

      2、重复代码特别多

    python3:
      源代码很规范,清晰,简单,符合python的宗旨

    python的划分

      解释型:当程序运行时,将代码从上至下,一句一句解释成二进制,再执行。

          典型:python,php

          优点:开发速度快,可以跨平台

          缺点:执行效率慢

      编译型:将源代码一次性转换成二进制文件,然后再执行

          典型:c,c++

          优点:执行效率快

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

    python版本

      python3:英文,中文没有问题,默认编码:utf-8

      python2:英文没有问题,中文报错,默认编码:ascli;

          显示中文:首行:#-*-encoding:utf-8-*-

    变量

      变量:将运算的中间结果暂存到内存,以便后续程序调用

      变量的规则:  

      1,变量是由数字,字母,下划线,任意组合.

      2,变量不能以数字开头.

      3,变量不能是python的关键字.

      ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if',   'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

      4,变量可描述性.

      fjdlasfjlf = 18

      5,变量不能是中文.

      7,变量不能过长.

    基础数据类型

      数字:int

      字符串:str

      布尔值:bool

      常量:没有常量,但是约定俗称:全部大写的是常量

    作业

      输出1,2,3,...100

    count = 1
    while count < 101:
        print (count)
        count += 1
    
    
    count = 1
    flag = True
    while flag :
        print (count)
        count += 1
        if count == 101:
            flag = False
    View Code

      输出1,2,3,4,5,6,8,9,10

    i = 0
    while i <=10:
        if i == 7:
            i += 1
        print (i)
        i += 1
    
    i = 0
    while i <=10:
        if i == 7:
            i += 1
            continue
        print (i)
        i += 1
    View Code

      输出1+2+3+...+100

    count = 0
    sum = 0
    while count <= 100:
        sum += count
        count += 1
    print (sum)
    View Code

      输出1-100所有的奇数

    i = 0
    while i <=100:
        if i%2 == 1:
            print (i)
        i+=1
    
    i = 1
    while i <= 100:
        print (i)
        i += 2
    View Code

      输出1-2+3-4+...+99

    i = 1
    sum = 0
    while i <100:
        if i % 2 == 1:
            sum += i
        else:
            sum -= i
        i += 1
    print (sum)
    View Code

     if 条件

    """
    if 条件:
        满足条件执行代码
    else:
        if条件不满足就走这段
    """
    AgeOfOldboy = 48
    if AgeOfOldboy > 50 :
        print("Too old, time to retire..")
    else:
        print("还能折腾几年!")
    View Code

    if... else...

    if 条件:
        满足条件执行代码
    elif 条件:
        上面的条件不满足就走这个
    elif 条件:
        上面的条件不满足就走这个
    elif 条件:
        上面的条件不满足就走这个    
    else:
        上面所有的条件不满足就走这段
    View Code

    猜数字小游戏

    count = 47
    flag = True
    while flag:
        guess = int(input('请输入你要猜的数字:'))
        if guess > count:
            print ('猜得太大了,往小里猜')
        elif guess < count:
            print ('猜得太小了,再大一点')
        else:
            print ('哈哈!恭喜你猜对啦!')
            flag = False
    View Code

      要求用户输入0-100的数字后,你能正确打印他的对应成绩

    score = int(input("输入分数:"))
    
    if score > 100:
        print("我擦,最高分才100...")
    elif score >= 90:
        print("A")
    elif score >= 80:
        print("B")
    elif score >= 60:
        print("C")
    elif score >= 40:
        print("D")
    else:
        print("太笨了...E")
    View Code

    while 循环

      while 条件:

        循环体
     
        如果条件为真,那么循环体则执行
        如果条件为假,那么循环体不执行
    循环终止语句

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

    • break用于完全结束一个循环,跳出循环体执行循环后面的语句
    • continue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的循环,break则完全终止循环

    continue例子

    count = 0
    while count <= 100:
        count += 1
        if count > 5 and count <95:#只要count在6到94之间,就不走下面的print语句,直接进入下一次loop
            continue
        print ('loop',count)
    print ('----out of while loop----')
    View Code

    while ... else ..

      与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句

      while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句

    import time
    count = 0
    while count <= 5:
        count += 1
        print ('loop',count)
    else:
        time.sleep(1)
        print ('循环正常执行完啦')
    print ('----out of while loop----')

      如果执行过程中被break啦,就不会执行else的语句

    count = 0
    while count <= 5 :
        count += 1
        if count == 3:break
        print("Loop",count)
    
    else:
        print("循环正常执行完啦")
    print("-----out of while loop ------")

      用户登陆(三次机会重试)

    i = 1
    while i <= 3:
        username = input('请输入你的名字:')
        password = input('请输入你的密码:')
        if username == 'alex' and password == 'alex3714':
            print ('欢迎登录!')
            break
        else:
            print ('输入错误,请重新输入')
        i += 1
        if i == 4:
            print ('登录失败')
            break
    View Code
  • 相关阅读:
    权限管理
    Linux常用命令
    SSM的开发步骤分析
    03每日课后作业面试题汇总
    Redis常用命令
    大觅网07day
    消息队列面试题
    bzoj 3745: [Coci2015]Norma
    Codeforces 343E Pumping Stations
    UOJ #236. 【IOI2016】railroad
  • 原文地址:https://www.cnblogs.com/Big-Dinosaur/p/9885714.html
Copyright © 2011-2022 走看看