Computer Science

Python Data Types

Python data types refer to the different categories of data that can be used in Python programming. These include fundamental types like integers, floats, and strings, as well as more complex types like lists, tuples, dictionaries, and sets. Understanding data types is crucial for effectively manipulating and organizing data within Python programs.

Written by Perlego with AI-assistance

5 Key excerpts on "Python Data Types"

Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.
  • Bioinformatics Algorithms
    eBook - ePub

    Bioinformatics Algorithms

    Design and Implementation in Python

    • Miguel Rocha, Pedro G. Ferreira(Authors)
    • 2018(Publication Date)
    • Academic Press
      (Publisher)
    dictionary types. These data types can be further sub-divided according to the way their elements are organized and accessed. Strings, lists, and tuples are sequence types since they have an implicit order of their elements, which can be accessed by an index value.
    Sets and dictionaries represent a collection of unordered elements. The set type implements the mathematical concept of sets of elements (any object), where the position or order of the elements is not maintained. Dictionaries are a mapping type, since they rely on a hashing strategy to map keys to the corresponding values, which can be any object.
    One important characteristic of some of these data types is that once the variables are created their value cannot be changed. These are called immutable types and include strings, tuples, and sets. An attempt to alter the composition of a variable of one of these types generates an error.
    Table 2.1 provides a summary of the different features of Python primitive and container data types. The last column indicates if the container type allows different types of their elements or not.
    Table 2.1 Features of Python built-in data types.
    Data typeComplexityOrderMutableIndexedHeterogeneous
    intprimitiveyes
    floatprimitiveyes
    complexprimitiveyes
    Booleanprimitiveyes
    stringcontaineryesnoyesno
    listcontaineryesyesyesyes
    tuplecontaineryesnoyesyes
    setcontainernononoyes
    dictionarycontainernoyesnoyes
    In Python, the data type is not defined explicitly and it is assumed during the execution by taking into account the computation context and the values assigned to the variable. This results in a more compact and clear code syntax, but may raise execution errors, for instance when the name of a variable is incorrectly written or when non-operations are performed (e.g. sum of an integer with a string).

    2.2.2 Assigning Values to Variables

    In Python, the operator
    =
    is used to assign a value to a variable name. It is the core operation in any computer program, that allows to define the dynamics of the code. This operator is different from
    ==
  • Hands on Data Science for Biologists Using Python
    • Yasha Hasija, Rajkumar Chakraborty(Authors)
    • 2021(Publication Date)
    • CRC Press
      (Publisher)
    2
    Basic Python Programming
    In this chapter, we will go through a basic understanding and overview of Python programming which is a prerequisite for any form of data analysis. Variables and operator, string, list and tuples, dictionary, conditions, loops, functions, and objects are some of the topics covered in this chapter.
    Let us begin with a familiar syntax in Python which is used for commenting a statement. If a statement or a line begins with “#”, then Python will ignore it. Comments are useful to make any code self-explanatory. We will use a lot of comments wherever required to make the code more understandable to readers.
    Code:
    #Let’s print “Hi there!” print('Hi there!')
    Output:
    Hi there!
    After executing the above code in the Jupyter Notebook by pressing “ctrl + enter”, the first statement or line starting with “#” will be ignored, and as a result, we will see “Hi there!”. Therefore, the first line is a comment which describes that the code will print “Hi there!”.

    Datatypes and Operators

    Datatypes

    As the name suggests, datatypes are the different types of data, such as numbers, characters, and Booleans that can be stored and analyzed in Python. Among various datatypes, the four most common are:
    int (integers or whole numbers)
    float (decimal numbers or floating-points)
    bool (Boolean or True/False)
    str (string or a collection of characters like a text)
    There are two important ways in which Python represents a number: int and float. Decimals numbers (float), such as 1.0, 3.14, ‒2.33, etc., will potentially consume more space than integers or whole numbers, like 1, 3, ‒4, 0, etc. Think of this way, if we take whole numbers between 0 and 1, then we will see only the two numbers 0 and 1, but, in the case of decimal numbers, we will get infinite numbers between 0.0 and 1.0. Next, we have a Boolean datatype, which is “True” or “False”, and these are used in making conditions which we will learn shortly. Lastly, “str” or the string datatype is the datatype that biologists will need and encounter the most - whether it is the DNA, RNA, and/or protein sequences or names, most of them are text or strings. Therefore, we have a separate section for strings in this chapter. It is imperative to mention here that string-type data always remains inside quotes, i.e. (‘<string data>’). For example, ‘ATGAATGC’ will be a string for Python.
  • Programming with Python for Social Scientists
    We will learn more about what lists and dictionaries are and what they do in later chapters, but suffice to say for now that there are three basic data types – integers, floats and strings – plus other objects in Python (such as lists and dictionaries) that can be assigned to any variable you declare. And, moreover, you can check the type of any variable using the type() command. Running scripts, using commands, and passing arguments around At this point, it’s also worth reflecting on what we did in terms of how we worked with Python code, so that we can apply these skills in future chapters. So, first of all, you opened up a script in the Editor window, then ran (i.e. executed) that script in the shell. This enabled you to work with that program in the shell – all the variables that were assigned in the script became usable to you in the shell. This is important – if you hadn’t run the script, variables like var1 wouldn’t have been declared and wouldn’t have been assigned values. From there, you also learned how to use a command in the shell, on a script that you have executed – in our example above, we checked the data types of different variables by typing type(my_integer) and so on. Here, we had a command called type(), and we passed that command an “argument” which was the name of a variable (i.e. my_integer). Applying this command to different variables gave us different results. This concept of commands and arguments is very useful to us too – it allows us to work with and develop the code in our scripts, to check things are as we expect, and to learn more about coding as we go
  • Data Science and Machine Learning
    eBook - ePub

    Data Science and Machine Learning

    Mathematical and Statistical Methods

    • Dirk P. Kroese, Zdravko Botev, Thomas Taimre, Radislav Vaisman(Authors)
    • 2019(Publication Date)
    F9 is equivalent to executing these lines one by one in the console.
    OBJECT
    In Python, data is represented as an object or relation between objects (see also Section D.2 ). Basic data types are numeric types (including integers, booleans, and floats), sequence types (including strings, tuples, and lists), sets, and mappings (currently, dictionaries are the only built-in mapping type).
    Strings are sequences of characters, enclosed by single or double quotes. We can print strings via the
    print
    function.
    print ("Hello World!" )
    Hello World!
    For pretty-printing output, Python strings can be formatted using the
    format
    function. The bracket syntax {i } provides a placeholder for the i -th variable to be printed, with 0 being the first index. Individual variables can be formatted separately and as desired; formatting syntax is discussed in more detail in Section D.9 .
    ☞ 477
    print ("Name:{1} (height {2} m, age {0})" .format (111,"Bilbo " ,0.84))
    Name:Bilbo (height 0.84 m, age 111)
    Lists can contain different types of objects, and are created using square brackets as in the following example:
    x = [1, 'string' , "another string" ] # Quote type is not important
    [1, 'string', ' another string ']
    Elements in lists are indexed starting from 0, and are mutable (can be changed):
    x = [1,2]
    x[0] =2
    # Note that the first index is 0
    x [2,2]
    MUTABLE
    In contrast, tuples (with round brackets) are immutable
  • Introduction to GIS Programming and Fundamentals with Python and ArcGIS®
    • Chaowei Yang(Author)
    • 2017(Publication Date)
    • CRC Press
      (Publisher)
    False.
    Code 3.5 Function type, None type, and bool type.
    Tips Different from C or Java language, Python does not support Byte, Boolean, Char, Pointer data types. 3.3.2Composite Data Types
    Another category of built-in variable data types is called a composite data type (Table 3.4 ). Composite data types are ordered and sequentially accessible via index offsets in the set. This group, also known as sequences, typically includes list, tuple, dictionary, and set. A list is different types of data separated by commas, and are included in a pair of brackets, for example, [1, 2, 3, 4]. Tuples are lists of different types of data, such as (1, “the first rated products,” “milk”) set off by parentheses. Dictionaries are lists of keys and value pairs, as shown in the following example {‘john’:‘3-1111’, ‘phil’:’3-4742’, ‘david’:‘3-1212’}
    List
    The most commonly used and important composite data type is list , which can be used to group different values together. Use list to keep a series of points, polylines, and polygons in GIS programs.
    Define : A list object can be created from: [v1, v2, v3, ….] , where elements are surrounded by a square bracket (e.g., as in Code 3.6 ).
    Code 3.6 List example.
    Operators: Composite data types are good for expressing complex operations in a single statement. Table 3.4 lists common operators shared by complex data types.
    Table 3.4 Composite Data Types and Common Operators
    Container Define Feature Examples Common Operators
    List Delimited by [ ]; mutable [‘a’, ‘b’, ‘c’]
    seq[index], seq[index1: index2], seq * expr, seq1 + seq2, obj in seq, obj not in seq, len(seq) etc
    Tuple Denoted by parenthesis () immutable (‘a’, ‘b’, ‘c’)
    dictionary {key: value, key: value, …} mutable {‘Alice’: ‘7039931234’, ‘Beth’: ‘7033801235’}
    seq[index]: gets a value for a specific element. The starting index of all sequence data is 0, and the end index is one fewer than the number of elements n in the sequence (i.e., n -1) (Code 3.6a