The
f"The sum of {i} and {j} is {sum}."
is a
formatted
string literal.
10 20 30 The sum of i and j is sum. The sum of 10 and 20 is 30.
A variable is a container that can contain a value. We put a value into a variable by writing an assignment statement. To the left of the equal sign, write the name of the varible. To the right of the equal sign, write the expression whose value should be copied into the variable. A value always travels from right to left through an equal sign.
I’m afraid that the official documentation defines a variable to be a mutable object. This is like Plato’s definition of a man as a featherless biped.
The name of a variable must begin with a letter (upper or lowercase)
or an underscore (_
).
The remaining characters (if any) in the name
can be letters (upper or lowercase), digits, or underscores.
Do not use one of the
keywords
of the language (e.g.,
import
)
as the name of a variable.
And
remember
that variable with the funny name
__class__
?
Well, for the time being,
don’t start and end your variables names with double underscores.
See
Identifiers
and keywords.
print(f"The sum of {i} and {j} is {sum}.")
The sum of 10 and 20 is 30.Before they invented formatted string literals, I had to set the
print
separator to the empty string
""
to prevent a space from being output
before the period at the end of the sentence.
print("The sum of ", i, " and ", j, " is ", sum, ".", sep = "")
Other ways to get the same output are
print("The sum of", i, "and", j, "is", sum, end = "") print(".")
print("The sum of " + str(i) + " and " + str(j) + " is " + str(sum) + ".\n")
str
ing
into the variables.
i = "cup" j = "cake" compound = i + j plural = compound + "s" print(f"The sum of {i} and {j} is {compound}.") print(f"The plural of {compound} is {plural}.")
The sum of cup and cake is cupcake. The plural of cupcake is cupcakes.
i = "cup" j = "cake" compound = i + j plural = compound + "s" print(f"The sum of \"{i}\" and \"{j}\" is \"{compound}\".") print(f"The plural of \"{compound}\" is \"{plural}\".")
The sum of "cup" and "cake" is "cupcake". The plural of "cupcake" is "cupcakes".Another way to put a double quote into a string is to surround the string with
'
single quotes'.
i = "cup" j = "cake" compound = i + j plural = compound + "s" print(f'The sum of "{i}" and "{j}" is "{compound}".') print(f'The plural of "{compound}" is "{plural}".')
The sum of "cup" and "cake" is "cupcake". The plural of "cupcake" is "cupcakes".
#Make sure you have the same number of items on each side of the = i, j, k = 10, 20, 30 print(i, j, k)
10 20 30
i = 10 j = 20 #Try to swap the values of i and j. i = j j = i print(f"Now i contains {i} and j contains {j}.")
Now i contains 20 and j contains 20.
One way to swap is by using a third variable:
i = 10 j = 20 #Swap the values of i and j. temp = i i = j j = temp print(f"Now i contains {i} and j contains {j}.")
Now i contains 20 and j contains 10.
In Python, a simpler way to swap is by using a multiple assignment:
i = 10 j = 20 #Swap the values of i and j. #Make sure you have the same number of variables on each side of the = i, j = j, i print(f"Now i contains {i} and j contains {j}.")
Now i contains 20 and j contains 10.
x, y, z = [10, 10, 10] #[10, 10, 10] is a list of three ints print(x, y, z)
10 10 10
x, y, z = 3 * [10] #3 * [10] is a list of three ints print(x, y, z)
10 10 10
x = y = z = 10 #chained assignment print(x, y, z)
10 10 10