Python Basic Tutorials

Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more

Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c

To make everything easy: We refer CPython as Python

Python Loops

Loops are basically the repetition of a certain block of code for a number of iteration or until a condition is satisfied. In python, loops are used to iterate through the iterables like List, Tuples, Dictionary, Strings, etc.

There are 2 kinds of loops in Python

  • for loop

    In python, For loops are basically used to iterate over the sequences or iterables like a List, Tuple, Dictionary, Strings, etc.

    With the for loop, we can execute a set of statements, once for each item in a list, tuple, set, etc.

    Syntax
    
    for item in sequence:
        #loop-body
    
    Example 1
    
    #looping through the list
    nums=[1,2,4,53,3]
    for num in nums:
        print(num)
    
    Output
                
    1
    2
    4
    53
    3 
    Example 2
                
    for i in range(5):
        print(i)
    Output
    
    0
    1
    2
    3
    4
    Example 3
    
    languages=['python','c','c++','java','perl','ruby']
    for i in range(len(languages))
        print(i)
    
    Output
                
    python
    c
    c++
    java
    perl
    ruby
  • While loop

    While loop in python is used to perform the iteration over the block of code until the certain given condition is true. While loop is used generally while we don't know the number of iterations to be iterated

    Syntax
    
    while Conditional-expression:
          #block of code
            
    Example 1
    
    #program to print 1 to 10 numbers
    num=1
    while(num<10):
        print(num)
        num+=1
    
    Output
    
    1
    2
    3
    4
    5
    6
    7
    8
    9 
    Example 2
    
    #program to find out factorial of any number
    num=int(input("Enter number: "))
    factorial=1
    i=1
    while(i<=num):
        factorial*=i
        i=i+1
        print("Factorial of {} is {}.".format(num,factorial})
    
    Output
    
    Enter number: 5
    Factorial of 5 is 120.
The range() function

The range() function is a very useful function that is used to generate a sequence of numbers. For example, range(5) will generate 0 to 5. Also, we can pass parameters to this function to make it more useful and flexible. The syntax would be range(start,end,step-size). Here, range(1,10,2) will produce all the odd numbers between 1 to 10.

Example

>>>print(list(range(10))
   [1,2,3,4,5,6,7,8,9]
>>>print(list(range(2,7))
   [2,3,4,5,6]
>>>print(list(range(2,10,2))
   [2,4,6,8,10]