Numbers, Strings, and Variables
You can assign to variables with =. There is no separate declaration required before you assign a variable:
>>> x=3 >>> x 3 >>> y=x*2 >>> y 6 >>>
Python supports all standard arithmetic operators. Note that the division operator ( / ) yields different results on integer and floating-point types:
>>> 22 / 7 3 >>> 22.0 / 7.0 3.14285714286 >>>
Scientists and mathematicians will be pleased to hear that complex numbers are fully supported using the letter j :
>>> (3 + 1j) * (3 - 1j) (10+0j) >>>
Strings can be wrapped in either double or single quotes. They can be concatenated with the + operator and repeated with the * operator. You can also access individual characters by their position:
>>> greeting = 'hello' >>> epithet = 'stranger' >>> greeting + ", " + epithet 'hello, stranger' >>> "spam" * 10 'spamspamspamspamspamspamspamspamspamspam' >>>
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access