pyhton基础

作者: TimeSHU | 来源:发表于2018-04-22 17:47 被阅读3次

    Python基础

    - 基础

        1. 第一句python

            - 后缀名是可以是任意?

            - 导入模块时,如果不是.py文件

            ==> 以后文件后缀名是 .py

        2. 两种执行方式

            python解释器 py文件路径

            python  进入解释器:

            实时输入并获取到执行结果

        3. 解释器路径

            #!/usr/bin/env python

        4. 编码

            # -*- coding:utf8 -*-

            ascill    00000000

            &          00000001

            unicode    0000000000000000+

            &          0000000000000001

            中        001000000000000111110010

            utf-8      能用多少表示就是用多少表示

            &          00000001

            中        001000000000000111110010

            Python3 无需关注

            Python2 每个文件中只要出现中文,头部必须加

        5. 执行一个操作

            提醒用户输入:用户和密码

            获取用户名和密码,检测:用户名=root  密码=root

            正确:登录成功

            错误:登陆失败

            a. input的用法,永远等待,直到用户输入了值,就会将输入的值赋值给一个东西

        6. 变量名

            - 字母

            - 数字

            - 下划线

            PS:

                数字不能开头

                不能是关键字

                最好不好和python内置的东西重复 **

        7. 条件语句

            缩进用4个空格

                a.

                    n1 = input('>>>')

                    if "alex" == "alex":

                     n2 = input('>>>')

                    if n2 == "确认":

                        print('alex SB')

                    else:

                        print('alex DB')

                    else:

                        print('error')

            注意:

                        n1 = "alex"  赋值

                        n1 == 'alex'  比较,

            b.

                    if 条件1:

                        pass

                    elif 条件2:

                        pass

                    elif 条件3:

                        pass

                    else:

                        pass

                    print('end')

            c. 条件1

                    and  or

                        if n1 == "alex" or n2 == "alex!23":

                            print('OK')

                        else:

                            print('OK')

                    PS:

                            pass 代指空代码,无意义,仅仅用于表示代码块

        8. 基本数据类型

            字符串  -    n1 = "alex"  n2 = 'root'  n3 = """eric"""  n4='''tony'''

            数字    -    age=21        weight = 64  fight = 5 

            加减乘除等:

            字符串:

            加法:

                n1 = "alex"

                n2 = "sb"

                n4 = "db"

                n3 = n1 + n2 + n4

                # "alexsbdb"

            乘法:

                n1 = "alex"

                n3 = n1 * 10

            数字:

                n1 = 9

                n2 = 2

                n3 = n1 + n2

                n3 = n1 - n2

                n3 = n1 * n2

                n3 = n1 / n2

                n3 = n1 % n2

                n3 = n1 ** n2

    出题:

            11  12  13 ...

                num = 12

                n = num % 2

                if n == 0:

                    print('偶数')

                else:

                    print('奇数')

        9. 循环

            死循环

            while 1==1:

                print('ok')

        10. 练习题

            if条件语句

            while循环

            奇数偶数

                1、使用while循环输入 1 2 3 4 5 6    8 9 10

                    n = 1

                    while n < 11:

                        if n == 7:

                            pass

                        else:

                            print(n)

                        n = n + 1

                        print('----end----')

                2、求1-100的所有数的和

                    n = 1

                    s = 0

                    while n < 101:

                        s = s + n

                        n = n + 1

                    print(s)

                3、输出 1-100 内的所有奇数

                        n = 1

                        while n < 101:

                        temp = n % 2

                        if temp == 0:

                              pass

                        else:

                            print(n)

                        n = n + 1

                        print('----end----')

                4、输出 1-100 内的所有偶数

                        n = 1

                        while n < 101:

                        temp = n % 2

                        if temp == 0:

                            print(n)

                        else:

                            pass

                        n = n + 1

                        print('----end----')

            5、求1-2+3-4+5 ... 99的所有数的和

                        n = 1

                        s = 0 # s是之前所有数的总和

                        while n < 100:

                            temp = n % 2

                            if temp == 0:

                                s = s - n

                            else:

                                s = s + n

                            n = n + 1

                            print(s)

    相关文章

      网友评论

        本文标题:pyhton基础

        本文链接:https://www.haomeiwen.com/subject/ytpclftx.html