Boston Python Workshop 3/Data types: Difference between revisions
Content added Content deleted
imported>Jesstess No edit summary |
imported>Jesstess |
||
Line 65: | Line 65: | ||
</pre> |
</pre> |
||
==== |
====String concatenation==== |
||
<pre> |
<pre> |
||
>>> print "Hello" |
>>> print "Hello" + "World" |
||
⚫ | |||
⚫ | |||
</pre> |
</pre> |
||
====String concatenation==== |
|||
====Printing strings==== |
|||
<pre> |
<pre> |
||
>>> print "Hello |
>>> print "Hello" |
||
⚫ | |||
⚫ | |||
>>> print "Hello", "World" |
>>> print "Hello", "World" |
||
Hello World |
Hello World |
Revision as of 15:28, 12 June 2011
Numbers: integers and floats
Addition
>>> 2 + 2 4
Subtraction
>>> 0 - 2 -2
Multiplication
>>> 2 * 3 6
Division
>>> 4 / 2 2 >>> 1 / 2 0
Integer divison produces an integer. You need a number that knows about the decimal point to get a decimal out of division:
>>> 1.0 / 2 0.5 >>> float(1) / 2 0.5
Remainder
>>> 4 % 2 0 >>> 4 % 3 1
Types
>>> type(1) <type 'int'> >>> type(1.0) <type 'float'>
Strings
>>> "Hello" 'Hello'
String concatenation
>>> print "Hello" + "World" HelloWorld
Printing strings
>>> print "Hello" Hello >>> print "Hello", "World" Hello World >>> print "Hello", "World", 1 Hello World 1
Types
>>> type("Hello") <type 'str'>