Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Python Programming/Command-line one-liners

From Wikibooks, open books for an open world
<Python Programming
Thelatest reviewed version waschecked on13 October 2019. There is1 pending change awaiting review.

Python can run one-liners from an operating system command line using option -c:

  • python-c"print(3.0/2)"
    • Calculates and outputs the result.
  • python-c"import math;print(math.sin(1))"
    • Imports a module required and outputs sine value.
  • python-c"for i in range(1,11):print(i)"
    • Uses a loop to output numbers from 1 to 10.
  • python-c"for i in range(1,11):for j in range(1,11): print(i,j)"
    • Does not work; two loops in a line are an invalid syntax.
  • python-c"for i, j in ((i,j) for i in range (1,11) for j in range(1,11)): print(i, j)"
    • Outputs pairs using two analogues of loop within a comprehension.
  • echohey|python-c"import sys,re;[sys.stdout.write(line) for line in sys.stdin if re.search('he.', line)]"
    • Acts as grep: outputs each line of the input containing a substring matching a regular expression. Not a Python one-liner strength.
  • echohallo|python-c"import sys,re;[sys.stdout.write(re.sub('h[au]llo', 'hello', line)) for line in sys.stdin]"
    • Acts as sed: for each line of the input, performs a regex replacement and outputs the results. Again, not a Python one-liner strength.
  • python-mcalendar
    • Outputs a year's calendar using calendar module.
  • python-c"import playsound as p;p.playsound(r'C:\WINDOWS\Media\notify.wav')"
    • On Windows, plays notification sound. Requires installation of playsound module. The module works across platforms; what is Windows specific above is the file path.

External links

[edit |edit source]
Retrieved from "https://en.wikibooks.org/w/index.php?title=Python_Programming/Command-line_one-liners&oldid=4476609"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp