Skip to content

Files

Latest commit

b4e95d1 · Mar 13, 2024

History

History
199 lines (158 loc) · 4.17 KB

basic-data-types.md

File metadata and controls

199 lines (158 loc) · 4.17 KB

Basic Data Types

Integers and Floats

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.0

type()

The 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...

Division Operator

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.0

Floor Division Operator

The 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
5

Converting Between Float and Integer

Floats can be converted to integers with the int() function. This will round down.

>>> int(3.5)
3
>>> int(3.0)
3
>>> int(3.9)
3

Likewise, there is a float() command that can convert an int to a float.

>>> float(3)
3.0
>>> float(1)
1.0

There are also a couple of other ways we can convert an int to a float.

>>> 5 + 0.0
5.0
>>> 10 * 1.0
10.0

Strings

The 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

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
True

This 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
False

This time we get the result False because "Sydney" and "London" are not the same thing.

Truth Values

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
True

Using 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)
0

Something 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("")
False

However, converting bool to str gives us the words "True" and "False" in string form.

>>> str(True)
'True'
>>> str(False)
'False'

Summary

  • 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 a float.
  • The floor division operator // can return ints.
  • The float() and int() commands allow us to convert between float and int.
    • This can result in a loss of precision when a float is converted to an int as it will always round the value down to the closest integer.
  • The bool data type represents the values True and False.
  • Data types other than bool may still have a truth-value.

Prev | List of Contents | Next