Must know these basic things in Python first

Must know these basic things in Python 

1. Setup: 
First you need to know how to install python3 and setup it.


2. Tuples: 

Tuple contains group of elements which can be same or different data types.
Tuple are immutable. That mean We can't modify tuple elements.
 
Tuple is similar to list but Tuples are read-only. Mean can't modify it's elements.

But list is mutable. we can modify list.

Tuple are used to store data which should not modified.

Tuple occupies less memory compare to List.
 
Tuple are represented using parenthesis ().
Example: a = (10, 55, -5, 44, "Python", "Linux")


Let's create an empty tuple:



    tuple_name = ()
    print(tuple_name)
 
    output: ()


 

 

How to create one element tuple.

 
 
    num = (1,)     # Without comma it will become an integer.
    num2 =("Python",)
    print(num)
    print(num2)
 
    output: 
    (1,)
    ('Python',) 


 
 

 
 
    a = ("Hello")
    print(a)
    print(type(a))
 
 
    output:
    Hello
    <class 'str'> 
 
 
 

    a = ("Hello",)
    print(a)
    print(type(a))
    print(len(a))
 
 
     Output:
    ('Hello',)
    <class 'tuple'> 
 
 



Ways to print tuple

 

    # one way of creation
    tupl = "one way","This is tuple"
    print(tupl)
    print(type(tupl))
 
    output:
    ('one way', 'This is tuple')
    <class 'tuple'>


 
 



Another ways to print Tuple

 
 
    # Another ways to print tuple
    tuple = ("One ways", "This is tuple")
    print(tuple)
    print(type(tuple))
 
 
    output: 
    ('One ways', 'This is tuple')
    <class 'tuple'> 
 



Nesting tuple

 
 
    # Nesting Tuples
        tup = ("Hello", "World",112,"Angela")
        tup1 = ("Washington DC",True,"Barcelona")
        tup3 = (tup, tup1)
        print(tup3)
 
    output:
     (('Hello', 'World', 112, 'Angela'), ('Washington DC', True, 'Barcelona'))
 


 


