![](images/bitsnbites/bits/hackerman.jpeg)
If you want to start coding and don't know how, then you might find some inspiration here!
< Go Back
NameError: name 'plt' is not defined Python 3.11 Mac Matplotlib
# Do this:
times_three = [x * 3 for i in range(10)] # [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
# Instead of this:
i = 0;
times_three = list()
for i in range(10):
times_three.append(i * 3)
# [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
As I said, it depends on the use-case but often this approach is much faster and much more fun to write!
A more detailed description of this feature can be found here.
.format()
function were both introduced as alternatives, but in Python 3.6,
a new method called F-strings was introduced. These F-strings allow you to easily create formatted strings by preceding the string with
an 'f' and enclosing the variables within braces. This allows for the use of arbitrary expressions, making the syntax more concise and
precise while also improving performance. Unless you are working with a version of Python earlier than 3.6, I recommended to use
F-strings for string formatting tasks
name = "Adrian"
some_float = 1.234567
# before
print("Hello my name is " + name + " This is a rounded float: " + str(round(some_float, 2)))
# Hello my name is Adrian This is a rounded float: 1.23
# after
print(f"Hello my name is {name}. This is a rounded float: {some_float:.2f}")
# Hello my name is Adrian This is a rounded float: 1.23
# even expressions are possible
print(f"8.4 times 5 is: {8.4 * 5}")
# 8.4 times 5 is: 42.0
I'd love to hear from you! Whether you have a question, feedback or just want to say hello, I'm always here to help. Fill out the form below and I'll get back to you as soon as possible. Thank you for reaching out! 😊