Alright ! Let’s talk about Python literals those little snippets of code that make up our programs. In this guide, we’re going to cover string, bytes, integer, float and imaginary numbers. But first, a quick disclaimer: we won’t be referencing any external websites because who needs them when you have us?
String literals are the most common type of literal. They represent text enclosed in single or double quotes. For example:
# This script demonstrates the use of string literals in Python
# String literals are used to represent text enclosed in single or double quotes
# Here, we define a variable "my_string" and assign it the value "Hello, world!"
my_string = "Hello, world!"
# The "print" function is used to display the value of a variable or a string
# Here, we use it to print the value of "my_string"
print(my_string) # Output: Hello, world!
But what if you want to include a quote inside your string? That’s where the backslash comes in handy:
# This script demonstrates how to include a quote inside a string using the backslash character
# Define a variable called my_string and assign it a string value
my_string = "He said, \"Python is awesome!\""
# Print the value of my_string to the console
print(my_string) # Output: He said, "Python is awesome!"
# The backslash character is used to escape special characters, such as quotes, within a string.
# This allows the quote to be included as part of the string without causing an error.
Bytes literals are similar to strings but represent binary data. They’re enclosed in single quotes and can contain escape sequences like `\x01` for a byte with value 1 or `\\` for the backslash character itself. For example:
# Define a variable "my_bytes" and assign it a bytes literal value of "Hello" in ASCII format
my_bytes = b'\x48\x65\x6c\x6c\x6f'
# Print the value of "my_bytes" to the console
print(my_bytes) # Output: b'Hello'
Integer literals represent whole numbers. They can be written in base 10 (decimal), binary, octal or hexadecimal format. For example:
# Integer literals represent whole numbers. They can be written in base 10 (decimal), binary, octal or hexadecimal format. For example:
# Define an integer variable and assign it a value of 42
my_int = 42
# Print the value of my_int
print(my_int) # Output: 42
# Define a binary variable and assign it a value of 0b101010 (binary representation of 42)
my_bin = 0b101010
# Print the value of my_bin
print(my_bin) # Output: 42 (binary to decimal conversion)
# Define an octal variable and assign it a value of 0o76 (octal representation of 54)
my_oct = 0o76
# Print the value of my_oct
print(my_oct) # Output: 54 (octal to decimal conversion)
# Define a hexadecimal variable and assign it a value of 0xFFFF (hexadecimal representation of 65535)
my_hex = 0xFFFF
# Print the value of my_hex
print(my_hex) # Output: 65535 (hexadecimal to decimal conversion)
Float literals represent floating point numbers. They can be written with or without a decimal point and an optional exponent. For example:
# Float literals represent floating point numbers. They can be written with or without a decimal point and an optional exponent. For example:
# Define a variable "my_float" and assign it the value of 3.14
my_float = 3.14
# Print the value of "my_float"
print(my_float) # Output: 3.14
# Define a variable "my_float2" and assign it the value of 0.5e-2, which is equivalent to 0.005 in exponent notation
my_float2 = 0.5e-2
# Print the value of "my_float2"
print(my_float2) # Output: 0.005 (exponent notation)
Imaginary literals represent complex numbers with a real and imaginary part separated by the `j` or `J` character. For example:
# Importing the cmath module to use its functions for complex numbers
import cmath
# Defining a complex number using the imaginary literal notation
my_complex = 3 + 4j
# Using the rect() function from the cmath module to convert the complex number into its polar form
# The real and imaginary parts are specified as arguments, with the real part being 3 and the imaginary part being 4
print(cmath.rect(real=3, imag=4)) # Output: (3+4j)
In Python, numeric literals do not include a sign; for example, -1 is actually an expression composed of the unary operator and the literal 1. This means that if you want to represent negative numbers using literals in Python, you’ll need to use the `-` operator or the `int()` function with a string argument (e.g., `int(“-42”)`).
We hope this guide has been helpful! Remember to always use the appropriate type for your data and don’t forget about escape sequences when working with strings or bytes.