Tuple Concatenation

 
     # This is tuple Concatenation 
 
    jiraf = ("Grass", "Taller","Thin")
    jiraf2 = ("Beautiful","Love","Animal",True)
    print(jiraf + jiraf2)
 
     output:
    ('Grass', 'Taller', 'Thin', 'Beautiful', 'Love', 'Animal',True
 


 


This is Tuple Repetition


    # Repetition in Tuples
 
    this_is_tupleRepetation = ("Diehard4 ",) * 5
    print(this_is_tupleRepetation)
    print(type(this_is_tupleRepetation))


    Output:
    ('Diehard4 ', 'Diehard4 ', 'Diehard4 ', 'Diehard4 ', 'Diehard4 ')
    <class 'tuple'>


 

 

 Tuple is Immutable. Let's try to check


    # Tuple is immutable

    tup = ("Python", "C++","JavaScript")
    tup[0]= "Programmer"

    print(tup)
 
 
    Output: 
    tup[0]= "Programmer"
    TypeError: 'tuple' object does not support item assignment 
 






Tuple Slicing


 
    # Slicing in Tuples

    tup_one = ("Bengalore","Hyderabad","Noida","Silicon Velly","Tokyo","HongKong")
    # print first elements Bengalore
    print(tup_one[0])

    # print first to 4 elements
    print(tup_one[:4])

    # Lets's print elements from 3 to end
    print(tup_one[3:])

    # Let's print elements from 2 to 5
    print(tup_one[2:5])

    # Let's check index number of "HongKong'
    print(tup_one.index("HongKong"))
 
     Output:
 
     Bengalore
    ('Bengalore', 'Hyderabad', 'Noida', 'Silicon Velly')
    ('Silicon Velly', 'Tokyo', 'HongKong')
    ('Noida', 'Silicon Velly', 'Tokyo')
    5
 



Delete a Tuple


 
    # Tuple Deleting

    country = ("London", "Malaysia","Brazil")
    del country
    print(country)
 
 
    output: 
 
print(country)
    NameError: name 'country' is not defined
 
 



Find the length of a Tuple

 
    # Print the length of this variable.
    len_tuple = (122, True, False, "Lebanon")
    print(len(len_tuple))
 
    OUTPUT: 4 





Converting list into Tuple:

 
 
    list_name = ["Angela Yu",True, False,"Harry"]

    tup = (tuple(list_name))
    print(tup)
    print(type(tup))
 
    output:
 
    ('Angela Yu', True, False, 'Harry')
    <class 'tuple'> 
     

 


Tuple in Loop


## Tuple in Loops


tuple_loop = ("This is Tuple Loop")
n = 7 # Number of times loop runs

for i in range(n):
tuple_loop = (tuple_loop,)
print(tuple_loop)
 
 
    output: 
 
    ('This is Tuple Loop',)
    (('This is Tuple Loop',),)
    ((('This is Tuple Loop',),),)
    (((('This is Tuple Loop',),),),)
    ((((('This is Tuple Loop',),),),),)
    (((((('This is Tuple Loop',),),),),),)
    ((((((('This is Tuple Loop',),),),),),),)


 

 


 

3. Tokens:

4. Functions:
 
 
 

5. Dictionary:
 
 
Keys in a dictionary don't allow polymorphism.

Dictionary holds key and value. key-value is provided in the dictionary ti make it more optimized.


It is important to note that dictionaries have been modified to maintain insertion order with the release
of python 3.7, so they are now ordered collection of data values.


in python dictionary can be created by placing a sequence of elements within curly {} braces, separated by comma. Dictionary holds pairs of values, one being the key and the other corresponding 
pair elements being its Key and Value. 

Values in a dictionary can be of any data type and can be duplicated, where as keys can't be repeated 
and must be immutable.



Dictionary keys are case sensitive, the same name but different cases of key will be treated distinctly.


 
 

Creating a dictionary with integer keys.

 
    ## Creating a dictionary with integer keys.

    dict = {
    1: "Hello world", 2: "you are One", 3: "we live alone"
    }
 
    print(" Dictionary with the use of integer Keys: ")
    print(dict)
 
     output:  
     Dictionary with the use of integer Keys:
     {1: 'Hello world', 2: 'you are One', 3: 'we live alone'} 
 

 
 
 
 

Creating a dictionary with mixed keys.

 
 
   ## Creating a dictionary with mixes keys.

    dict1 = {
    "Name": "Angela Yu", 1:[1,2,3,4,5,True,False]
    }

    print(dict1)
 
 
 
    output:
 
     {'Name': 'Angela Yu', 1: [1, 2, 3, 4, 5, True, False]}


 
 
 

Creating an empty Dictionary python


    ## creating an empty dictionary

    dict3 = {}
    print("This is an empty Dictionary:")
    print(dict3)
    
    output:
    
    This is an empty Dictionary:
    {}


 
 
 

 Creating a dictionary with dict() methods.

 
    # creating dictionary with dict() method

    Dict = dict({1: "Python", 2:"For", 3:"Beginners"})
    print(Dict)
 
    output:
    {1: 'Python', 2: 'For', 3: 'Beginners'} 

 

Creating dictionary as a pair.

 
# Creating a Dictionary with each items as a pair.

    Dict = dict([(1, "python"),(2, "for"),(3, "Beginners")])
    print(Dict)
 
    output: 
    {1: 'python', 2: 'for', 3: 'Beginners'}

 

 

Creating a Nested dictionary.

 
    # Creating a nested dictionary

    dict0 = {1: "python", 2: "for",
    3:{'A': "Welcome", 'B': "to", 'C':"python"}}

    print(dict0)
 
    output: 
    {1: 'python', 2: 'for', 3: {'A': 'Welcome', 'B': 'to', 'C': 'python'}} 


 
 
 

Adding elements to a dictionary


    # First creating empty elements Dictionary
    dict5 = {}
    print(dict5)

    # Adding elements to a Dictionary
    dict5[0] = "Python"
    dict5[2] = "for"
    dict5[3] = 1
    print(dict5)

    # Adding set of values to a single Key.
    dict5['Value_set'] = 2,3,4
    print(dict5)

    output:
    {}
    {0: 'Python', 2: 'for', 3: 1}
    {0: 'Python', 2: 'for', 3: 1, 'Value_set': (2, 3, 4)} 
    
  
 
 
 
 
 

6. Modules:

7. Data types:

8. Operations:
 
9. Branching:
 
10.  Loops:

11. Lists:

12. Sorting:

13. Searching:

14. Python Debugger:

Post a Comment (0)
Previous Post Next Post