In Python there are two core numeric types that you typically deal with. int is the type for whole-numbers or integers, while float is the type for numbers with a decimal point.
two_int = 2
two_float = 2.0The type() command in Python shows us the type of some data.
>>> type(2)
<class 'int'>
>>> type(3.2)
<class 'float'>The meaning of class is something that will be explained a bit later...
The division operator / will always return a float. This is true when it is applied to float and int.
>>> 1 / 2
0.5
>>> 4 / 2
2.0
>>> 15 / 3.0
5.0The floor division operator // can be used to obtain an int result from division if two integers are given as inputs.
>>> 15.0 // 3
5.0
>>> 15 // 3
5Floats can be converted to integers with the int() function. This will round down.
>>> int(3.5)
3
>>> int(3.0)
3
>>> int(3.9)
3Likewise, there is a float() command that can convert an int to a float.
>>> float(3)
3.0
>>> float(1)
1.0There are also a couple of other ways we can convert an int to a float.
>>> 5 + 0.0
5.0
>>> 10 * 1.0
10.0The string type can hold text information. We can use either double or single quotation marks to create them.
>>> "hello"
'hello'
>>> 'hello'
'hello'In both cases, their type() will still be string.
>>> type("hello")
<class 'str'>
>>> type('hello')
<class 'str'>The + operator allows us to combine or concatenate strings. But sometimes you need to remember to add a space!
>>> "hello" + "world"
'helloworld'Instead we could do one of the following:
>>> "hello " + "world"
'hello world'
>>> "hello" + " world"
'hello world'
>>> "hello" + " " + "world"
'hello world'Booleans are a type that represent one of two possible values: True or False. They are an important part of conditional statements, which will be covered in more detail later in this tutorial.
Now let's revisit the example of using the is operator to see if two variables hold the same bit of data.
>>> name = "Sydney"
>>> city = name
>>> city is name
TrueThis is telling us that name and city are both storing the same value. Now we can try a similar thing, but this time with variables that contain different data.
>>> name = "Sydney"
>>> city = "London"
>>> name is city
FalseThis time we get the result False because "Sydney" and "London" are not the same thing.
There are data types other than booleans that can still have truth-values. Python considers empty strings and the number 0 in float and int form to be False. A non-empty string and any number other than 0 in int or float form is considered True.
>>> 1 == True
True
>>> 0 == True
False
>>> 0 == False
TrueUsing the float() and int() commands we can see what happens when bools are converted to numbers.
>>> float(True)
1.0
>>> float(False)
0.0
>>> int(True)
1
>>> int(False)
0Something similar can also be done the other way around.
>>> bool(1.0)
True
>>> bool(1)
True
>>> bool("hello")
True
>>> bool(0.0)
False
>>> bool(0)
False
>>> bool("")
FalseHowever, converting bool to str gives us the words "True" and "False" in string form.
>>> str(True)
'True'
>>> str(False)
'False'- Python
ints are a data type that store whole numbers. - Python
floats are a data type that store numbers with a decimal place. - The division operator
/will always return afloat. - The floor division operator
//can returnints. - The
float()andint()commands allow us to convert betweenfloatandint.- This can result in a loss of precision when a
floatis converted to anintas it will always round the value down to the closest integer.
- This can result in a loss of precision when a
- The
booldata type represents the valuesTrueandFalse. - Data types other than
boolmay still have a truth-value.
Prev | List of Contents | Next