Python Ternary Operator All In One
Python Ternary Expression / Python
Ternary Operator
Python 三元表达式 / Python 三元运算符
Python ternary operator
error ❌
import os
unicode = os.path.supports_unicode_filenames;
# print("\nunicode =", unicode ? "✅" : "❌")
# ❌ SyntaxError: invalid syntax
solution ✅
A
if
conditionelse
B
#!/usr/bin/env python3
# coding: utf8
__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:[email protected]
"""
import os
import platform
unicode = os.path.supports_unicode_filenames;
print("\nunicode =", "✅" if unicode else "❌")
# unicode = ✅
JavaScript ternary operator
const flag = true;
const emoji = flag ? "✅" : "❌";
console.log("emoji =", emoji);
// emoji = ✅
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator
demos
os & platform
https://docs.python.org/zh-cn/3/library/os.html
https://docs.python.org/zh-cn/3/library/os.path.html#module-os.path
https://docs.python.org/zh-cn/3/library/platform.html
$ python3 ./os-platform.py
$ python3 ./os-platform.py
#!/usr/bin/env python3
# coding: utf8
__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:[email protected]
"""
import os
import platform
# print("os =", os)
# os = <module 'os' from '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/os.py'>
# __path = os.path
# print("__path =", __path)
# __path = <module 'posixpath' from '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/posixpath.py'>
path = './os.py'
shell = './multi-line-comments.sh'
cat1 = '/Users/xgqfrms-mm/Documents/github/math_package_project/000-xyz/cat-1.sh'
cat2 = '/Users/xgqfrms-mm/Documents/github/math_package_project/000-xyz/cat-2.sh'
abspath = os.path.abspath(path)
basename = os.path.basename(path)
# commonpath = os.path.commonpath([path, shell, cat1, cat2])
# ValueError: Can't mix absolute and relative paths ❌
# 相对路径与绝对路径不可以混用
# commonpath = os.path.commonpath([path, shell])
# 相对路径,返回空
commonpath = os.path.commonpath([cat1, cat2])
commonprefix = os.path.commonprefix([cat1, cat2])
print("abspath =", abspath)
# abspath = /Users/xgqfrms-mm/Documents/github/math_package_project/000-xyz/os.py
print("basename =", basename)
# basename = os.py
print("commonpath =", commonpath)
# commonpath = /Users/xgqfrms-mm/Documents/github/math_package_project/000-xyz
print("commonprefix =", commonprefix)
# commonprefix = /Users/xgqfrms-mm/Documents/github/math_package_project/000-xyz/cat-
unicode = os.path.supports_unicode_filenames;
# Python Ternary Operator
标签:__,Python,print,platform,Ternary,xgqfrms,Operator,path,os
From: https://www.cnblogs.com/xgqfrms/p/17362985.html