Meaning | Datatype | Example Constants |
---|---|---|
Integer | int | 385 -47 0 |
Double Precision Floating Point | float | 34.87 -8.67e15 3.12e-27 0.0 |
String | str | "boy" 'boy' |
Logical or Boolean | bool | True False |
name = 'Larry' amt = 235.41The output should look like this:
Larry owes me $235.41. (*)Format the values of name and amt so that the output looks like (*). Format the output in four ways: (a) listing the values, (b) concatenating the values, (c) expression interpolation using an f-string, (d) using the string format method. Ans:
# (a) Listing the items. # sep="" is needed so there is no space between the fields. print(name, " owes me $", amt, ".", sep="") // or print(name, " owes me $" + str(amt) + ".") # (b) Concatenation. print(str(name) + " owes me $" + str(amt) + ".") # (c) Expression interpolation with F-string print(f"{name} owes me ${amt}.") # (d) String format method. print("{0:s} owes me ${1:6.2f}".format(name, amt))
# Example 1. # The prefix 0b designates a Python # binary integer constant. a = 0b00010110 print("{0} {1:02x}".format(a, a)) # Example 2. # No prefix on the 22 designates a # Python decimal constant. b = 22 print("{0:08b} {1:02x}".format(b, b)) # Example 3. # The 0x prefix designates a Python # hexadecimal (base 16) constant. c = 0x1e print("{0:08b} {1}".format(c, c))