• python入门


    Python安装:无脑下一步。
    开发工具:pycharm
    输出语句:print(“hello world”)
    1. 定义变量:

    x=1
    y=2
    z=x+y
    print(z)

    2. 判断语句:

    # encoding=utf-8 #一般都放在第一行,代码中包含中文,所以需要设置编码,否则会报错.注:有中文注释都必须加上这个编码
    score = 90
    if score>=90: #每个条件后都需要加上冒号
    print("优秀") #没有{}来表示条件后的语句块,以条件后的第一个缩进为开始,最后一个缩进为结束
    print("很好")
    if 3>1:
    print("对的")
    elif score>=80:
    print("良好")
    print("一般")
    elif score>=60:
    print("及格")
    #值得一提的是默认情况下,代码中有中文需要注意,不光是在运行时,在运行后也是需要设置的,
    # 因为默认的输出中文会乱码。大家可以在这里设置。
    # File>>Settings>>Editor>>File Encodings>>Project Encodings 改成UTF-8 

    3. 循环:

    # encoding=utf-8
    for i in range(0,3): # range表示范围 表示0=<i<3
    print(i)
    # print("index"+i) 不支持这种字符串拼接,要使用format
    print("index {0} {1} {2}".format(i,"cn","po")) # 此处的format中的参数个数要和前面的大括号中的对应
    print("index {0} {1}".format(i,"cn","po")) # 此时表示我只需要format中的前两个参数,第三个参数会被忽略
    print("end")

      输出:

    0
    index 0 cn po
    index 0 cn
    1
    index 1 cn po
    index 1 cn
    2
    index 2 cn po
    index 2 cn
    end

    4. 定义函数def:

    # coding=utf-8
    def hello_world(): #使用def定义函数,无参函数
    print("hello world") #函数体同样以缩进来区分开始和结束
    
    def get_max(x, y):
    if x>y:
    return x
    else: #任何一个条件、函数等之后都需要加冒号表示其后的语句块准备开始
    return y #冒号后面必须有语句块,否则报错
    
    
    hello_world() #执行函数
    print(get_max(9,5)) #执行有参函数

      输出:

    hello world
    9

    5. OO面向对象class:

    # encoding=utf-8
    class FirstTest: #声明类
    def __init__(self, name): #构造函数(别的语言一般使用FirstTest(name)这种方式,self相当于this,必须带上)
    self._name=name; # 类内部变量_name
    def print_first(self): # 类中的方法
    print("Hello {0}".format(self._name))
    
    f = FirstTest("world") # 实例化对象,相当于new FirstTest("world")
    f.print_first() #调用对象中的方法

      输出:

    Hello world

    6. 继承:

    # encoding=utf-8
    class FirstTest: #声明类
    def __init__(self, name): #构造函数(别的语言一般使用FirstTest(name)这种方式,self相当于this,必须带上)
    self._name=name; # 类内部变量_name
    def print_first(self): # 类中的方法
    print("Hello {0}".format(self._name))
    
    class SecondTest(FirstTest): #直接在类名后加上(父类)就行,即“类名(父类)”
    def __init__(self, name): #构造函数
    FirstTest.__init__(self, name) #调用父类的构造函数
    def print_second(self):
    print("Second {0}".format(self._name))
    
    s = SecondTest("world")
    s.print_first()
    s.print_second()

    7. 引入其他文件的类:

    # coding=utf-8 第一种方法
    import HelloWorld #引入某个文件
    s = HelloWorld.SecondTest("import") # 实例化引入文件中的某个类
    s.print_second()
    # coding=utf-8 第二种方法
    from HelloWorld import * #从某个文件中引入某个类,可使用*代表引入全部
    s = SecondTest("import")
    s.print_second()
  • 相关阅读:
    centos7 install flash player
    【转载】网站配置Https证书系列(二):IIS服务器给网站配置Https证书
    【转载】网站配置Https证书系列(一):腾讯云申请免费的SSL证书的流程步骤(即https安全连接使用的证书)
    【转载】网站配置Https证书系列(三):IIS网站设置Http链接直接跳转Https安全连接
    【转载】 C#使用Union方法求两个List集合的并集数据
    【转载】程序设计过程中SQL语句Where 1=1的作用
    【转载】C#编程中两个List集合使用Intersect方法求交集
    【转载】腾讯云服务器域名解析操作详解
    【转载】C#中使用OrderBy和ThenBy等方法对List集合进行排序
    【转载】腾讯云服务器通过设置安全组放行对外端口号
  • 原文地址:https://www.cnblogs.com/wangmingshun/p/5339186.html
Copyright © 2020-2023  润新知