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 that 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 short code 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 string easily.

    Python language has many concepts for accessing characters in 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:

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

    But in Python a new way with colon( : ) gives access data from 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 fast output. In Looping of Python language, use function range().

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

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

    Example:
    1. a, b = 0, 1  
    2. while b < 15:  
    3. print(b ,"\n")  
    4. a, b = b, a+b  
    5. a, b = 0, 1  
    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  
    13. #-100 end value  
    14. print(i ,"\n") #-10 denote difference each time in loop  
    15. OS = [ "windows","linux","mac"]  
    16. for os in OS:  
    17. print(os, len(os) ,"\n")  
    18. for n in range(2, 10): # calculate number is prime or not prime  
    19. flag=0  
    20. for x in range(2, n):  
    21. if n % x == 0:  
    22. flag=0  
    23. break  
    24. else:  
    25. flag=1  
    26. if flag==0:  
    27. print(n , " Not Prime\n")  
    28. else:  
    29. 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.

Up Next
    Ebook Download
    View all
    Learn
    View all