lists all the math operators in Python.
-2
and 30
, for example, are said to be integer values. The integer (or int) data type indicates values that are whole numbers. Numbers with a decimal point, such as 3.14
, are called floating-point numbers (or floats). Note that even though the value 42
is an integer, the value 42.0
would be a floating-point number.If you ever see the error message SyntaxError: EOL while scanning string literal
, you probably forgot the final single quote character at the end of the string, such as in this example:
str()
, int()
, and float()
functions.)The *
operator is used for multiplication when it operates on two integer or floating-point values. But when the *
operator is used on one string value and one integer value, it becomes the string replication operator. Enter a string multiplied by a number into the interactive shell to see this in action.
spam
variable in this example stores 'Hello'
until you replace it with 'Goodbye'
.It can be only one word.
It can use only letters, numbers, and the underscore (_
) character.
It can’t begin with a number.
You can compute expressions with a calculator or type string concatenations with a word processor. You can even do string replication easily by copying and pasting text. But expressions, and their component values—operators, variables, and function calls—are the basic building blocks that make programs. Once you know how to handle these elements, you will be able to instruct Python to operate on large amounts of data for you.
It is good to remember the different types of operators (+
, -
, *
, /
, //
, %
, and **
for math operations, and +
and *
for string operations) and the three data types (integers, floating-point numbers, and strings) introduced in this chapter.
A few different functions were introduced as well. The print()
and input()
functions handle simple text output (to the screen) and input (from the keyboard). The len()
function takes a string and evaluates to an int of the number of characters in the string. The str()
, int()
, and float()
functions will evaluate to the string, integer, or floating-point number form of the value they are passed.
In the next chapter, you will learn how to tell Python to make intelligent decisions about what code to run, what code to skip, and what code to repeat based on the values it has. This is known as flow control, and it allows you to write programs that make intelligent decisions.