Python hello world - 图1
© getcodify.com

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Integer/Float calculation

  1. #######################
  2. ########## caculating #
  3. #######################
  4. Operator Description Example
  5. + Addition operator 100 + 45 = 145
  6. - Subtraction operator 500 - 65 = 435
  7. * Multiplication operator 25 * 4 = 100
  8. / Float Division Operator 10 / 2 = 5.0
  9. // Integer Division Operator 10 / 2 = 5
  10. ** Exponentiation Operator 5 ** 3 = 125
  11. % Remainder Operator 10 % 3 = 1 # 10 / 3 = 3 ... 1

Float formate

  1. ##########print##########
  2. pi = 3.141592653
  3. print('%10.3f' % pi) #字段宽10,精度3
  4. ## 3.142
  5. print("pi = %.*f" % (3,pi)) #用*从后面的元组中读取字段宽度或精度
  6. ## pi = 3.142
  7. print('%010.3f' % pi) #用0填充空白
  8. ## 000003.142
  9. print('%-10.3f' % pi) #左对齐
  10. ## 3.142
  11. print('%+f' % pi) #显示正负号
  12. ## +3.141593

List

  1. ## duplicats removing
  2. A = [1,1,1,2,3,4,3]
  3. print(A)

Remove duplicates from list

  1. list(set(A))

int list to str list

  1. [str(x) for x in int_list]

or

  1. a_list = ["1", "2", "3"]
  2. ##int list to str list
  3. list(map(str, a_list))
  4. ##str list to int list
  5. list(map(int, a_list))

逐个相减

  1. c = [b[i] - a[i] for i in range(len(a))]

Compare Two list

  1. l = [1, 2, 3, 5]
  2. l_one = [2, 8, 6, 10]
  3. print set(l) & set(l_one)

Time

  1. import time
  2. print(time.time())
  3. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

Date caculating

zhengxiangwen 2017

  1. import datetime #导入日期时间模块
  2. today = datetime.date.today() #获得今天的日期
  3. print today #输出今天日期
  4. 2016-01-25
  5. yesterday = today - datetime.timedelta(days=1) #用今天日期减掉时间差,参数为1天,获得昨天的日期
  6. print yesterday
  7. 2016-01-25
  8. tomorrow = today + datetime.timedelta(days=1) #用今天日期加上时间差,参数为1天,获得明天的日期
  9. print tomorrow
  10. 2016-01-25
  11. print "昨天:%s, 今天:%s, 明天:%s" % (yesterday, today, tomorrow) #

For loop

  1. for x in range(0,10):
  2. print (x,end = '')

Read/Write a file

  1. ###read file
  2. f = open("demofile.txt", "r")
  3. print(f.read())
  4. with open(fname) as f:
  5. content = f.readlines()
  6. ### write to file
  7. fo = open("foo.txt", "w")
  8. fo.write( "www.runoob.com!\nVery good site!\n")
  9. fo.close()
  10. ## wirte at the end of the file
  11. with open('something.txt', 'a') as f:
  12. f.write('text to be appended')

print

  1. A =" "
  2. B =u"\u2581"
  3. C =u"\u2582"
  4. D =u"\u2583"
  5. E =u"\u2584"
  6. F =u"\u2585"
  7. G =u"\u2586"
  8. H =u"\u2587"
  9. I =u"\u2588"
  10. for i in A,B,C,D,E,F,G,H,I:
  11. print("\x1b[3;45;6m%s\x1b[0m"%(i),end='')

Python hello world - 图2

path

  1. import pathlib
  2. pathlib.Path(__file__).parent.absolute()
  3. import pathlib
  4. pathlib.Path().absolute()
  5. import os
  6. sys.path[0]
  7. ## Chage Workind Director
  8. import os
  9. os.chdir("../")

import

  1. ## import from the same directory
  2. import XXX
  3. ## import from the directories in the same directory
  4. from Directory import xxx
  5. ## import from .. directory
  6. import sys
  7. sys.path.append("..")
  8. import xxx

str to var

  1. for i in range(4):
  2. name='v'+str(i)
  3. locals()['v'+str(i)]=i
  4. print v1,v2,v3

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