zoukankan      html  css  js  c++  java
  • 【Python】Python3纯代码极简教程

      1 #!/usr/bin/python3
      2 
      3 '''
      4 Python3.6.x简单教程
      5 
      6  示例、注释
      7  交互式和脚本式编程
      8  变量类型
      9      数字(Number)
     10      字符串(String)
     11      列表(List)
     12      元组(Tuple)
     13      字典(Dictionary)
     14  运算符
     15  条件语句
     16      if条件语句
     17      Python中没有switch case语句
     18  循环语句
     19      While循环语句
     20      For循环语句
     21      循环嵌套
     22      Break语句
     23      Continue语句
     24  Pass语句
     25  函数
     26 
     27 '''
     28 
     29 # 常用python包
     30 # import string  字符串操作
     31 # import math    数学工具
     32 # import os      文件和文件夹操作
     33 # import datetime 日期时间操作
     34 # import random   随机数
     35 # import parser   解析文件
     36 # 其他第三方package
     37 # 了解Python自带工具包,参看《Python标准库》一书
     38 
     39 # 示例程序
     40 
     41 # print("Hello Python3.6")
     42 # # 查看python版本号
     43 # import sys
     44 # print("Python版本号:",sys.version)    # dsfsdsfdsd
     45 
     46 # 注释dasdfasdfadsfasdfasdfsadfasdfasdfasdfasdfsadfsdfsadfsadfsadfsadfsdfsadfsadfsadf
     47 # print("该行代码被注释掉了,所以不会打印出来")
     48 
     49 # 多行注释
     50 '''
     51 这是python中的多行注释
     52 这是另一行被注释的内容
     53 '''
     54 
     55 """
     56 sdfasdfsa
     57 sdfasdfsa
     58 sdfsadfas
     59 """
     60 
     61 '''
     62 fsdfsd
     63 
     64 '''
     65 
     66 # 变量赋值
     67 # num = 1
     68 # myname = "jack"
     69 # a,b,c = 1,2,3
     70 #
     71 # d = 9.05
     72 # e = 8
     73 #
     74 # print("myname:",type(myname))
     75 # print("b:",type(b))
     76 #
     77 # print(type(d))
     78 # print(type(e))
     79 #
     80 # myname,b = b,myname
     81 #
     82 # print("myname:",type(myname))
     83 # print("b:",type(b))
     84 #
     85 #
     86 # print("a,b,c的值分别为:",a,b,c)
     87 
     88 
     89 # 变量数据类型
     90 
     91 # 数字类型
     92 # number1 = 1                         # int型
     93 # number2 = 10.2                      # float型
     94 # number3 = 4654654564645645645       # long型
     95 # number4 = 2 + 3j                    # 复数
     96 #
     97 # print(number1)
     98 # print(number2)
     99 # print(number3)
    100 # print(number4)
    101 #
    102 # print(type(number1))
    103 # print(type(number2))
    104 # print(type(number3))
    105 # print(type(number4))
    106 
    107 # 字符串string
    108 # import string
    109 # myname = "jack"
    110 # address = 'shenzhen'
    111 # print(myname[0])
    112 # print(myname[0:4:3])
    113 # print(myname[1:])
    114 # print(myname * 2)
    115 # print('=' * 20)
    116 # print("我的名字是" + myname)
    117 # print("我的名字是",myname)
    118 
    119 # 列表list
    120 # 列表使用[]表示,其中的元素以逗号,隔开,列表是有序集合
    121 # list = ['jack',12,'shenzhen',45,23.34]
    122 # tinylist = ["apple","android"]
    123 # print(type(list))
    124 # print(list)
    125 # print(list[0])
    126 # print(list[1:3])
    127 # print(list * 2)
    128 # print(list + tinylist)
    129 # list[0]='tom'
    130 # print(list)
    131 
    132 
    133 # 元组tuple
    134 #元组使用()表示,其中的元素也是以逗号,隔开,不过元组不能二次赋值,相当于只读列表,元组是有序集合
    135 # tuple = ('jack',12,'shenzhen',45,23.34)
    136 # tinytuple = ("apple","android")
    137 # print(type(tuple))
    138 # print(tuple)
    139 # print(tuple[0])
    140 # print(tuple + tinytuple)
    141 # 元组值不允许修改,下面代码会报错
    142 # tuple[0] = 'tom'
    143 
    144 # 字典dict
    145 #字典使用{}表示,字典有索引key和对应的值value组成,字典值是通过索引来存取的,而非通过偏移来存取
    146 # dict = {}
    147 # dict['name'] = 'jack'
    148 # dict['age'] = 12
    149 # dict['address'] = 'shenzhen'
    150 # dict['hobby'] = 'coding'
    151 # dict[2] = 'demoitem'
    152 # print(type(dict))
    153 # print(dict['name'])
    154 # print(dict.keys())
    155 # print(dict.values())
    156 # print(dict)
    157 
    158 
    159 # 类型转换
    160 # x = 'a'
    161 # print(int(x))
    162 # float(x)
    163 # str(x)
    164 # tuple(x)
    165 # list(x)
    166 # dict(d)
    167 
    168 
    169 # 运算符
    170 # 算术运算符
    171 # 比较运算符
    172 # 赋值运算符
    173 # 逻辑运算符
    174 # 位运算符
    175 # 成员运算符
    176 # 身份运算符
    177 
    178 
    179 # 算术运算符
    180 # + - * / % ** //
    181 # a = 5
    182 # b = 3
    183 # print(a + b)
    184 # print(a - b)
    185 # print(a * b)
    186 # print(a / b)
    187 # print(a % b)
    188 # print(a ** b)
    189 # print(a // b)   # 返回商的整数部分
    190 
    191 # 比较运算符
    192 # ==  # 等于
    193 # !=  # 不等于
    194 # >   # 大于
    195 # <   # 小于
    196 # >=  # 大于等于
    197 # <=  # 小于等于
    198 # a = 5
    199 # b = 3
    200 # print(a == b)
    201 # print(a != b)
    202 # print(a > b)
    203 # print(a < b)
    204 # print(a >= b)
    205 # print(a <= b)   # 返回商的整数部分
    206 
    207 
    208 
    209 # 赋值运算符
    210 # =     赋值
    211 # +=    加法赋值
    212 # -=    减法赋值
    213 # *=    乘法赋值
    214 # /=    除法赋值
    215 # %=    取模赋值
    216 # **=   幂赋值
    217 # //=   取整除赋值
    218 # a = 10
    219 # b = 20
    220 # c = a + b
    221 # c += a
    222 # c = c + a
    223 # c -= a
    224 # c = c - a
    225 # c *= a
    226 # c = c * a
    227 # c /= a
    228 # c = c / a
    229 # c %= a
    230 # c = c % a
    231 # c **= a
    232 # c = c ** a
    233 # c //= a
    234 # c = c // a
    235 
    236 
    237 # 逻辑运算符
    238 # and
    239 # or
    240 # not
    241 
    242 # a = 10
    243 # b = 20
    244 # if (a and b):
    245 #     print("变量a和b都是true")
    246 # else:
    247 #     print("变量a和b至少有一个不为true")
    248 #
    249 # if (a or b):
    250 #     print("变量a和b至少有一个是true")
    251 # else:
    252 #     print("变量a和b都不为true")
    253 #
    254 # if (not a):
    255 #     print("变量a为false")
    256 # else:
    257 #     print("变量a为true")
    258 
    259 # 位运算符
    260 # &    按位与
    261 # |    按位或
    262 # ^    按位异或
    263 # ~    按位非
    264 # <<   左移
    265 # >>   右移
    266 # a = 2  # 000 00100
    267 # b = 4  # 0000 0100
    268 # print("a的二进制值:" + bin(a))
    269 # print("b的二进制值:" + bin(b))
    270 # print("a和b按位与的二进制值:" + bin(a & b))
    271 # print("a和b按位或的二进制值:" + bin(a | b))
    272 # print("a和b按位异或的二进制值:" + bin(a ^ b))
    273 # print("a按位非的二进制值:" + bin(~a))
    274 # print("a左移1位的二进制值:" + bin(a << 1))
    275 # print("a右移1位的二进制值:" + bin(a >> 1))
    276 
    277 # 成员运算符
    278 # 成员运算符用来判断某个变量值是否存在于给定的集合中
    279 # in      存在
    280 # not in  不存在
    281 # a = 10
    282 # b = 2
    283 # list = [1,2,3,4,5]
    284 #
    285 # if (not (a in list)):
    286 #     print("a的值存在于list中")
    287 # else:
    288 #     print("a的值不存在于list中")
    289 #
    290 # if (b in list):
    291 #     print("b的值存在于list中")
    292 # else:
    293 #     print("b的值不存在于list中")
    294 #
    295 # if (a not in list):
    296 #     print("a的值不存在于list中")
    297 # else:
    298 #     print("a的值存在于list中")
    299 
    300 
    301 
    302 # 身份运算符
    303 # 用于比较两个对象的存储单元
    304 # is      判断两个标识符是不是引用自一个对象,如果引用自同一个对象(同一个内存单元),则返回true,否则返回false
    305 # is not  判断两个标识符是不是引用自不同对象,如果引用自同一个对象(同一个内存单元),则返回false,否则返回true
    306 
    307 
    308 # a = 20
    309 # b = 30
    310 # if (a is b):
    311 #     print("a和b指向同一个内存单元")
    312 # else:
    313 #     print("a和b指向不同内存单元")
    314 #
    315 # print(id(a))
    316 # print(id(b))
    317 
    318 # if (a is not b):
    319 #     print("a和b指向不同内存单元")
    320 # else:
    321 #     print("a和b指向同一个内存单元")
    322 #
    323 # print(id(a))
    324 # print(id(b))
    325 #
    326 # # 修改变量b的值
    327 # b = 'tom'
    328 # if (a is b):
    329 #     print("a和b指向同一个内存单元")
    330 # else:
    331 #     print("a和b指向不同内存单元")
    332 #
    333 # print(id(a))
    334 # print(id(b))
    335 #
    336 # b = 79
    337 # print(id(b))
    338 #
    339 # if (a is not b):
    340 #     print("a和b指向不同内存单元")
    341 # else:
    342 #     print("a和b指向同一个内存单元")
    343 
    344 
    345  #is 和 == 区别
    346  #is 用于判断两个变量引用的对象是否是同一个,==用于判断引用的变量的值是否相等
    347 
    348 
    349 # 条件语句
    350 
    351 # Python中指定任何非0非空(null)的值为true,0或者null为false
    352 # if 判断条件:
    353 #     执行符合该条件下的语句
    354 # else:
    355 #     执行其他语句
    356 
    357 # a = 10
    358 # if (a > 20):
    359 #     print("a大于20")
    360 # else:
    361 #     print("a不大于20")
    362 
    363 #
    364 # gender = 'F'
    365 # if (gender == 'F'):
    366 #     print("此人为女生")
    367 # else:
    368 #     print("此人为男生")
    369 
    370 
    371 # if 判断条件1:
    372 #     执行符合条件1下的语句
    373 # elif 判断条件2:
    374 #     执行符合条件2下的语句
    375 # elif 判断条件3:
    376 #     执行符合条件3下的语句
    377 # else:
    378 #     如果上述条件都不符合,则执行该语句
    379 
    380 # score = 89
    381 # if (score == 100):
    382 #     print("成绩满分")
    383 # elif (score >= 90):
    384 #     print("成绩在90和100分之间")
    385 # elif (score >= 60):
    386 #     print("成绩在60和90分之间")
    387 # elif (score >= 0):
    388 #     print("成绩在0和60分之间")
    389 # else:
    390 #     print("成绩分数不正常")
    391 
    392 
    393 
    394 #  循环语句
    395 #    While循环语句
    396 # a = 5
    397 # while(a > 0):
    398 #     print("当前轮次a的值为:",a)
    399 #     a -= 1
    400 
    401 # import time
    402 # # 无限循环,即死循环
    403 # while(True):
    404 #     print("死循环...")
    405     #time.sleep(2)
    406 
    407 # while循环使用else语句
    408 # a = 5
    409 # while (a > 0):
    410 #     print("当前轮次a的值为:", a)
    411 #     a -= 1
    412 # else:
    413 #     print("a的值已经不再大于0:",a)
    414 
    415 
    416 #  For循环语句
    417 # for循环可以遍历任何序列的元素,如一个列表或者一个字符串。
    418 # for letter in 'Python':
    419 #     print("当前字符为:",letter)
    420 #
    421 # fruits = ['apple','banana','mango']
    422 # for current_fruit in reversed(fruits):
    423 #     print("当前水果为: ",current_fruit)
    424 
    425 # for num in range(10):
    426 #     print("当前数字: ",num)
    427 #
    428 # # for循环使用else语句
    429 #     for num in range(10):
    430 #         print("当前数字: ", num)
    431 #     else:
    432 #         print("数字已经打印完毕")
    433 
    434 
    435 
    436 #  循环嵌套
    437 # for嵌套循环
    438 # a = [1,2,3,4,5]
    439 # b = [1,1,1,1,1]
    440 # for a_item in a:
    441 #     sum = 0
    442 #     for b_item in b:
    443 #         sum += b_item
    444 #     else:
    445 #         sum += a_item
    446 #     print("列表b中所有元素与a中当前元素的总和为:",sum)
    447 # else:
    448 #     print("结束")
    449 
    450 
    451 # while嵌套循环
    452 # a = 5
    453 # while (a > 0):
    454 #     print('&' * a)
    455 #     b = 3
    456 #     while(b > 0):
    457 #         print('*' * b)
    458 #         b -= 1
    459 #     a -= 1
    460 
    461 
    462 
    463 
    464 #  Break语句和Continue语句
    465 # break:用于终止整个循环
    466 # continue:用于跳过当前循环轮次,开始下一轮循环
    467 
    468 # print("break语句示例")
    469 # for letter in 'Python':
    470 #     if letter == 'h':
    471 #         continue
    472 #     print("当前字母为:",letter)
    473 
    474 #
    475 # print("continue语句示例")
    476 # for letter in 'Python':
    477 #     if letter == 'h':
    478 #         continue
    479 #     print("当前字母为:", letter)
    480 
    481 
    482 
    483 #  Pass语句
    484 # Python中的pass语句为空语句,即不执行任何内容,表示直接跳过的意思。
    485 # for letter in 'Python':
    486 #     if letter == 'h':
    487 #         pass
    488 #         print("这是pass块")
    489 #     print("当前字母为:",letter)
    490 # print("循环结束,bye")
    491 
    492 
    493 
    494 
    495 
    496 #  函数
    497 # 函数是为了重复使用的代码块,Python中提供了很多内建函数,不过我们也可以定义自己的函数,称为自定义函数。
    498 # 定义函数
    499 # def add(a,*b):
    500 #     "定义一个加法函数,返回两个数的和"
    501 #     print("a的值为:",a)
    502 #     sum = 0
    503 #     sum += a
    504 #     #print("b的值为:",b)
    505 #     for var in b:
    506 #         sum += var
    507 #     return sum
    508 # #
    509 # #
    510 # # # 调用函数
    511 # print(add(1,8,3,2,4,5,6))
    512 
    513 # 参数类型
    514 
    515 # 必备参数:必备参数必须以正确的顺序传入函数,调用时的数量必须和定义时的一样
    516 # 关键字参数:使用关键字参数允许函数调用时参数的顺序与定义时不一致,因为此时Python解释器能够用参数名称匹配参数值
    517 #默认参数:调用函数时,如果没有传入默认参数的值,那么就会利用定义时的默认值
    518 # 不定长参数:你可能需要一个函数能处理比当初定义时更多的参数,这些参数称为不定长参数,这种参数声明时不会命名
    519 
    520 # 必备参数
    521 #print(add(1,2))
    522 
    523 # 关键字参数
    524 #print(add(b = 3,a = 8))
    525 #
    526 # # 默认参数,也称缺省参数,默认参数必须在参数列表的最后一个
    527 # def printinfo(name,age = 12):
    528 #     print("name:",name)
    529 #     print("age:",age)
    530 # #
    531 # printinfo("jack",34)
    532 # printinfo("tom")
    533 
    534 
    535  # 不定长参数,加了*号的变量名会存放所有未命名的变量参数
    536 # def func_name([formal_args,] *var_args_tuple):
    537 #     statements
    538 
    539 # def printinfo(name,*vartuple):
    540 #     print("输出:")
    541 #     print("name:",name)
    542 #     print("打印不定长参数:")
    543 #     for var in vartuple:
    544 #         print(var)
    545 #     print("参数打印结束")
    546 #
    547 # #
    548 # printinfo("jack")
    549 # printinfo("tom",14,"john","shenzhen",5.03)
    550 # printinfo("tom",14,"john")
    551 
    552 
    553 # 变量作用域:全局变量 局部变量
    554 # 定义在函数内部的变量拥有局部作用域,定义在函数外部的变量拥有全局作用域
    555 # 局部变量只能在其被定义的函数内部访问,而全局变量则可以在整个程序范围内访问。
    556 
    557 # total = 9   # 全局变量
    558 # def sum (arg1,arg2):
    559 #     global total
    560 #     print(total)
    561 #     total = arg1 + arg2   # 局部变量total
    562 #     #print("函数内是局部变量:",total)
    563 #     return total
    564 #
    565 # print(sum(1,2))
    566 # print(total)
  • 相关阅读:
    MongoDB 创建数据库
    MongoDB
    MongoDB 概念解析
    window平台安装 MongoDB(二)
    MongoDB入门学习(1)
    解决DevExpress10.2.4版本在VS2012工具箱控件不显示的问题
    Aspose.Word 输出表格后空格字符丢失的解决方法
    ArcEngine 创建空间参考设置默认域
    SPATIALITE 各版本数据库差异
    WGS84投影的WKID说明
  • 原文地址:https://www.cnblogs.com/leejack/p/9526641.html
Copyright © 2011-2022 走看看