Python Literals
Python literals
Python literals are a data type and can hold any value type, such as strings, numbers, and more.
Because Python literals are used to store base data for the source code of your software, they need to be robust enough to work with any data. Python literals can hold any data you need.
Strings
Strings are one of the most common data value types you will work with in Python. All string literals are enclosed between two quotes (''
) or double quotes (""
) characters.
s = 'It\'s Always Sunny' # in a single quotet = "It's Always Sunny" # in double quotesm = '''It\'sAlwaysSunny''' # multi-line Stringsqc = 'D' # character literal in single quotedqc = "R" # character literal in double quotes
Numeric
Numeric values are another value you will find yourself working with often.
nb = 0b10100 # Binary Literalsnd = 50 # Decimal Literalno = 0o320 # Octal Literalnh = 0x12b # Hexadecimal Literalnf = 24.8 # Float Literal
Booleans
Boolean values are beneficial, especially in the source code of your software, as they are frequently used to set operating parameters.
Booleans are two: True
and False
; each one are an alias of 0 (False
) and 1 (True
).
one = (1 == True) # truetwo = (2 == False) # falsethree = (3 == True) # falseboolTruMath = True + 10 # Result is 11boolFalseMath = False + 10 # Result is 10
Collections
Collections are also helpful, and the fact is you will likely need them to store collections of data in your source code.
numList = [1, 2, 3, 4, 5] # ListnumTuple = (1, 2, 3, 4, 5) # TuplenumSet = {'1', '2', '3', '4', '5'} # SetpeopleDict = {'D': 'Deandra', 'C': 'Charlie', 'M': 'Mac'} # Dictionary
Special
Finally, a special literal is used to signify a null value, and the None keyword can be used in a literal when you need a value to be null or โ no value.
iAmNone = None # Special literals