python Book


Hey system this is 19Oct 12:32am | This is chapter:2  38 no page. 


 Hey system this is 19 Oct 12:32am | This is chapter:2 38 no page. 

 Hey system this is 9 Dec 11:15pm | This is chapter:3 completed.page-70


1.  >>> (Triple greater than symbols)----->     this is python Prompt.

2. need help about python. just type  >>> help()

3. API - Application Programming Interface.



Datatype in Python 


Comment:

 
A comment is used to describe the features of a program.
Comment increase readability of our programs.
Writing  comments will make our programs clear to others.


There are 2 types of comment in python.

i. Single line comment.
 Single line comment start with # symbols.

ii. Multi line comment.
 Multi line comment start with Triple double quotes (""" """) and triple single quotes (''' ''').
Multi line comment mark several lines as a comment in the beginning and ending of the block.

The triple double quotes and triple single quotes are called multi line comment.
 
 


 

Docstring:

If we write strings inside  Triple double quotes (""" """) and triple single quotes (''' '''),
are written as first statements in a module, function, class, or a method, then these strings are called 
documentation string or docstring.
 
 
Example of docstring

 
    def add(x,y):
        """
        you know this is a docstring example.
        here we are wrinting a function.
        """
        print(f"sum of x and y is {x + y}")

        """
        above we use f-string
        we can also print like this below
        """
        print("sum of x and y is", x + y)

    def message():
        '''
        This function display a message.
        This is a welcome message to the user.
        '''
        print("Welcome to the python")

    # Now call the function
    add(25,30)
    message()






 Do you know ?
= (equal) is an assignment operator.


Identifier: 

The name given to a variable is called Identifier
Identifiers represents names of variables, Function, Objects or any such things.



Here message is an identifier of a function

    def message():

    # Here message is an identifier of a function
        '''
        This function display a message.
        This is a welcome message to the user.
        '''
        print("Welcome to the python")
     
    message()
    


Here a ,b c are identifier of  variable.

    a = 10
    b = 20
    c = a + b
    # a ,b c are identifier of a variable.

    print(type(c))
    print(f"Sum of your result is {c}")




Datatypes in Python:

A datatype represents the type of data stored into a variable or memory.


Built in Datatype: 
The datatype which are already available in Python Language are called Built-in Datatypes.
 
 
Built-in Data types are 5 types.

i. None Type
In python, None data type represents an objects  that does not contain any value.
In Boolean expression, None data type represents False.



ii. Numeric Type
Numeric types represents numbers. 
 
There are three subtypes.

1. int          2. float         3. complex

1. int (integer)

The int datatypes represents an integer number. An integer number is a number without any decimal point or fraction part.
 
Example: 1, 5000, 50, 40, 9, 3, 122, -55, -10000, 500000000 etc.

Let's store an integer number into variable x, y, z.


    x = 50
    y = -5000
    z = 9

 
 
 
 
 
2. float (floating point number)
The float datatype represents floating point number.
A floating point number is  a number that contains a decimal point number.

for example: 0.5, 10.2, 500.5, 522.225, -5.256, -90.2, 0.0001, 999.9999 etc.


Lets store some floating point number into variable a,b,c

 
    a = 500000000.859
    b = .555
    c = 555.2025
 
 
 
 
 
 
 
3. complex
 
A  complex number is a number that is written in the form of  a+bj or a+bJ.
Here "a" represents the real part of the number and "b" represent the imaginary part of the number.

The suffix j and after "b" indicates the square root value of -1.
The parts ''a'' and "b" may contain integer and floats.

Example: 3 + 5j, -1-5.5J, 0.2+10.5J are all complex number.
c1 = -1-5.5J


Boolean

Note: Boolean, Python internally represents True as 1 and False 0.
A blank string  like " " is also represented as False.
 
 
 
    print(True + True)
    output: 2 
     
    print(True - True)    
    output: 0 
    
    print(True - False)
    output: 1  
 
 






iii. Sequences
Generally, a sequence represents a group of elements or items. 
For example, a group of integer numbers will form a sequence.

There are 6 types of Sequence in Python.

1. Str
In python, str represents string data type. A string is represented by a group of character.

strings are enclosed in single quotes or double quotes. Both are valid.

str1 = " This is double quotes "
str2 = ' This is single quotes ' 

