1.9 Chapter summary
This chapter introduced the basics of programming in Python, including:
printandinput.- Variables and assignment.
- Strings, integers, and floats.
- Arithmetic, concatenation.
- Common error messages.
- Comments and docstrings.
At this point, you should be able to write programs that ask for input, perform simple calculations, and output the results. The programming practice below ties together most topics presented in the chapter.
| Function | Description |
|---|---|
print(values) | Outputs one or more values, each separated by a space, to the user. |
input(prompt) | If present, prompt is output to the user. The function then reads a line of input from the user. |
len(string) | Returns the length (the number of characters) of a string. |
type(value) | Returns the type (or class) of a value. Ex: type(123) is <class 'int'>. |
| Operator | Description |
=(Assignment) | Assigns (or updates) the value of a variable. In Python, variables begin to exist when assigned for the first time. |
+(Concatenation) | Appends the contents of two strings, resulting in a new string. |
+(Addition) | Adds the values of two numbers. |
-(Subtraction) | Subtracts the value of one number from another. |
*(Multiplication) | Multiplies the values of two numbers. |
/(Division) | Divides the value of one number by another. |
**(Exponentiation) | Raises a number to a power. Ex: 3**2 is three squared. |
| Syntax | Description |
#(Comment) | All text is ignored from the # symbol to the end of the line. |
' or "(String) | Strings may be written using either kind of quote. Ex: 'A' and "A" represent the same string. By convention, this book uses double quotes (") for most strings. |
"""(Docstring) | Used for documentation, often in multi-line strings, to summarize a program's purpose or usage. |