zoukankan      html  css  js  c++  java
  • python特性

    
    
    # for用法
    for i in range(0,100,2):
        print(i)
    n = 0
    # while用法
    while n < 100:
        print(n)
        n += 2
    else:
        print("循环结束")
    # 实现99乘法口诀for实现
    for n in range(1,10):
        for m in range(1, n + 1):
            # end=' ' 以空格结束,默认是
    
            # 字符串前加f可写变量
            print(f'{n}*{m}={n*m}',end=' ')
        print("
    ")
    # 99乘法口诀while实现
    x = 1;
    while x < 10:
        y = 1
        while y < x + 1:
            print(f'{x}*{y}={x*y}',end=' ')
            y = y + 1
        x = x + 1
        print("
    ")
    # 流程控制break与continue
    for s in 'python':
        if s == 'y':
            continue
        elif s == 'o':
            break
        print(s);
    # 字符串
    print('你好'[0])
    #
    print('你好'[-1])
    #
    print('你好'[-2])
    #
    print('北京欢迎你'[2:4]) #切片
    # 欢迎
    print('北京欢迎你'[2:])
    # 欢迎你
    print('北京欢迎你'[:2])
    # 北京
    print('{}喜欢{}'.format('波妞','宗介')) # 变量填充
    print('are'+' you'+' ok?') # 字符串拼接
    
    #列表[]
    my_list = [1,2,'a',1.3]
    my_list.append('py') # 尾部插入
    my_list.insert(1,1.5) # 指定索引插入 
    my_list.extend('sss') # 追加序列
    print('删除{}'.format(my_list.pop())) # 删除尾部
    print('删除{}'.format(my_list.pop(0)))  # 指定索引删除
    my_list.remove('s')  # 指定值删除,删除距离0索引最近的一个[if have
    print(my_list)
    # [1.5, 2, 'a', 1.3, 'py', 's']
    
    # 元组()  --不可变的列表
    my_yz = (1,2)
    # my_yz[0] = 2 会报错
    
    #字典{}
    #键值对 键-->值
    user = {
        'name': 'Tom',
        'age' : 18
    }
    user['fav'] = '打篮球'
    print(user['name'])
    
    # 函数
    def my_fun():
        print("my_fun_run")
    my_fun() # 如果在函数定义的前面调用会报错
    
    
    # 文件读取
    f = open('33.txt',encoding='utf8')
    s= f.read()
    print(s)
    f.close
    # 文件写入
    # ,mode='w' :写入
    f = open('write_test.txt',mode='w',encoding='utf8')
    f.write('雨缥缈
    ')
    f.write('倦红尘
    ')
    f.close()
    
    # 类与对象
    class Person:
        def __init__(self, name, sex, bir):
            self.name = name
            self.sex = sex
            self.bir = bir
        def say(self, word):
            print(f'{self.name}说:"{word}"')
    z_s = Person('张三','','20120202')
    z_s.say('你好')
    input()

    猜数小游戏

    import random
    # 随机数模块
    target = random.randint(1,10)
    total_change = 3
    change = total_change
    print(f'猜数字游戏,数字在1-10之间,一共有{change}次机会')
    while change > 0:
        change = change - 1
        number = int(input("请输入"))
        if number > target:
            print("大了")
            continue
        elif number < target:
            print("小了")
            continue
        else:
            break
    if change:
        print(f"恭喜猜对,你猜了{total_change - change}次")
    else:
        print("失败乃成功之母")
  • 相关阅读:
    Android实战技巧之三十八:Handler使用中可能引发的内存泄漏
    android上FragmentTabHost实现自己定义Tab Indicator
    GSO/TSO/GRO等对VirtIO虚机的网络性能影响分析(by quqi99)
    经典开源作品
    @在php中的作用
    .net开发中常用的第三方组件
    如何正确并完全安装Visual Studio 2015企业版本?
    TinyPNG---一个压缩PNG的神站
    asp.net MD5 加密
    PHP判断访问者手机移动端还是PC端的函数,亲测好用
  • 原文地址:https://www.cnblogs.com/cl94/p/12271443.html
Copyright © 2011-2022 走看看