we can also write strings inside """ (Triple double quotes) and ''' (Triple single quotes).
 
str3 = """This is triple double 
                quotes string. we can add more character here """ 

str4 = ''' This is Triple single quotes string.
            This is same as double triple quotes.'''
 
 
 
Note: The Slice operator represents square bracket [ this is under a Square bracket ].

 
   
    str = 'welcome to the Python World'
    print(str)
    print(str[-1])
    print(str[:34])
    print(str[11:])
    print(str[5:20])
    print(len(str))
    
    Output:

    welcome to the Python World
    d
    welcome to the Python World
    the Python World
    me to the Pytho
    27



 

2. Bytes datatype

The  Bytes  datatype  represents a group of byte numbers just like an array does.
A byte number is any positive integer from 0 to 255 (inclusive).
 
 
    elements = [10, 20, 0, 40, 100, 52, 45,788]
 
     output:
     bytes must be in range(0, 256)
 
 
 
Bytes datatype can store numbers in the range from 0 to 255 and it cannot even store negative  numbers.

For example:
 
 
    elements = [10, 20, 0, 40, 100, 52, 45,78]
    x = bytes(elements)
    print(x)
    print(x[5])
 
    output: 52 
 

 
 
 

3. Bytearray Datatype

The bytearray datatype is similar to bytes datatype. The difference is that the bytes type array cannot 
be modified but the bytearray type array can be modified.


 
    elements = [1,25,45,96,78,45,22,14,71,56]
    x = bytearray(elements)
    # print(elements[0])



    ## we can modify or edit the elements of the bytearray.
    x[0] = 88 # replace 0 th element by 88
    x[1] = 99 # replace 1st elements by 99
    # print(elements[0])

    for i in x: print(i)
 






4. List
A list represents a group of elements. 
The main difference between a list and an array is that a list can store different types of elements.
But an array can store only one type of elements.
 
 
 
    list = [99, 45.25,"Angela",-88,"New york",7896, "London" ]

    # print whole list
    print(list)
 
    ## print first element
    print(list[0])
 
    # print elements from 1 to end
    print(list[1:])
 
    # print elements from 0 to 5
    print(list[:5])
 
    # print -3 element from right sides
    print(list[-3])
 
    # multiply list elements with 2
    print(list * 2)
 
     output:
 
     [99, 45.25, 'Angela', -88, 'New york', 7896, 'London']
     99
     [45.25, 'Angela', -88, 'New york', 7896, 'London']
     [99, 45.25, 'Angela', -88, 'New york']
    New york
    [99, 45.25, 'Angela', -88, 'New york', 7896, 'London', 99,
     45.25, 'Angela', -88, 'New york', 7896, 'London']
 
 
 
 
 
 

5. Tuple
A tuple is similar to a list. 
A tuple contains a group of elements which can be of different types.
The elements in the tuple are separated by commas and enclosed in parentheses ().
Where as the list elements can be modified, it is not possible to modify the tuple elements.

Let's create a tuple.

 
    tple = (10, -20, 15.5,"Berlin", "Mary")
    # It will whole tuple elements
    print(tple)
    # print tuple 1 st elements
    print(tple[0])
    # print tuple from 1 to 4
    print(tple[1:4])
    # print 1 st elements from the last from right side
    print(tple[-1])
    # make double tuple elements
    print(tple * 2)

    #TypeError: 'tuple' object does not support item assignment
    # tple[0]="Qazi"



 
 
 
 
 

6. Range
The range datatype represents a sequence of numbers. 
The number in the range are not modifiable. 
Generally, range is used for repeating a for loop for a specific number of times.

Let's create a range number:

 
    r = range(10)
    for i in r:
        print(i)
 
 
    output:

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
 
   
 
 
 
 

iv. Sets
 
A set is an unordered collection of elements much like a set in Mathematics.
The order of elements is not maintained in the sets. It means the elements may not appear in the same order as they are entered into the set.
A set does not accept duplicate elements.
 
There are two sub types of set.
a. Set Datatype
b. Frozenset datatype.


Set Datatype:
 
To create a set, we should enter the elements separated by commas inside curly
braces {}.
 
 
 
 ## This is a set.
sett = {10, 20, 30, 20, 50}
print(sett)




# ch = set("Hello")
# print(ch)

## we can convert a list into a set using the set() function.
lst = [1,2,5,4,3]
s = set(lst)
print(s)


## Update() method is used to add elements to a set as:
s.update([50,60])
print(s)


## remove() method is used to remove any particular elements from a set as:
s.remove(50)
print(s)


 

 
  
Frozenset datatype:

The frozenset datatype is same as the set datatype.
The main difference is that the elements in the set datatype can be modified, where as the elements of frozenset cannot be modified.
we can create a frozenset by passing a set to frozenset() function.
 
 
 
 
 ## This is frozenset.

fset = {50,60,70,80,90}
print(fset)
      
    output: {80, 50, 70, 90, 60}



## Another way of creating a frozenset is by passing a string
##(a group of characters) to the frozenet() function as:

fs = frozenset("abcdefgh")
print(fs)
 
     output: 
     frozenset({'b', 'f', 'a', 'c', 'h', 'd', 'g', 'e'})


 
 






V. Mapping Type:

The dict datatype is an example for a map.
The "dict " represents a "dictionary" that contains pair of elements such that the first elements represents the "key" and the next one becomes its value.

The key and it's value should be separated by a colon (:) and every pair should 
be separated by a comma. Al the elements should be enclosed inside curly brackets{}.
 

we can create a dictionary by typing the roll numbers and names of students.
Here roll numbers are keys and names will become values.
 
 

we write these key value pairs inside curly braces as:
 
 
     d = {10: "Kamal", 11: "Pranav", 12: "Helsenki", 13:
"Anup", 14: "Angela", 15: "Syed"}
 
 


We can create an empty dictionary without any elements.

 
    d = {}
 


we can store the key and value into d as:

 
    d[10] = "Japan"
d[11] = "Spain"
    
    print(d)
 
    output: {10: 'Japan', 11: 'Spain'}
 





some example for dictionary:

 
 ## This is a dictionary

dict = {10: "syed", 11: "Pranav", 12: "Helsenki", 13:
"Anup", 14: "Angela", 15: "Kamal"}

# It will print whole elements of dict
print(dict)

# it will print only 11 key from the elements.
print(dict[11])

## it will print only keys
print(dict.keys())

## it will print only values
print(dict.values())

# Adding value "Angela yu" to the key 11
dict[11] = " Angela Yu"
print(dict)
 
    ## deleting key 13
    del dict[13]
    print(dict)


 
 
 
 
 
User-defined datatypes:
 
The datatypes which can be created by the programmers are called User-defined datatypes.

For eample: An array, A class, or a module is user defined datatypes.




 
 
 






إرسال تعليق (0)
أحدث أقدم