What are the 4 types of data in python?
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Basic Data Types in Python Show
Now you know how to interact with the Python interpreter and execute Python code. It’s time to dig into the Python language. First up is a discussion of the basic data types that are built into Python. Here’s what you’ll learn in this tutorial:
IntegersIn Python 3, there is effectively no limit to how long an integer value can be. Of course, it is constrained by the amount of memory your system has, as are all things, but beyond that an integer can be as long as you need it to be: >>>
Python interprets a sequence of decimal digits without any prefix to be a decimal number: The following strings can be prepended to an integer value to indicate a base other than 10:
For example: >>>
For more information on integer values with non-decimal bases, see the following Wikipedia sites: Binary, Octal, and Hexadecimal. The
underlying type of a Python integer, irrespective of the base used to specify it, is called >>>
Floating-Point NumbersThe >>>
Complex NumbersComplex numbers are specified as >>>
StringsStrings are sequences of character data. The string type in Python is called String literals may be delimited using either single or double quotes. All the characters between the opening delimiter and matching closing delimiter are part of the string: >>>
A string in Python can contain as many characters as you wish. The only limit is your machine’s memory resources. A string can also be empty: What if you want to include a quote character as part of the string itself? Your first impulse might be to try something like this: >>>
As you can see, that doesn’t work so well. The string in this example opens with a single quote, so Python assumes the next single quote, the one in parentheses which was intended to be part of the string, is the closing delimiter. The final single quote is then a stray and causes the syntax error shown. If you want to include either type of quote character within the string, the simplest way is to delimit the string with the other type. If a string is to contain a single quote, delimit it with double quotes and vice versa: >>>
Escape Sequences in StringsSometimes, you want Python to interpret a character or sequence of characters within a string differently. This may occur in one of two ways:
You can accomplish this using a backslash ( Let’s see how this works. Suppressing Special Character MeaningYou have already seen the problems you can come up against when you try to include quote characters in a string. If a string is delimited by single quotes, you can’t directly specify a single quote character as part of the string because, for that string, the single quote has special meaning—it terminates the string: >>>
Specifying a backslash in front of the quote character in a string “escapes” it and causes Python to suppress its usual special meaning. It is then interpreted simply as a literal single quote character: >>>
The same works in a string delimited by double quotes as well: >>>
The following is a table of escape sequences which cause Python to suppress the usual special interpretation of a character in a string:
Ordinarily, a newline character terminates line input. So pressing Enter in the middle of a string will cause Python to think it is incomplete: >>>
To break up a string over more than one line, include a backslash before each newline, and the newlines will be ignored: >>>
To include a literal backslash in a string, escape it with a backslash: >>>
Applying Special Meaning to CharactersNext, suppose you need to create a string that contains a tab character in it. Some text editors may allow you to insert a tab character directly into your code. But many programmers consider that poor practice, for several reasons:
In Python (and almost all other common computer languages), a tab character can be specified by the escape sequence >>>
The escape sequence Here is a list of escape sequences that cause Python to apply special meaning instead of interpreting literally:
Examples: >>>
This type of escape sequence is typically used to insert characters that are not readily generated from the keyboard or are not easily readable or printable. Raw StringsA raw string literal is
preceded by >>>
Triple-Quoted StringsThere is yet another way of delimiting strings in Python. Triple-quoted strings are delimited by matching groups of three single quotes or three double quotes. Escape sequences still work in triple-quoted strings, but single quotes, double quotes, and newlines can be included without escaping them. This provides a convenient way to create a string with both single and double quotes in it: >>>
Because newlines can be included without escaping them, this also allows for multiline strings: >>>
You will see in the upcoming tutorial on Python Program Structure how triple-quoted strings can be used to add an explanatory comment to Python code. Boolean Type, Boolean Context, and “Truthiness”Python 3 provides a Boolean data type. Objects of Boolean type may have one of two values, >>>
As you will see in upcoming tutorials, expressions in Python are often evaluated in Boolean context, meaning they are interpreted to represent truth or falsehood. A value that is true in Boolean context is sometimes said to be “truthy,” and one that is false in Boolean context is said to be “falsy.” (You may also see “falsy” spelled “falsey.”) The “truthiness” of an object of Boolean type is self-evident: Boolean objects that are equal to You will learn more about evaluation of objects in Boolean context when you encounter logical operators in the upcoming tutorial on operators and expressions in Python. Built-In FunctionsThe Python interpreter supports many functions that are built-in: sixty-eight, as of Python 3.6. You will cover many of these in the following discussions, as they come up in context. For now, a brief overview follows, just to give a feel for what is available. See the Python documentation on built-in functions for more detail. Many of the following descriptions refer to topics and concepts that will be discussed in future tutorials. Math
Type Conversion
Iterables and Iterators
Composite Data Type
Classes, Attributes, and Inheritance
Input/Output
Variables, References, and Scope
Miscellaneous
ConclusionIn this tutorial, you learned about the built-in data types and functions Python provides. The examples given so far have all manipulated and displayed only constant values. In most programs, you are usually going to want to create objects that change in value as the program executes. Head to the next tutorial to learn about Python variables. Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Basic Data Types in Python What are the 4 main data types?4 Types of Data: Nominal, Ordinal, Discrete, Continuous | upGrad blog.
What are data types in Python?Built-in Data Types. What are the 5 data types in Python?Python has six standard Data Types:-. Numeric.. String.. Tuple.. Dictionary.. How many data types are in Python?In a programming language like Python, there are mainly 4 data types: String – It is a collection of Unicode characters (letters, numbers and symbols) that we see on a keyboard. Numerical – These data types store numerical values like integers, floating-point numbers and complex numbers.
|