Python for Programmers: Basic Data Types
Welcome to the Basic Data Types lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
Python's basic data types include integers, floating point numbers, strings, and booleans. These mostly work in familiar ways, so we'll only discuss them briefly.
Python's Boolean values are
TrueandFalse. Note that they're nottrueandfalse! Normally, UpperCamelCased names indicate classes in Python, but this is an exception. It's an inconsistency on Python's part.>
1 == 1Result:
True
>
2 > 3Result:
False
Many other languages, most notably JavaScript, use C-style logical operators. In those languages,
&&means "and",||means "or", and!means "not". Python's operators are different: it uses the wordsand,or, andnot.>
True and TrueResult:
True
>
1 + 1 == 2 and 2 + 2 == 3Result:
False
>
False or TrueResult:
True
>
1 > 2 or 3 == 4Result:
False
>
not TrueResult:
False
>
not not TrueResult:
True
Although Python doesn't use the more common
&&operator style, its precedence rules match most other languages. As an example,not x or yis equivalent to(not x) or y.>
not True or TrueResult:
True
>
(not True) or TrueResult:
True
>
not (True or True)Result:
False
We can use the
boolbuilt-in function to convert other types into booleans. Any zero number (like0or0.0) converts toFalse, and any non-zero number converts toTrue.>
bool(1.5)Result:
True
>
bool(0.0)Result:
False
>
bool(37)Result:
True
>
bool(0)Result:
False
Strings work similarly. Empty strings convert to
Falseand non-empty strings convert toTrue.>
bool("abc def")Result:
True
>
bool("")Result:
False
Note that whitespace characters are still characters. A string with only whitespace converts to
True.>
bool(" ")Result:
True
We can use any value in boolean expressions. For example,
not valueworks even ifvalueisn't a boolean. In that case, Python uses theboolbuilt-in to automatically convertvalueinto a boolean. It's as if we didnot bool(value). (In a future lesson, we'll come back to this idea of "truthy" and "falsey" values in more detail.)>
not " "Result:
False
Python distinguishes between integers and floating point numbers. Integers are whole numbers: positive, negative, and 0. Floats contain decimal points, like "real numbers" in a math class. However, floats don't guarantee precision. A future lesson will cover floating point precision in more detail.
Any operation involving a float returns a float.
>
2.0 * 3.0Result:
6.0
Combining integers and floats gives us a float, even if the result is a round number.
>
3 * 1.5Result:
4.5
>
5 * 2.0Result:
10.0
>
10 + 3Result:
13
Python has the usual operators like
+,*, etc. It also has a convenient exponentiation operator,**, which many languages lack.>
3**2Result:
>
2**3Result:
8
We can compare integers against floats, like
some_int == some_float. However, floating point imprecision means that this is much more subtle than it seems. We'll see more details of floating point imprecision in a future lesson.>
2 == 2.0Result:
True
Python has special syntax for scientific notation with an explicit exponent. For example,
2.1e3is equivalent to2.1 * (10**3), or2.1 * 1000.>
2.1e3Result:
2100.0
This syntax always gives us floats. The
2e2in the next example doesn't include a decimal point, but it's equivalent to2.0 * 100.>
2e2Result:
200.0
Scientific notation also allows negative exponents.
1.2e-1gives the equivalent of1.2 * (10**-1), or1.2 / 10>
1.2e-1Result:
0.12
0.12and.12are equivalent in Python, so either one is a correct answer for that code example.>
0.12 == .12Result:
Strings go inside single or double quotes. (Though Python also has some additional string syntax that we'll see later.) We can get the length of a string with the
lenfunction.>
message = "Hello"len(message)Result:
5
We can combine strings with
+.>
name = "Amir"greeting = "Hello, " + name + "!"greetingResult:
'Hello, Amir!'
We can escape characters inside of strings with
\, which makes it clear that we're including a special character. For example."\""is a string containing a single character, a double quote.>
len("\"")Result:
1
In this case, it'd be better to use a single-quoted string so we don't have to escape the
"character at all.>
'"' == "\""Result:
Those are the basic types! Now we can move on to the deeper details that make Python Python.