#
, and extend to the end of the physical line. A comment may appear at thestart of a line or following whitespace or code, but not within a stringliteral. A hash character within a string literal is just a hash character.Since comments are to clarify code and are not interpreted by Python, they maybe omitted when typing in examples.>>>
. (It shouldn’t take long.)+
, -
, *
and /
work just like in most other languages(for example, Pascal or C); parentheses (()
) can be used for grouping.For example:2
, 4
, 20
) have type int
,the ones with a fractional part (e.g. 5.0
, 1.6
) have typefloat
. We will see more about numeric types later in the tutorial./
) always returns a float. To do floor division andget an integer result (discarding any fractional result) you can use the //
operator; to calculate the remainder you can use %
:**
operator to calculate powers 1:=
) is used to assign a value to a variable. Afterwards, noresult is displayed before the next interactive prompt:_
. This means that when you are using Python as a desk calculator, it issomewhat easier to continue calculations, for example:int
and float
, Python supports other types ofnumbers, such as Decimal
and Fraction
.Python also has built-in support for complex numbers,and uses the j
or J
suffix to indicate the imaginary part(e.g. 3+5j
).'...'
) ordouble quotes ('...'
) with the same result 2.
can be usedto escape quotes:print()
function produces a morereadable output, by omitting the enclosing quotes and by printing escapedand special characters:
to be interpreted asspecial characters, you can use raw strings by adding an r
beforethe first quote:''...''
or ''...''
. End of lines are automaticallyincluded in the string, but it’s possible to prevent this by adding a
atthe end of the line. The following example:+
operator, andrepeated with *
:+
:s[:i]+s[i:]
is always equal to s
:word[1:3]
is2.len()
returns the length of a string:str.format()
.%
operator are described in more detail here.append()
method (we will see more about methods later):len()
also applies to lists:a
and b
simultaneously get the new values 0 and 1. On the last line this is used again,demonstrating that the expressions on the right-hand side are all evaluatedfirst before any of the assignments take place. The right-hand side expressionsare evaluated from the left to the right.while
loop executes as long as the condition (here: a<10
)remains true. In Python, like in C, any non-zero integer value is true; zero isfalse. The condition may also be a string or list value, in fact any sequence;anything with a non-zero length is true, empty sequences are false. The testused in the example is a simple comparison. The standard comparison operatorsare written the same as in C: <
(less than), >
(greater than), (equal to), <=
(less than or equal to), >=
(greater than or equal to)and !=
(not equal to).print()
function writes the value of the argument(s) it is given.It differs from just writing the expression you want to write (as we didearlier in the calculator examples) in the way it handles multiple arguments,floating point quantities, and strings. Strings are printed without quotes,and a space is inserted between items, so you can format things nicely, likethis:**
has higher precedence than -
, -3**2
will beinterpreted as -(3**2)
and thus result in -9
. To avoid thisand get 9
, you can use (-3)**2
.n
have thesame meaning with both single ('...'
) and double ('...'
) quotes.The only difference between the two is that within single quotes you don’tneed to escape '
(but you have to escape '
) and vice versa.