Problem Solving with Python

  • Problem Solving with Python
  • 1. Preface
    • 1.2. Acknowledgments
    • 1.3. Supporting Materials
    • 1.4. Formatting Conventions
    • 1.5. Errata
  • 2. Orientation
    • 2.2. Why Python?
  • 3. Jupyter Notebooks
    • 3.2. What is a Jupyter Notebook?
    • 3.3. Why Jupyter Notebooks?
    • 3.4. The Jupyter Notebook Interface
    • 3.5. Getting Help in a Jupyter Notebook
    • 3.6. Summary
    • 3.7. Review Questions
  • 4. The Python REPL
    • 4.2. Python as a Calculator
    • 4.3. Variables
    • 4.4. String Operations
    • 4.5. Print Statements
    • 4.6. Summary
    • 4.7. Review Questions
  • 5. Data Types and Variables
    • 5.2. Numeric Data Types
    • 5.3. Boolean Data Type
    • 5.4. Strings
    • 5.5. Lists
    • 5.6. Dictionaries and Tuples
    • 5.7. Combining Data Types
    • 5.8. Summary
    • 5.9. Review Questions
  • 6. NumPy and Arrays
    • 6.2. NumPy
    • 6.3. Installing NumPy
    • 6.4. Python Lists and NumPy Arrays
    • 6.5. Array Creation
    • 6.6. Array Indexing
    • 6.7. Array Slicing
    • 6.8. Array Operations
    • 6.9. Advanced topics
    • 6.10. Systems of Linear Equations
    • 6.11. Summary
    • 6.12. Review Questions
  • 7. Plotting with Matplotlib
    • 7.2. What is Matplotlib?
    • 7.3. Line Plots
    • 7.4. Saving plots
    • 7.5. Multi Line Plots
    • 7.6. Bar Charts and Pie Charts
    • 7.7. Error Bars
    • 7.8. Histograms
    • 7.9. Box Plots and Violin Plots
    • 7.10. Scatter Plots
    • 7.11. Plot annotations
    • 7.12. Subplots
    • 7.13. Plot Styles
    • 7.14. Contour Plots
    • 7.15. Quiver and Stream Plots
    • 7.16. 3D Surface Plots
    • 7.17. Summary
    • 7.18. Review Questions
  • 8. Functions and Modules
    • 8.2. Why Functions?
    • 8.3. First Function
    • 8.4. Functions with Multiple Arguments
    • 8.5. Functions with Default Arguments
    • 8.6. Calling Functions from Other Files
    • 8.7. Docstrings in Functions
    • 8.8. Positional and Keyword Arguments
    • 8.9. Summary
    • 8.10. Review Questions
  • 9. If Else Try Except
    • 9.2. User Input
    • 9.3. Selection Statements
    • 9.4. If statements
    • 9.5. If Else Statements
    • 9.6. Try-Except Statements
    • 9.7. Flowcharts
    • 9.8. Summary
    • 9.9. Review Questions
  • 10. Loops
    • 10.2. For Loops
    • 10.3. While Loops
    • 10.4. Break and Continue
    • 10.5. Flowcharts Describing Loops
    • 10.6. Summary
    • 10.7. Review Questions
  • 11. Appendix
    • 11.2. Reserved and Keywords in Python
    • 11.3. ASCII Character Codes
    • 11.4. Virtual Environments
    • 11.5. NumPy Math Functions
    • 11.6. Git and GitHub
    • 11.7. LaTeX Math
    • 11.8. Problem Solving with Python Book Construction
    • 11.9. Contributions
    • 11.10. Cover Artwork
    • 11.11. About the Author
Powered by Jupyter Book

4.5. Print Statements¶

One built-in function in Python is print(). The value or expression inside of the parenthesis of a print() function “prints” out to the REPL when the print() function is called.

An example using the print() function is below:

>>> name = "Gabby"
>>> print("Your name is: ")
Your name is: 
>>> print(name)
Gabby

Remember that strings must be enclosed by quotation marks. The following command produces an error.

>>> print(Gabby)

NameError: name 'Gabby' is not defined

This error is corrected by surrounding the string Gabby with quotation marks.

>>> print("Gabby")
Gabby

Expressions passed to the print() function are evaluated before they are printed out. For instance, the sum of two numbers can be shown with the print() function.

>>> print(1+2)
3

If you want to see the text 1+2, you need to define "1+2" as a string and print out the string "1+2" instead.

>>> print("1+2")
1+2

Strings can be concatenated (combined) inside of a print() statement.

>>> name = Gabby
>>> print('Your name is: ' + name)
Your name is Gabby

The print() function also prints out individual expressions one after another with a space in between when the expressions are placed inside the print() function and separated by a comma.

>>> print("Name:","Gabby","Age", 2+7)
Name: Gabby Age 9

previous

4.4. String Operations

next

4.6. Summary

By Peter Kazarinoff
© Copyright 2021.