DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

are doing, and then you can use {} within your string to
insert values or other functions. Here is an example:


Click here to view code image


>>> name = 'Piper'
>>> name2 = 'Chris'
>>> print(f'{name2} says Hi to {name}!')
Chris says Hi to Piper!

For more on formatting strings and beautifying your
output, see the Python documentation.


FLOW CONTROL WITH CONDITIONALS


AND LOOPS


So far you have been exposed to many of the building
blocks of the Python language. The real power of a
programming language is in the mechanisms you can use
to embed logic and respond to different conditions by
changing the flow of operation. Python has three primary
control statements:


if: An if statement is a conditional statement that can compare values
and make branching decisions.
for: A for loop is a counting loop that can iterate through data a
specific number of times.
while: The while loop can iterate forever when certain conditions are
met.

You can use these three statements in various
combinations to create very sophisticated programs. In
this section you will see how each of these statements
work.


If Statements


An if statement starts with an if and then sets up a
comparison to determine the truth of the statement it is
evaluating and ending with a : to tell Python to expect
the clause (the action if the condition is true) block of
code next. As mentioned earlier in this chapter,

Free download pdf