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.
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.
- 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:
- val = 2 + 5
-
- print(val ,"\n")
-
- val = 2 * ( 9 + 4 )
-
- print(val,"\n")
-
- val = ( 30 - 4 * 5 ) / 6
-
- print(val,"\n")
-
- val = 7 / 5
-
- print(val,"\n")
-
- val = 5 ** 2
-
- print(val,"\n")
-
- val = -5**2 #this code gives output -25 but output should be 25
- # so always use parenthesis (-5)**2
- print(val,"\n")
-
- val = (-5)**2
-
- print(val,"\n")
-
- val = 5
-
- print(val,"\n")
Output:
7
26
1.6666666666666667
1.4
25
-25
25
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:
- print('NeErAj KuMaR' ,"\n")
- print("\"NeErAj KuMaR \"" ,"\n")
- print('C:\windows\name' ,"\n")
- print(r'C:\windows\name' ,"\n")
- print('Py' 'thon' ,"\n")
- word = 'NeErAj'
- print("+----+----+-----+------+")
- print("| N | e | E | r | A | j")
- print("+----+----+-----+------+")
- print("| 0 | 1 | 2 | 3 | 4 | 5 ")
- print("| -6| -5| -4| -3| -2|-1")
- print(word[0] ,"\n")
- print(word[5] ,"\n")
- print(word[-1] ,"\n")
- print(word[-6] ,"\n")
- print(word[0:3] ,"\n")
- print(word[2:5] ,"\n")
- print(word[2:]+word[:5] ,"\n")
- 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
- 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:
- value = [ 1 , 3 , 2 , 4 , 5 ]
- print(value ,"\n")
- print(value[:] ,"\n")
- print(value[2] ,"\n")
- print(value[:4] ,"\n")
- print(value+[ 12 , 13 , 54 , 33 , 42 ] ,"\n")
- value.append(43)
- print(value ,"\n")
- value[0:2]=[ 9 , 3 , 7]
- 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]
- 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:
- a, b = 0, 1
- while b < 15:
- print(b, "\n")
- a, b = b, a + b
- a, b = 0, 1
- while b < 15:
- print(b, end = ', ')
- a, b = b, a + b
- print("\n")
- for i in range(5):
- print(i, "\n")
- for i in range(-10, -100, -10):
- print(i, "\n")
- OS = ["windows", "linux", "mac"]
- for os in OS:
- print(os, len(os), "\n")
- for n in range(2, 10):
- flag = 0
- for x in range(2, n):
- if n % x == 0:
- flag = 0
- break
- else :
- flag = 1
- if flag == 0:
- print(n, " Not Prime\n")
- else :
- 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.