O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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'
>>>

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required