Boston Python Workshop 3/Data types: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
(Created page with '==Numbers: integers and floats== ====Addition==== <pre> >>> 2 + 2 4 </pre> ====Subtraction==== <pre> >>> 0 - 2 -2 </pre> ====Multiplication==== <pre> >>> 2 * 3 6 </pre> ==…')
 
imported>Jesstess
No edit summary
Line 56: Line 56:
>>> type(1.0)
>>> type(1.0)
<type 'float'>
<type 'float'>
</pre>

==Strings==

<pre>
>>> "Hello"
'Hello'
</pre>

====Printing strings====

<pre>
>>> print "Hello"
Hello
</pre>

====String concatenation====

<pre>
>>> print "Hello" + "World"
HelloWorld
>>> print "Hello", "World"
Hello World
>>> print "Hello", "World", 1
Hello World 1
</pre>

====Types====

<pre>
>>> type("Hello")
<type 'str'>
</pre>
</pre>

Revision as of 15:27, 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'

Printing strings

>>> print "Hello"
Hello

String concatenation

>>> print "Hello" + "World"
HelloWorld
>>> print "Hello", "World"
Hello World
>>> print "Hello", "World", 1
Hello World 1

Types

>>> type("Hello")
<type 'str'>