Skip to content

Summary

Summary

In this chapter, you learned about user input and loops in Python.

The chapter started out with a discussion f-strings in Python. f-strings are one way of inserting variables into strings and print() statements. In Python, f-strings are prepended with the letter f. variables and expression inside of f-strings are enclosed in curly braces { }.

The general form of an f-string is below.

fstring = f'Insert {variable}'

The next part of the chapter was user input and Python's input() function. Remember that the input() function outputs a string, not a float or integer.

Selection structures and if statements were discussed next. In Python, if statements can be used to run a block of code if a certain logical condition is True. The general form of an if statement in Python is below.

if <logical condition>:
    <code>

Remember that any code that runs if the <logical condition> is True must be indented the same number of spaces.

The final part of the chapter involved the repetion structures for loops and while loops. for loops run for a defined number of times and can be defined in the following way:

for i in range(<num>):
    <code>

Strings and lists can also be used in for loops.

The last part the chapter discussed while loops While loops run as long as a certain logical condition remains True. The general structure of a while loop in Python is below:

while <logical condition>:
    <code>

Just like in for loops, any code to run as part of the while loop must be indented the name number of spaces.

Key Terms and Concepts

loop

for loop

while loop

selection statement

if

repetition structure

f-string

iterable

input

Additional Resources

The input() function official documentation on Python.org:

https://docs.python.org/3/library/functions.html#input

Python 3's f-Strings: An Improved String Formatting Syntax (Guide) by Joanna Jablonski on RealPython.com:

https://realpython.com/python-f-strings/

Python Loops Tutorial by Satyabrata Pal on DataCamp.com:

https://www.datacamp.com/community/tutorials/loops-python-tutorial

If Statements from the Hands-on Python Tutorial by Dr. Andrew N. Harrington, Loyola University:

http://anh.cs.luc.edu/handsonPythonTutorial/ifstatements.html