zoukankan      html  css  js  c++  java
  • day01-Python基础

    一:变量

    声明变量

    >>> name = "Guido"

    声明一个变量,变量名为:name  变量name的值为:"Guido"

    #可变的量

    #存储数据

    #避免重复代码

    变量定义的规则

    • 变量名只能是 字母、数字或下划线的任意组合
    • 变量名的第一个字符不能是数字
    • 以下关键字不能声明为变量名:['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']
    • Age_of_guido #一般首字母大写,用于定义类;AGE_OF_GUIDO#大写,一般用于定义常量

    变量的赋值

    >>> name2 = name
    >>> print(name,name2)
    Guido Guido
    >>> name = "Linus"
    >>> print(name,name2)
    Linus Guido

    #%s %d %f %r

    >>> msg = "my name is %s and age is %s" %("Guido",45)
    >>> print(msg)
    my name is Guido and age is 45

    >>> msg = "my name is %s and age is %d" %("Guido",45)
    >>> print(msg)
    my name is Guido and age is 45

    >>> msg = "my name is %s and age is %f" %("Guido",45)
    >>> print(msg)
    my name is Guido and age is 45.000000

    二:数据类型

    数字:

    int(整型)、long(长整型)
    
    float(浮点型)、complex(复数)
    
    a = 27   数字
    
    a = "27"  字符串
    View Code

    布尔&字符串:

    布尔值 (真或假  Ture False 1或0)
    
    字符串 
    
    字符串常用功能:
    
    移除空白、分割、长度、索引、切片、格式化输出
    
    移除空白:
    
    >>> name = "         Guido!"
    >>> name
    '         Guido!'
    >>> name.strip()
    'Guido!'
    
    长度:
    
    >>> len(name)
    15
    
    索引:
    
    >>> name[9]
    'G'
    >>> name[10]
    'u'
    >>> name[11]
    'i'
    >>> name[12]
    'd'
    
    >>> name[13]
    'o'
    
    切片:
    
    >>> name[9:13]
    'Guid'
    顾头不顾尾
    >>> name[9:14]
    'Guido'
    
    字符串拼接:
    
    >>> msg = "my name is " + name
    >>> msg
    'my name is Guido'
    
    格式化输出:
    
    >>> name = "Guido"
    >>> age= 56
    >>> msg = "my name is %s and i am %s years old!" % (name,age)
    >>> msg
    'my name is Guido and i am 56 years old!'
    View Code

    列表:

    >>> names = ['linus','scott','guido']
    >>> names[0]
    'linus'
    >>> names[1]
    'scott'
    >>> names[2]
    'guido'
    
    还可以反向取值
    
    >>> names[-3]
    'linus'
    
    列表修改:
    
    >>> names[-3] = "jack"
    >>> names
    ['jack', 'scott', 'guido']
    
    追加数据:
    
    >>> names.append('tom')
    >>> names
    ['jack', 'scott', 'guido', 'tom']
    
    插入数据:
    
    >>> names.insert(2,'larry')
    >>> names
    ['jack', 'scott', 'larry', 'guido', 'tom']
    
    删除数据:
    
    >>> del names[2]
    >>> names
    ['jack', 'scott', 'guido', 'tom']
    >>> names.remove('tom')
    >>> names
    ['jack', 'scott', 'guido']
    
    修改数据:
    
    >>> names
    ['jack', 'scott', 'guido', 'rain', 'rose']
    >>> names.index('guido')
    2
    >>> names[2] = "elice"
    >>> names
    ['jack', 'scott', 'elice', 'rain', 'rose']
    >>> names[names.index('rain')] = 'RAIN'
    >>> names
    ['jack', 'scott', 'elice', 'RAIN', 'rose']
    
    统计数据:
    
    ['jack', 'scott', 'elice', 'RAIN', 'rose', 'scott', 'scott']
    >>> names.count("scott")
    3
    
    >>> names.index("scott")
    1
    
    排序数据:
    
    >>> names.sort()
    >>> names
    ['RAIN', 'elice', 'jack', 'rose', 'scott', 'scott', 'scott']
    >>> names.insert(2,"!")
    >>> names.sort()
    >>> names
    ['!', 'RAIN', 'elice', 'jack', 'rose', 'scott', 'scott', 'scott']
    
    >>> names[2] = '5'
    >>> names
    ['!', 'RAIN', '5', 'jack', 'rose', 'scott', 'scott', 'scott']
    >>> names.sort()
    >>> names
    ['!', '5', 'RAIN', 'jack', 'rose', 'scott', 'scott', 'scott']
    
    >>> names.reverse()
    >>> names
    ['scott', 'scott', 'scott', 'rose', 'jack', 'RAIN', '5', '!']
    
    切片:
    
    >>> names
    ['scott', 'scott', 'scott', 'rose', 'jack', 'RAIN', '5', '!']
    >>> names[3:7]
    ['rose', 'jack', 'RAIN', '5']
    >>> names[-1]
    '!'
    >>> names[-3:-1]
    ['RAIN', '5']
    >>> names[-3:]
    ['RAIN', '5', '!']
    >>> names[0:5]
    ['scott', 'scott', 'scott', 'rose', 'jack']
    >>> names[:5]
    ['scott', 'scott', 'scott', 'rose', 'jack']
    
    >>> names
    ['scott', 'scott', 'scott', 'rose', 'jack', 'RAIN', '5', '!']
    >>> names[0:-1]
    ['scott', 'scott', 'scott', 'rose', 'jack', 'RAIN', '5']
    >>> names[0:-1:1]    1代表步长
    ['scott', 'scott', 'scott', 'rose', 'jack', 'RAIN', '5']
    >>> names[0:-1:2]    2代表步长
    ['scott', 'scott', 'jack', '5']
    View Code

    运算:

    算数运算:
    幂
    >>> 5**2
    25>>> 5/3
    1.6666666666666667>>> 5+2
    7>>> 5-2
    3>>> 5*2
    10
    取整除
    >>> 5//2
    2
    取模-返回除法的余数,奇数偶数
    >>> 5%2
    1
    >>> 5%3
    2
    >>> 5%4
    1
    >>> 5%5
    0
    比较运算:
    >>> a = 10
    >>> b = 5
    >>> a == b
    False
    >>> a != b
    True
    >>> a > b
    True
    >>> a < b
    False
    >>> a >= b
    True
    >>> a <= b
    False
    赋值运算:
    = 简单的赋值运算符 c = a + b将a + b的运算结果赋值为c
    += 加法赋值运算符  c += a等效于c = c + a
    -=  减法赋值运算符  c -= a等效于c = c - a
    += 乘法赋值运算符  c *= a等效于c = c * a
    += 除法赋值运算符  c /= a等效于c = c / a
    += 取模赋值运算符  c %= a等效于c = c % a
    += 幂赋值运算符     c **= a等效于c = c ** a
    += 取整除赋值运算符  c //= a等效于c = c // a
    
    逻辑运算  andornot
    and
    >>> today = "mon"
    >>> tomorrow = "tes"
    >>> today == "mon" and tomorrow == "teu"
    False
    >>> today == "mon" and tomorrow == "tes"
    True
    
    or
    >>> today == "mon" or tomorrow == "tes"
    True
    
    not
    
    成员运算:innot in
    >>> names
    [1, 2, 3, 4, 5, 6]
    >>> 7 in names
    False
    >>> 3 in names
    True
    >>> 10 not in names
    True
    
    身份运算:isis not
    >>> 8 is 8
    True
    >>> type(8)
    <class 'int'>
    >>> type(8) is int
    True
    >>> type(8) is not int
    False
    >>> type('eric') is not int
    True
    View Code

     位运算:

     

    #!/usr/bin/python
      
    a = 60            # 60 = 0011 1100
    b = 13            # 13 = 0000 1101
    c = 0
      
    c = a & b;        # 12 = 0000 1100
    print "Line 1 - Value of c is ", c
      
    c = a | b;        # 61 = 0011 1101
    print "Line 2 - Value of c is ", c
      
    c = a ^ b;        # 49 = 0011 0001 #相同为0,不同为1
    print "Line 3 - Value of c is ", c
      
    c = ~a;           # -61 = 1100 0011
    print "Line 4 - Value of c is ", c
      
    c = a << 2;       # 240 = 1111 0000
    print "Line 5 - Value of c is ", c
      
    c = a >> 2;       # 15 = 0000 1111
    print "Line 6 - Value of c is ", c
    

      

    三:表达式if ... else

    用户登陆验证:python3

    --python中单双引号没有任何区别

    --用途:>>>msg = "my name's guido..."    >>> msg = "a,'b',c,d"

    #!/usr/bin/env python
    name = input("用户名:")
    pwd = input("密码:")
    
    if name == "scott" and pwd =="tiger":
        print("Welcome,scott!")
    else:
        print("用户名和密码错误!")

     猜年龄:

    #!/usr/bin/env python
    my_age = 30
    user_input = int(input("your guess num:"))
    if user_input == my_age:
        print("Congratulations,you got it!")
    elif user_input < my_age:
        print("think bigger!")
    else:
        print("think smaller!")

    四:表达式for loop

    循环10次

    for i in range(10):
        print("loop:",i)
    

    输出:

    loop: 0
    loop: 1
    loop: 2
    loop: 3
    loop: 4
    loop: 5
    loop: 6
    loop: 7
    loop: 8
    loop: 9
    

    需求一:还是上面的程序,但是遇到小于5的循环次数就不走了,直接跳入下一次循环 

    for i in range(10):
        if i<5:
            continue #不往下进行,直接进入下一次loop
        print("loop:",i)
    View Code

    需求二:还是上面的程序,但是遇到大于5的循环次数就不走了,直接退出

    for i in range(10):
        if i>5:
            break #不往下进行,直接跳出整个loop
        print("loop:",i)
    View Code

    猜年龄

    AGE = 56
    for i in range(10):
        if i == 3:
            print("too many attemps,bye...")
            break
        guess_num = int(input("your guess num:"))
        if guess_num == AGE:
            print("Congratulations,you got it.")
            break
        elif guess_num > AGE:
            print("try smaller...")
        else:
            print("try bigger...")
    

    For 循环:引入计数器的场景。 break:跳出整个循环

    AGE = 56
    count = 0
    for i in range(10):
        if count == 3:
            user_confirm = input("do you want to keep guessing.?").strip()
            if user_confirm == "y":
                count = 0
            else:
                break
        guess_num = int(input("your guess num:"))
        if guess_num == AGE:
            print("Congratulations,you got it.")
            break
        elif guess_num > AGE:
            print("try smaller...")
        else:
            print("try bigger...")
    
        count += 1
    

    For循环(break,continue)

    continue:跳出本次循环,进入下一次循环

    for i in range(10):
        if i == 5:
            continue
        print("loop:",i)
    示例1
    for i in range(10):
        if i == 5:
            for j in range(10):
                print("inner loop",j)
            continue
        print("loop",i)
    示例2
    for i in range(10):
        if i == 5:
            for j in range(10):
                print("inner loop",j)
                if j == 6:
                    break
            continue
        print("loop",i)
    示例3

    五:while loop

    死循环:

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

    计数器:

    count = 0
    while True:
        if count < 3:
    
            user = input("请输入您的姓名: ")
            pwd = input("请输入账户密码: ")
    
            if user == "elon"  and pwd == "elon":
                print("Congratulations!!!")
                break
            else:
                count +=1
        else:
            print("too many error,bye!")
    
            break
    计数器

    循环100次就退出

    count = 0
    while True:
        print("count:",count)
        count +=1
        if count == 100:
            print("over!")
            break
    View Code

    for 循环的,如何实现让用户不断的猜年龄,但只给最多3次机会,再猜不对就退出程序。

    my_age = 30
    count = 0
    while count < 3:
        user_input = int(input("your guess num:"))
        if user_input == my_age:
            print("Congratulations,you got it!")
            break
        elif user_input < my_age:
            print("think bigger!")
        else:
            print("think smaller!")
        count += 1
    else:
        print("超过最大输入次数!")
    View Code
    import time
    t0_start = time.time()
    count0 = 0
    while True:
        if count0 == 1000000:
            break
        count0 += 1
    print("cost0:",time.time()-t0_start,count0)
    t_start = time.time()
    count = 0
    while count < 1000000:
        count += 1
    print("cost:",time.time()-t_start,count)
    View Code
  • 相关阅读:
    JavaScript 事件
    Docker 部署asp.netcore
    Docker 安装
    JavaScript 窗口操作
    JavaScript 定时器
    JavaScript Dom
    Javascript try catch es5标准模式
    JavaScript 数组去重
    JavaScript 返回具体类型方法
    mysql 触发器
  • 原文地址:https://www.cnblogs.com/elontian/p/6364854.html
Copyright © 2011-2022 走看看