Last modified on 01 Oct 2021.
Input
# Get input from user and display (input's type is `string`)
age = input("Your age? ") # python 3, raw_input for python 2
print("Your age:", age) # don't need space after "age"
# Get the input and store to numbers list
numbers = list(map(int, input().split()))
# Get multi inputs on 1 line
x, y, z, n = (int(input()) for _ in range(4))
Comment
Using #
on each line.
# print("This is not showed.")
print("This is showed.)
This is showed.
Print / Display
# Print normally
print("Hello!") # python 3
print "Hello!" # python 2
# print with `format`
print("Hello {} and {}.".format("A", "B"))
# change order
print("Hello {2} and {1}.".format("A", "B"))
# Directly insert (python 3.6 or above)
b = "B"
print(f'Hello {"A"} and {b}.')
# long strings
print('This is a part of sentence.'
'This is other part.')
# Print normally
Hello!
Hello!
# print with `format`
Hello A and B.
# change order
Hello B and A.
# Directly insert
Hello A and B.
# long strings
This is a part of sentence. This is other part.
# print decimals
print("{:.6f}".format(number))
# print decimals
1.000000
# print multiples
print("1", 5, "thi") # there are spaces
# print multiples
1 5 thi
# first 20 characters, second 10 characters
print( '{:<20s} {:<10s}'.format(string_one, string_two) )
Display separated results (like in executing multiple code cells),
display(df_1)
display(df_2)
Logging
Check more here.
import logging
log = logging.getLogger(__name__)
# order of increasing severity
log.debug('something')
log.info('something')
log.warning('something')
log.error('something')
logger.critical('something')
# by default, the logging module logs the messages with a severity level of WARNING or above
# thus: debug and info aren't show by default
If the log.info()
doesn’t work, set below[ref]
,
logging.getLogger().setLevel(logging.INFO) # show all except "debug"
# or
logging.basicConfig(level=logging.DEBUG) # show all
# in the class
import logging
log = logging.getLogger(__name__)
log.info("abc")
log.debug("xyz")
log.info("abc: %s", abc)
log.debug("xyz: {}".format(xyz))
# in the jupyter notebook
import logging
log = logging.getLogger(__name__)
logging.basicConfig(level=os.environ.get("LOGLEVEL", "DEBUG"))