***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***
***Use tabs for Content***

POSTS

    Standard Data Types


    Python has five standard data types −

    • Numbers
    • Dictionary
    • Boolean
    • Set
    • Sequence

    1. Python Numbers

    Number data types store numeric values.

    Python supports four different numerical types −

    • int (signed integers)
    • long (long integers, they can also be represented in octal and hexadecimal)
    • float (floating point real values)
    • complex (complex numbers)

    To verify the type of any object in Python, use the type( ) function

    >>> type(10)

    <class 'int'>

    >>> a=11

    >>> print(type(a))

    <class 'int'>

    2. Python Strings

    Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes.

    Example:

    str = 'Hello World!'

    Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.

    Example:

    print str[0]       # Prints first character of the string

    print str[2:5]     # Prints characters starting from 3rd to 5th

    print str[2:]      # Prints string starting from 3rd character

    The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator.

    Example:

    print str + "TEST" # Prints concatenated string

    3. Python Lists

    Lists are the most versatile of Python's compound data types.

    A list contains items separated by commas and enclosed within square brackets ([]).

    Lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.

    The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator.

    Example:

    list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]

    print list[0]       # Prints first element of the list

    print list[1:3]     # Prints elements starting from 2nd till 3rd

    print list[2:]      # Prints elements starting from 3rd element

    print list * 2      # Prints list two times

    4. Python Tuples

    A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

    The main differences between lists and tuples is that Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.

    Example:

    my_tuple = (1, 2, 3, 4, 5)

    >>> print(my_tuple)

    (1, 2, 3, 4, 5)

    5. Python Dictionary

    A dictionary is an object that stores a collection of data. Each element in a dictionary has two parts: a key and a value. A key is used to locate a specific value. Dictionary elements are commonly referred to as key-value pairs.

    A dictionary created by enclosing the elements inside a set of curly braces ( {} ). An element consists of a key, followed by a colon, followed by a value. The elements are separated by commas.

    Example:

    phonebook = {'Ashraf':'555−1111', 'Amjad':'555−2222', 'Anwar':'555-3333'}

    The values in a dictionary can be objects of any type, but the keys must be immutable objects. For example, keys can be strings, integers, floating-point values, or tuples. Keys cannot be lists or any other type of immutable object.

    6. Python boolean

    Python boolean type is one of the built-in data types provided by Python, which are defined by the True or False keywords. Generally, it is used to represent the truth values of the expressions. 

    Numbers can be used as bool values by using Python’s built-in bool() method. Any integer, floating-point number, or complex number having zero as a value is considered as False, while if they are having value as any positive or negative number then it is considered as True.

    Example:

    var1 = 0

    print(bool(var1))

    var2 = 1

    print(bool(var2))

    var3 = -9.7

    print(bool(var3))

    Output:

    False

    True

    True

    7. Python Set

    In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements.

    Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by ‘comma’. Type of elements in a set need not be the same, various mixed-up data type values can also be passed to the set.

    Example:

    set1 = set("GeeksForGeeks")  # using string

    set2 = set(["Geeks", "For", "Geeks"])  #using list

    set3 = set([1, 2, 'Geeks', 4, 'For', 6, 'Geeks'])   #using mixed values