• Python基础


    目录

      声明变量

      类型转换

      四则运算

      列表List

      循环

      判断

      字典

      文件IO

      函数


    声明变量

      

    # -*- coding: utf-8 -*-
    #整数
    a = 10
    print(a)
    #浮点数
    b = 10.1
    print(b)
    #字符串
    c = "aaa10"
    print(c)
    
    print(type(a))
    print(type(b))
    print(type(c))
    '''
    10
    10.1
    aaa10
    <class 'int'>
    <class 'float'>
    <class 'str'>
    '''

     返回目录

    类型转换 

     

    # -*- coding: utf-8 -*-
    a = "10"
    b = int(a)
    c = float(a)
    print(b)
    print(c)
    print(type(b))
    print(type(c))
    
    '''
    10
    10.0
    <class 'int'>
    <class 'float'>
    '''

     返回目录

    四则运算 

     

    # -*- coding: utf-8 -*-
    a = 2
    b = 3
    print(a + b)
    print(a - b)
    print(a * b)
    print(a / b)
    print(a ** b) #a的b次方
    
    '''
    5
    -1
    6
    0.6666666666666666
    8
    '''

     返回目录

    列表List 

      

    # -*- coding: utf-8 -*-
    print("---------------声明与添加元素:-----------------")
    a = []
    print(a)
    print(type(a))
    a.append("你好")
    a.append(3)
    print(a)
    '''
    []
    <class 'list'>
    ['你好', 3]
    '''
    
    print("---------------list索引与长度:-----------------")
    a = [2,4,6,8]
    print("list的长度是:" + str(len(a)))
    print(a[0])
    print(a[1])
    print(a[2])
    print(a[3])
    print(a[-1]) #打印最后一个元素
    '''
    list的长度是:4
    2
    4
    6
    8
    8
    '''
    
    print("---------------list切片:-----------------")
    a = [2,4,6,8,10]
    print(a[1:3]) 
    print(a[1:-1]) 
    print(a[:3])
    print(a[1:])
    '''
    [4, 6]
    [4, 6, 8]
    [2, 4, 6]
    [4, 6, 8, 10]
    '''
    
    print("---------------遍历list:-----------------")
    a = [1,3,5,7]
    print("迭代器方式:")
    for i in a:
        print(i)
    
    print("遍历下标方式:")
    for i in range(len(a)):
        print(a[i])
    '''
    迭代器方式:
    1
    3
    5
    7
    遍历下标方式:
    1
    3
    5
    7
    '''
    
    
    print("---------------判断元素是否在list:-----------------")
    a = [1,3,5,7]
    print(1 in a)
    #True

     返回目录

    循环 

     

    # -*- coding: utf-8 -*-
    a = [2,4,6,8]
    
    for i in a:
        print(i)
    '''
    2
    4
    6
    8
    '''
        
    for i in range(5):
        print(i)
    '''
    0
    1
    2
    3
    4
    '''
        
    k = 1
    while k<=3:
        print(k)
        k += 1
    '''
    1
    2
    3
    '''

     返回目录

    判断 

     

    # -*- coding: utf-8 -*-
    a = True
    b = False
    print(type(a))
    #<class 'bool'>
    
    a = 1
    b = 2
    print(a!=b)
    #True
    
    if a > b:
        print("a大于b")
    elif a < b:
        print("a小于b")
    else:
        print("a等于b")
    #a小于b

     返回目录

    字典 

     

    # -*- coding: utf-8 -*-
    print("---------------声明与添加元素:-----------------")
    a = {}
    print(a)
    print(type(a))
    a["Java"] = 1
    a["Python"] = 2
    a["Python"] = 3
    print(a)
    '''
    {}
    <class 'dict'>
    {'Java': 1, 'Python': 3}
    '''
    
    print("---------------dict索引与长度:-----------------")
    a = {'Java': 1, 'Python': 2}
    print("dict的长度是:" + str(len(a)))
    print(a['Java'])
    print(a['Python'])
    '''
    dict的长度是:2
    1
    2
    '''
    
    print("---------------遍历dict:-----------------")
    a = {'Java': 1, 'Python': 2, 'Scala': 3}
    
    for i in a.items():
        print(i)
    '''
    ('Java', 1)
    ('Python', 2)
    ('Scala', 3)
    '''
    
    print("---------------返回dict的keys和values:-----------------")
    a = {'Java': 1, 'Python': 2, 'Scala': 3}
    
    print(a.keys())
    print(a.values())
    # dict_keys(['Java', 'Python', 'Scala'])
    # dict_values([1, 2, 3])
    
    print("---------------应用:统计出现次数:-----------------")
    X = ['Java','Java','Python','Python','Python','Scala','Scala']
    a = {}
    for x in X:
        res = a.get(x)
        if res == None:
            a[x] = 1
        else:
            a[x] += 1
    print(a)
    # {'Java': 2, 'Python': 3, 'Scala': 2}
    # 注意: 这样的实现方式效率最高,因为dict的查询复杂度是O(1); List查询复杂度是O(n)

     返回目录

    文件IO 

     

    # -*- coding: utf-8 -*-
    a = open("a.txt", encoding='UTF-8')
    for i in a:
        print(i)
    a.close()
    
    b = open("b.txt", "w", encoding='UTF-8')
    b.write("aa
    ")
    b.write("bb
    ")
    b.write("cc
    ")
    b.close()

          

     返回目录

     返回目录

    函数 

     

    # -*- coding: utf-8 -*-
    def m1(name, age=5):
        h = 6
        if age > h:
            print(name + "已经" + str(age) + "岁了,到了上小学的年龄")
        else:
            print(name + "只有" + str(age) + "岁,还不能上小学")
    
    m1("tom", 2)
    m1("peter")
    m1("tom", 10)
    '''
    tom只有2岁,还不能上小学
    peter只有5岁,还不能上小学
    tom已经10岁了,到了上小学的年龄
    '''

     返回目录

  • 相关阅读:
    Building Seam 2.0 Application with NetBeans 6.1
    Better Builds with Maven学习笔记
    NetBeans Globel Translation Team Tshirt!
    Participate in MySQLGlassFish Student Contest and Win $500
    NetBeans Globel Translation Team Tshirt!
    Better Builds with Maven学习笔记
    Building Seam 2.0 Application with NetBeans 6.1
    Maven2 的新特性
    Participate in MySQLGlassFish Student Contest and Win $500
    数据库设计及数据缓存
  • 原文地址:https://www.cnblogs.com/itmorn/p/8149204.html
Copyright © 2020-2023  润新知