Python Programming Tutorial - Part 2

Today I will define the further concepts of python programming language.
 
Note: Indentation is very important in Python code. Without indentation, it always gives an error of indentation.
 
I have already told you.
Download Python LanguageTutorials and give star on GitHub if those files are helpful for you.
 
In this article you will understand the following:
  • Numbers
  • Strings
  • List
  • Loop
These four concepts you have already used in another programming language. But in this language, they are different for writing. Python makes shortcode for faster typing and execution.
  1. Numbers:

    In Numbers, Python works as a Calculator. Because we can calculate any calculation without another code. Simply type number and operators then python gives output faster.

    Example:
    1. val = 2 + 5  
    2.   
    3. print(val ,"\n")  
    4.   
    5. val = 2 * ( 9 + 4 )  
    6.   
    7. print(val,"\n")  
    8.   
    9. val = ( 30 - 4 * 5 ) / 6  
    10.   
    11. print(val,"\n")  
    12.   
    13. val = 7 / 5  
    14.   
    15. print(val,"\n")  
    16.   
    17. val = 5 ** 2  
    18.   
    19. print(val,"\n")  
    20.   
    21. val = -5**2 #this code gives output -25 but output should be 25  
    22. # so always use parenthesis (-5)**2  
    23. print(val,"\n")  
    24.   
    25. val = (-5)**2  
    26.   
    27. print(val,"\n")  
    28.   
    29. val = 5 // 2  
    30.   
    31. print(val,"\n")  
    Output:
    7
    26
    1.6666666666666667
    1.4
    25
    -25
    25
    2

  2. Strings:

    Python Language gives more functionality to handle the string easily.

    Python language has many concepts for accessing characters in the string at any position with different types.

    Example:
    1. print('NeErAj KuMaR' ,"\n")  
    2. print("\"NeErAj KuMaR \"" ,"\n")  
    3. print('C:\windows\name' ,"\n")  
    4. print(r'C:\windows\name' ,"\n")  
    5. print('Py' 'thon' ,"\n")  
    6. word = 'NeErAj'  
    7. print("+----+----+-----+------+")  
    8. print("| N | e | E | r | A | j")  
    9. print("+----+----+-----+------+")  
    10. print("| 0 | 1 | 2 | 3 | 4 | 5 ")  
    11. print("| -6| -5| -4| -3| -2|-1")  
    12. print(word[0] ,"\n")  
    13. print(word[5] ,"\n")  
    14. print(word[-1] ,"\n")  
    15. print(word[-6] ,"\n")  
    16. print(word[0:3] ,"\n")  
    17. print(word[2:5] ,"\n")  
    18. print(word[2:]+word[:5] ,"\n")  
    19. print(len(word) ,"\n")  
    Output:
    NeErAj KuMaR
    "NeErAj KuMaR "
    C:\windows\name
    Python
    +----+----+-----+------+
    | N | e | E | r | A | j
    +----+----+-----+------+
    | 0 | 1 | 2 | 3 | 4 | 5
    | -6| -5| -4| -3| -2|-1
    N
    j
    j
    N
    NeE
    ErA
    ErAjNeErA
    6

  3. List:

    The list is also the same as the arrays concept in python language. You can access values with various types.

    But in Python, a new way with a colon( : ) gives access data from the list.

    Example:
    1. value = [ 1 , 3 , 2 , 4 , 5 ]  
    2. print(value ,"\n")  
    3. print(value[:] ,"\n")  
    4. print(value[2] ,"\n")  
    5. print(value[:4] ,"\n")  
    6. print(value+[ 12 , 13 , 54 , 33 , 42 ] ,"\n")  
    7. value.append(43)  
    8. print(value ,"\n")  
    9. value[0:2]=[ 9 , 3 , 7]  
    10. print(value ,"\n")  
    Output:
    [1, 3, 2, 4, 5]
    [1, 3, 2, 4, 5]
    2
    [1, 3, 2, 4]
    [1, 3, 2, 4, 5, 12, 13, 54, 33, 42]
    [1, 3, 2, 4, 5, 43]
    [9, 3, 7, 2, 4, 5, 43]

  4. Loop:

    In Python, Language looping is easy for getting the fast output. In Looping of Python language, use function range().

    Which runs automatic loop at the desired value in range() function.

    end=’,’ this code also useful in the loop. It denotes that each time loop finish, it adds automatic comma ‘,’ after each loop.

    Example:
    1. a, b = 01  
    2. while b < 15:  
    3.     print(b, "\n")  
    4. a, b = b, a + b  
    5. a, b = 01  
    6. while b < 15:  
    7.     print(b, end = ', ')  
    8. a, b = b, a + b  
    9. print("\n")  
    10. for i in range(5):  
    11.     print(i, "\n")  
    12. for i in range(-10, -100, -10): #-10 denote starting value# - 100 end value  
    13.     print(i, "\n")# - 10 denote difference each time in loop  
    14. OS = ["windows""linux""mac"]  
    15. for os in OS:  
    16.     print(os, len(os), "\n")  
    17. for n in range(210): #calculate number is prime or not prime  
    18.     flag = 0  
    19. for x in range(2, n):  
    20.     if n % x == 0:  
    21.         flag = 0  
    22.         break  
    23.     else :  
    24.         flag = 1  
    25. if flag == 0:  
    26.     print(n, " Not Prime\n")  
    27. else :  
    28.     print(n, " Prime\n"
    Output
    1
    1
    2
    3
    5
    8
    13
    1, 1, 2, 3, 5, 8, 13,
    0
    1
    2
    3
    4
    -10
    -20
    -30
    -40
    -50
    -60
    -70
    -80
    -90
    windows 7
    linux 5
    mac 3
    2 Not Prime
    3 Prime
    4 Not Prime
    5 Prime
    6 Not Prime
    7 Prime
    8 Not Prime
    9 Not Prime
I hope you will easily understand my code for learning the Python language.
 
Thank you.


Recommended Free Ebook
Similar Articles