val1 = 4 val2 = 1.237 val3 = "dog" print("{0:4d} {1:5.2f} {2:s}".format( val1, val2, str(val1 + val2) + val3))*a. 4 1.24 41.237dog
n = 13 print("{0:03d} {1:x} {2:02x} {3:08b}".format( _ n, n, n, n)a. 00001101 0d d 013 b. d 013 00001101 0d
25 81 95 15a. a = 25 81 95 15 b. a = 25, 81, 95, 15
# Problem 6 a = 0b01101101 print("{0:d} {1:02x}".format(a, a)) # Problem 7 b = 0x6d print("{0:08b} {1:d}".format(b, b)) # Problem 8 c = 117 print("{0:08b} {1:02x}".format(c, c)) # Output: 01110101 75 109 6d 01101101 109 # You can also omit the argument numbers like this: print("{:d} {:02x}".format(a, a)) print("{:08b} {:d}".format(b, b)) print("{:08b} {:02x}".format(c, c))
value1 = 0b00001011 value2 = 0b00001101 bitwise_and = value1 & value2 print("{0:08b} {1:02x} {2:d}".format(bitwise_and, _ bitwise_and, bitwise_and)) # Output: 00001001 09 9
# The prefixes 0b and 0x denote binary and # hex constants, respectively. a = 0x5c b = 110 print("{0:d} {1:02x} {2:08b}". format(a | b, a & b, a ^ b)) # Output: 126 4c 00110010
00000 41 6c 69 63 65 20 20 20 55 67 5c A l i c e U g \Interpretation: Field 1: Name is Alice (String), 3 bits for padding.
chr(0x2a) # Output: *
ord("*") # Output: 42
# Test standalone method round: x = 3.6556765 print(round(x, 3)) # Output: 3656 # Test standalone method type: s = "dog" print(type(s)) # Output: <class 'str'> # Test standalone method min: print(min(3, 1)) # Output: 1 # Test standalong method len: # The string s is defined above. print(len(s)) # Output: 3